DCCL v5
Loading...
Searching...
No Matches
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#include "dccl/logger.h"
32
33using dccl::Bitset;
34
35int main(int argc, char* argv[])
36{
37 bool verbose = false;
38 for (int i = 1; i < argc; ++i)
39 {
40 if (argv[i] && argv[i][0] == '-' && argv[i][1] == 'v' && argv[i][2] == '\0')
41 verbose = true;
42 }
43
44 dccl::dlog.connect(verbose ? dccl::logger::ALL : dccl::logger::WARN_PLUS, &std::cerr);
45
46 // construct
47 unsigned long value = 23;
48 Bitset bits(8, value);
49 std::string s = bits.to_string();
50 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << bits << std::endl;
51 assert(s == std::string("00010111"));
52
53 // bitshift
54 bits <<= 2;
55 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << bits << std::endl;
56 s = bits.to_string();
57 assert(s == std::string("01011100"));
58
59 bits >>= 1;
60 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << bits << std::endl;
61 s = bits.to_string();
62 assert(s == std::string("00101110"));
63
64 s = (bits << 3).to_string();
65 assert(s == std::string("01110000"));
66
67 s = (bits >> 2).to_string();
68 assert(s == std::string("00001011"));
69
70 // logic
71 unsigned long v1 = 14, v2 = 679;
72 Bitset bits1(15, v1), bits2(15, v2);
73
74 assert((bits1 & bits2).to_ulong() == (v1 & v2));
75 assert((bits1 | bits2).to_ulong() == (v1 | v2));
76 assert((bits1 ^ bits2).to_ulong() == (v1 ^ v2));
77
78 assert((bits < bits1) == (value < v1));
79 assert((bits1 < bits2) == (v1 < v2));
80 assert((bits2 < bits1) == (v2 < v1));
81
82 using namespace std::rel_ops;
83
84 assert((bits1 > bits2) == (v1 > v2));
85 assert((bits1 >= bits2) == (v1 >= v2));
86 assert((bits1 <= bits2) == (v1 <= v2));
87 assert((bits1 != bits2) == (v1 != v2));
88
89 assert((Bitset(8, 23)) == Bitset(8, 23));
90 assert((Bitset(16, 23)) != Bitset(8, 23));
91 assert((Bitset(16, 0x0001)) != Bitset(16, 0x1001));
92
93 assert(dccl::hex_encode(bits2.to_byte_string()) == "a702");
94 bits2.from_byte_string(dccl::hex_decode("12a502"));
95 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << bits2.size() << ": " << bits2 << std::endl;
96 assert(bits2.to_ulong() == 0x02a512);
97
98 // get_more_bits;
99 {
100 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << std::endl;
101 Bitset parent(8, 0xD1);
102 Bitset child(4, 0, &parent);
103
104 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "parent: " << parent << std::endl;
105 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "child: " << child << std::endl;
106
107 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "get more bits: 4" << std::endl;
108 child.get_more_bits(4);
109
110 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "parent: " << parent << std::endl;
111 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "child: " << child << std::endl;
112
113 assert(child.size() == 8);
114 assert(parent.size() == 4);
115
116 assert(child.to_ulong() == 0x10);
117 assert(parent.to_ulong() == 0xD);
118 }
119
120 {
121 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << std::endl;
122 Bitset grandparent(8, 0xD1);
123 Bitset parent(8, 0x02, &grandparent);
124 Bitset child(4, 0, &parent);
125
126 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "grandparent: " << grandparent << std::endl;
127 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "parent: " << parent << std::endl;
128 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "child: " << child << std::endl;
129
130 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "get more bits: 4" << std::endl;
131 child.get_more_bits(4);
132
133 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "grandparent: " << grandparent << std::endl;
134 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "parent: " << parent << std::endl;
135 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "child: " << child << std::endl;
136
137 assert(child.size() == 8);
138 assert(parent.size() == 4);
139 assert(grandparent.size() == 8);
140
141 assert(child.to_ulong() == 0x20);
142 assert(parent.to_ulong() == 0x0);
143 assert(grandparent.to_ulong() == 0xD1);
144 }
145
146 {
147 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << std::endl;
148 Bitset grandparent(8, 0xD1);
149 Bitset parent(&grandparent);
150 Bitset child(4, 0, &parent);
151
152 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "grandparent: " << grandparent << std::endl;
153 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "parent: " << parent << std::endl;
154 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "child: " << child << std::endl;
155
156 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "get more bits: 4" << std::endl;
157 child.get_more_bits(4);
158
159 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "grandparent: " << grandparent << std::endl;
160 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "parent: " << parent << std::endl;
161 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "child: " << child << std::endl;
162
163 assert(child.size() == 8);
164 assert(parent.size() == 0);
165 assert(grandparent.size() == 4);
166
167 assert(child.to_ulong() == 0x10);
168 assert(parent.to_ulong() == 0x0);
169 assert(grandparent.to_ulong() == 0xD);
170 }
171
172 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "all tests passed" << std::endl;
173
174 return 0;
175}
A variable size container of bits (subclassed from std::deque<bool>) with an optional hierarchy....
Definition bitset.h:43
bool is(logger::Verbosity verbosity, logger::Group group=logger::GENERAL)
Indicates the verbosity of the Logger until the next std::flush or std::endl. The boolean return is u...
Definition logger.h:191
void connect(int verbosity_mask, Slot slot)
Connect the output of one or more given verbosities to a slot (function pointer or similar)
Definition logger.h:213
Dynamic Compact Control Language namespace.
Definition any.h:28
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:95
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:46