29#include "../../binary.h"
30#include "../../codec.h"
35int main(
int argc,
char* argv[])
38 for (
int i = 1; i < argc; ++i)
40 if (argv[i] && argv[i][0] ==
'-' && argv[i][1] ==
'v' && argv[i][2] ==
'\0')
44 dccl::dlog.
connect(verbose ? dccl::logger::ALL :
dccl::logger::WARN_PLUS, &std::cerr);
52 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"=== Testing CRC-16 ===" << std::endl;
53 codec.load<TestCRC16>();
54 if (dccl::dlog.is(dccl::logger::INFO))
56 codec.info<TestCRC16>(&dccl::dlog);
59 TestCRC16 msg_in, msg_out;
64 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Message in:\n" << msg_in.DebugString() << std::endl;
65 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Encoding..." << std::endl;
67 codec.encode(&bytes, msg_in);
68 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Encoded (hex): " <<
dccl::hex_encode(bytes) << std::endl;
70 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Decoding..." << std::endl;
73 codec.decode(bytes, &msg_out);
75 catch (
const std::exception& e)
77 std::cerr <<
"UNEXPECTED exception during decode: " << e.what() << std::endl;
80 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Message out:\n" << msg_out.DebugString() << std::endl;
82 assert(msg_out.x() == msg_in.x());
83 assert(msg_out.y() == msg_in.y());
84 assert(msg_out.crc() != 0);
87 assert(msg_out.crc() == msg_out.crc());
90 std::string corrupt_bytes = bytes;
91 corrupt_bytes[corrupt_bytes.size() - 1] ^= 0xFF;
94 TestCRC16 corrupt_out;
95 codec.decode(corrupt_bytes, &corrupt_out);
96 std::cerr <<
"ERROR: Corrupt message did not throw exception!" << std::endl;
99 catch (
const std::exception& e)
101 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Caught expected exception for corrupt CRC-16: " << e.what() << std::endl;
104 codec.unload<TestCRC16>();
105 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"CRC-16 tests passed!" << std::endl;
112 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"\n=== Testing CRC-32 ===" << std::endl;
113 codec.load<TestCRC32>();
114 if (dccl::dlog.is(dccl::logger::INFO))
116 codec.info<TestCRC32>(&dccl::dlog);
119 TestCRC32 msg_in, msg_out;
124 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Message in:\n" << msg_in.DebugString() << std::endl;
125 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Encoding..." << std::endl;
127 codec.encode(&bytes, msg_in);
128 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Encoded (hex): " <<
dccl::hex_encode(bytes) << std::endl;
130 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Decoding..." << std::endl;
131 codec.decode(bytes, &msg_out);
132 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Message out:\n" << msg_out.DebugString() << std::endl;
134 assert(msg_out.x() == msg_in.x());
135 assert(msg_out.y() == msg_in.y());
136 assert(msg_out.crc() != 0);
139 std::string corrupt_bytes = bytes;
140 corrupt_bytes[corrupt_bytes.size() - 1] ^= 0xFF;
143 TestCRC32 corrupt_out;
144 codec.decode(corrupt_bytes, &corrupt_out);
145 std::cerr <<
"ERROR: Corrupt message did not throw exception!" << std::endl;
148 catch (
const std::exception& e)
150 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Caught expected exception for corrupt CRC-32: " << e.what() << std::endl;
153 codec.unload<TestCRC32>();
154 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"CRC-32 tests passed!" << std::endl;
161 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"\n=== Testing CRC-16 determinism ===" << std::endl;
162 codec.load<TestCRC16>();
169 std::string bytes1, bytes2;
170 codec.encode(&bytes1, msg);
171 codec.encode(&bytes2, msg);
172 assert(bytes1 == bytes2);
173 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"CRC is deterministic." << std::endl;
181 codec.encode(&bytes3, msg2);
182 assert(bytes3 != bytes1);
183 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"CRC differs for different data." << std::endl;
185 codec.unload<TestCRC16>();
186 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Determinism tests passed!" << std::endl;
189 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"\nAll CRC tests passed!" << std::endl;
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
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...
void connect(int verbosity_mask, Slot slot)
Connect the output of one or more given verbosities to a slot (function pointer or similar)
Dynamic Compact Control Language namespace.
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...