27#include "../../native_protobuf/dccl_native_protobuf.h"
29#include "../../binary.h"
30#include "../../codec.h"
36template <
typename Msg1,
typename Msg2>
38std::pair<std::size_t, std::size_t> compute_hashes()
40 std::size_t hash1 = codec.load<Msg1>();
42 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog << Msg1::descriptor()->full_name() <<
": " << dccl::hash_as_string(hash1)
44 std::size_t hash2 = codec.load<Msg2>();
46 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog << Msg2::descriptor()->full_name() <<
": " << dccl::hash_as_string(hash2)
48 return std::make_pair(hash1, hash2);
51template <
typename Msg1,
typename Msg2>
void expect_same()
53 auto hashes = compute_hashes<Msg1, Msg2>();
54 assert(hashes.first == hashes.second);
57template <
typename Msg1,
typename Msg2>
void expect_different()
59 auto hashes = compute_hashes<Msg1, Msg2>();
60 assert(hashes.first != hashes.second);
63int main(
int argc,
char* argv[])
66 for (
int i = 1; i < argc; ++i)
68 if (argv[i] && argv[i][0] ==
'-' && argv[i][1] ==
'v' && argv[i][2] ==
'\0')
72 dccl::dlog.
connect(verbose ? dccl::logger::ALL :
dccl::logger::WARN_PLUS, &std::cerr);
74 expect_same<TestMsg, TestMsgNoHashableChanges>();
75 expect_different<TestMsg, TestMsgNewID>();
76 expect_different<TestMsg, TestMsgNewEnum>();
77 expect_different<TestMsg, TestMsgNewBounds>();
78 expect_different<TestMsgV2, TestMsgV3>();
79 expect_different<TestMsgV3, TestMsgV4>();
80 expect_different<TestMsgV4, TestMsgV5>();
83 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"TestMsg desc: " << TestMsg::descriptor() << std::endl;
85 auto hash = codec.load<TestMsg>();
86 codec.info<TestMsg>();
88 TestMsg msg_in, msg_out;
89 msg_in.set_e(TestMsg::VALUE1);
90 msg_in.set_hash_req(0x1234);
92 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Message in:\n" << msg_in.DebugString() << std::endl;
93 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Try encode..." << std::endl;
95 codec.encode(&bytes, msg_in);
96 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"... got bytes (hex): " <<
dccl::hex_encode(bytes) << std::endl;
98 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Try decode..." << std::endl;
99 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog << codec.max_size(msg_in.GetDescriptor()) << std::endl;
101 codec.decode(bytes, &msg_out);
103 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"... got Message out:\n" << msg_out.DebugString() << std::endl;
105 msg_in.set_hash_opt(hash & 0xFFFF);
106 msg_in.set_hash_req(hash & 0xFFFFFFFF);
107 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog << hash << std::endl;
108 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Message in (with hash):\n" << msg_in.DebugString() << std::endl;
110 assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
111 codec.unload<TestMsg>();
115 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"TestMsg desc: " << TestMsg::descriptor() << std::endl;
117 codec.load<TestMsg>();
120 TestMsgNewEnum msg_out;
122 msg_in.set_e(TestMsg::VALUE1);
123 msg_in.set_hash_req(0x1234);
125 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Message in:\n" << msg_in.DebugString() << std::endl;
126 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Try encode..." << std::endl;
128 codec.encode(&bytes, msg_in);
129 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"... got bytes (hex): " <<
dccl::hex_encode(bytes) << std::endl;
131 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Try decode..." << std::endl;
132 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog << codec.max_size(msg_in.GetDescriptor()) << std::endl;
134 codec.unload<TestMsg>();
135 codec.load<TestMsgNewEnum>();
139 codec.decode(bytes, &msg_out);
142 catch (
const std::exception& e)
145 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Caught expected exception: " << e.what() << std::endl;
150 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"TestMsgMultiHash desc: " << TestMsgMultiHash::descriptor() << std::endl;
152 auto hash = codec.load<TestMsgMultiHash>();
153 codec.info<TestMsgMultiHash>();
155 TestMsgMultiHash msg_in, msg_out;
157 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Message in:\n" << msg_in.DebugString() << std::endl;
158 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Try encode..." << std::endl;
160 codec.encode(&bytes, msg_in);
161 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"... got bytes (hex): " <<
dccl::hex_encode(bytes) << std::endl;
163 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Try decode..." << std::endl;
164 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog << codec.max_size(msg_in.GetDescriptor()) << std::endl;
166 codec.decode(bytes, &msg_out);
168 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"... got Message out:\n" << msg_out.DebugString() << std::endl;
170 msg_in.set_hash4(hash & ((1 << 4) - 1));
171 msg_in.set_hash6(hash & ((1 << 6) - 1));
172 msg_in.set_hash8(hash & ((1 << 8) - 1));
173 msg_in.set_hash13(hash & ((1 << 13) - 1));
174 msg_in.set_hash26(hash & ((1 << 26) - 1));
175 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog << hash << std::endl;
176 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Message in (with hash):\n" << msg_in.DebugString() << std::endl;
178 assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
179 codec.unload<TestMsgMultiHash>();
184 codec.load<TestMsgHashMaxTooLarge>();
187 catch (
const std::exception& e)
190 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Caught expected exception: " << e.what() << std::endl;
193 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"All 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...