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