29 #include "../../native_protobuf/dccl_native_protobuf.h"
30 #include <google/protobuf/descriptor.pb.h>
32 #include "../../binary.h"
33 #include "../../codec.h"
39 template <
typename Msg1,
typename Msg2>
41 std::pair<std::size_t, std::size_t> compute_hashes()
43 std::size_t hash1 = codec.load<Msg1>();
45 std::cout << Msg1::descriptor()->full_name() <<
": " << dccl::hash_as_string(hash1)
47 std::size_t hash2 = codec.load<Msg2>();
49 std::cout << Msg2::descriptor()->full_name() <<
": " << dccl::hash_as_string(hash2)
51 return std::make_pair(hash1, hash2);
54 template <
typename Msg1,
typename Msg2>
void expect_same()
56 auto hashes = compute_hashes<Msg1, Msg2>();
57 assert(hashes.first == hashes.second);
60 template <
typename Msg1,
typename Msg2>
void expect_different()
62 auto hashes = compute_hashes<Msg1, Msg2>();
63 assert(hashes.first != hashes.second);
66 int main(
int ,
char* [])
68 dccl::dlog.
connect(dccl::logger::ALL, &std::cerr);
70 expect_same<TestMsg, TestMsgNoHashableChanges>();
71 expect_different<TestMsg, TestMsgNewID>();
72 expect_different<TestMsg, TestMsgNewEnum>();
73 expect_different<TestMsg, TestMsgNewBounds>();
74 expect_different<TestMsgV2, TestMsgV3>();
75 expect_different<TestMsgV3, TestMsgV4>();
78 std::cout <<
"TestMsg desc: " << TestMsg::descriptor() << std::endl;
80 auto hash = codec.load<TestMsg>();
81 codec.info<TestMsg>();
83 TestMsg msg_in, msg_out;
84 msg_in.set_e(TestMsg::VALUE1);
85 msg_in.set_hash_req(0x1234);
87 std::cout <<
"Message in:\n" << msg_in.DebugString() << std::endl;
88 std::cout <<
"Try encode..." << std::endl;
90 codec.encode(&bytes, msg_in);
91 std::cout <<
"... got bytes (hex): " <<
dccl::hex_encode(bytes) << std::endl;
93 std::cout <<
"Try decode..." << std::endl;
94 std::cout << codec.max_size(msg_in.GetDescriptor()) << std::endl;
96 codec.decode(bytes, &msg_out);
98 std::cout <<
"... got Message out:\n" << msg_out.DebugString() << std::endl;
100 msg_in.set_hash_opt(hash & 0xFFFF);
101 msg_in.set_hash_req(hash & 0xFFFFFFFF);
102 std::cout << hash << std::endl;
103 std::cout <<
"Message in (with hash):\n" << msg_in.DebugString() << std::endl;
105 assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
106 codec.unload<TestMsg>();
110 std::cout <<
"TestMsg desc: " << TestMsg::descriptor() << std::endl;
112 codec.load<TestMsg>();
115 TestMsgNewEnum msg_out;
117 msg_in.set_e(TestMsg::VALUE1);
118 msg_in.set_hash_req(0x1234);
120 std::cout <<
"Message in:\n" << msg_in.DebugString() << std::endl;
121 std::cout <<
"Try encode..." << std::endl;
123 codec.encode(&bytes, msg_in);
124 std::cout <<
"... got bytes (hex): " <<
dccl::hex_encode(bytes) << std::endl;
126 std::cout <<
"Try decode..." << std::endl;
127 std::cout << codec.max_size(msg_in.GetDescriptor()) << std::endl;
129 codec.unload<TestMsg>();
130 codec.load<TestMsgNewEnum>();
134 codec.decode(bytes, &msg_out);
137 catch (
const std::exception& e)
140 std::cout <<
"Caught expected exception: " << e.what() << std::endl;
145 std::cout <<
"TestMsgMultiHash desc: " << TestMsgMultiHash::descriptor() << std::endl;
147 auto hash = codec.load<TestMsgMultiHash>();
148 codec.info<TestMsgMultiHash>();
150 TestMsgMultiHash msg_in, msg_out;
152 std::cout <<
"Message in:\n" << msg_in.DebugString() << std::endl;
153 std::cout <<
"Try encode..." << std::endl;
155 codec.encode(&bytes, msg_in);
156 std::cout <<
"... got bytes (hex): " <<
dccl::hex_encode(bytes) << std::endl;
158 std::cout <<
"Try decode..." << std::endl;
159 std::cout << codec.max_size(msg_in.GetDescriptor()) << std::endl;
161 codec.decode(bytes, &msg_out);
163 std::cout <<
"... got Message out:\n" << msg_out.DebugString() << std::endl;
165 msg_in.set_hash4(hash & ((1 << 4) - 1));
166 msg_in.set_hash6(hash & ((1 << 6) - 1));
167 msg_in.set_hash8(hash & ((1 << 8) - 1));
168 msg_in.set_hash13(hash & ((1 << 13) - 1));
169 msg_in.set_hash26(hash & ((1 << 26) - 1));
170 std::cout << hash << std::endl;
171 std::cout <<
"Message in (with hash):\n" << msg_in.DebugString() << std::endl;
173 assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
174 codec.unload<TestMsgMultiHash>();
179 codec.load<TestMsgHashMaxTooLarge>();
182 catch (
const std::exception& e)
185 std::cout <<
"Caught expected exception: " << e.what() << std::endl;
188 std::cout <<
"All tests passed" << std::endl;
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
void connect(int verbosity_mask, Slot slot)
Connect the output of one or more given verbosities to a slot (function pointer or similar)
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...