DCCL v4
test.cpp
1 // Copyright 2013-2023:
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 // File authors:
6 // Toby Schneider <toby@gobysoft.org>
7 //
8 //
9 // This file is part of the Dynamic Compact Control Language Library
10 // ("DCCL").
11 //
12 // DCCL is free software: you can redistribute it and/or modify
13 // it under the terms of the GNU Lesser General Public License as published by
14 // the Free Software Foundation, either version 2.1 of the License, or
15 // (at your option) any later version.
16 //
17 // DCCL is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public License
23 // along with DCCL. If not, see <http://www.gnu.org/licenses/>.
24 #include <cassert>
25 #include <iostream>
26 #include <utility>
27 
28 #include "../../binary.h"
29 #include "../../bitset.h"
30 
31 using dccl::Bitset;
32 
33 int main()
34 {
35  // construct
36  unsigned long value = 23;
37  Bitset bits(8, value);
38  std::string s = bits.to_string();
39  std::cout << bits << std::endl;
40  assert(s == std::string("00010111"));
41 
42  // bitshift
43  bits <<= 2;
44  std::cout << bits << std::endl;
45  s = bits.to_string();
46  assert(s == std::string("01011100"));
47 
48  bits >>= 1;
49  std::cout << bits << std::endl;
50  s = bits.to_string();
51  assert(s == std::string("00101110"));
52 
53  s = (bits << 3).to_string();
54  assert(s == std::string("01110000"));
55 
56  s = (bits >> 2).to_string();
57  assert(s == std::string("00001011"));
58 
59  // logic
60  unsigned long v1 = 14, v2 = 679;
61  Bitset bits1(15, v1), bits2(15, v2);
62 
63  assert((bits1 & bits2).to_ulong() == (v1 & v2));
64  assert((bits1 | bits2).to_ulong() == (v1 | v2));
65  assert((bits1 ^ bits2).to_ulong() == (v1 ^ v2));
66 
67  assert((bits < bits1) == (value < v1));
68  assert((bits1 < bits2) == (v1 < v2));
69  assert((bits2 < bits1) == (v2 < v1));
70 
71  using namespace std::rel_ops;
72 
73  assert((bits1 > bits2) == (v1 > v2));
74  assert((bits1 >= bits2) == (v1 >= v2));
75  assert((bits1 <= bits2) == (v1 <= v2));
76  assert((bits1 != bits2) == (v1 != v2));
77 
78  assert((Bitset(8, 23)) == Bitset(8, 23));
79  assert((Bitset(16, 23)) != Bitset(8, 23));
80  assert((Bitset(16, 0x0001)) != Bitset(16, 0x1001));
81 
82  assert(dccl::hex_encode(bits2.to_byte_string()) == "a702");
83  bits2.from_byte_string(dccl::hex_decode("12a502"));
84  std::cout << bits2.size() << ": " << bits2 << std::endl;
85  assert(bits2.to_ulong() == 0x02a512);
86 
87  // get_more_bits;
88  {
89  std::cout << std::endl;
90  Bitset parent(8, 0xD1);
91  Bitset child(4, 0, &parent);
92 
93  std::cout << "parent: " << parent << std::endl;
94  std::cout << "child: " << child << std::endl;
95 
96  std::cout << "get more bits: 4" << std::endl;
97  child.get_more_bits(4);
98 
99  std::cout << "parent: " << parent << std::endl;
100  std::cout << "child: " << child << std::endl;
101 
102  assert(child.size() == 8);
103  assert(parent.size() == 4);
104 
105  assert(child.to_ulong() == 0x10);
106  assert(parent.to_ulong() == 0xD);
107  }
108 
109  {
110  std::cout << std::endl;
111  Bitset grandparent(8, 0xD1);
112  Bitset parent(8, 0x02, &grandparent);
113  Bitset child(4, 0, &parent);
114 
115  std::cout << "grandparent: " << grandparent << std::endl;
116  std::cout << "parent: " << parent << std::endl;
117  std::cout << "child: " << child << std::endl;
118 
119  std::cout << "get more bits: 4" << std::endl;
120  child.get_more_bits(4);
121 
122  std::cout << "grandparent: " << grandparent << std::endl;
123  std::cout << "parent: " << parent << std::endl;
124  std::cout << "child: " << child << std::endl;
125 
126  assert(child.size() == 8);
127  assert(parent.size() == 4);
128  assert(grandparent.size() == 8);
129 
130  assert(child.to_ulong() == 0x20);
131  assert(parent.to_ulong() == 0x0);
132  assert(grandparent.to_ulong() == 0xD1);
133  }
134 
135  {
136  std::cout << std::endl;
137  Bitset grandparent(8, 0xD1);
138  Bitset parent(&grandparent);
139  Bitset child(4, 0, &parent);
140 
141  std::cout << "grandparent: " << grandparent << std::endl;
142  std::cout << "parent: " << parent << std::endl;
143  std::cout << "child: " << child << std::endl;
144 
145  std::cout << "get more bits: 4" << std::endl;
146  child.get_more_bits(4);
147 
148  std::cout << "grandparent: " << grandparent << std::endl;
149  std::cout << "parent: " << parent << std::endl;
150  std::cout << "child: " << child << std::endl;
151 
152  assert(child.size() == 8);
153  assert(parent.size() == 0);
154  assert(grandparent.size() == 4);
155 
156  assert(child.to_ulong() == 0x10);
157  assert(parent.to_ulong() == 0x0);
158  assert(grandparent.to_ulong() == 0xD);
159  }
160 
161  std::cout << "all tests passed" << std::endl;
162 
163  return 0;
164 }
dccl::hex_encode
void hex_encode(CharIterator begin, CharIterator end, std::string *out, bool upper_case=false)
Encodes a (little-endian) hexadecimal string from a byte string. Index 0 of begin is written to index...
Definition: binary.h:100
dccl::Bitset
A variable size container of bits (subclassed from std::deque<bool>) with an optional hierarchy....
Definition: bitset.h:42
dccl::hex_decode
void hex_decode(const std::string &in, std::string *out)
Decodes a (little-endian) hexadecimal string to a byte string. Index 0 and 1 (first byte) of in are w...
Definition: binary.h:51