27 #include "../../native_protobuf/dccl_native_protobuf.h"
28 #include <google/protobuf/descriptor.pb.h>
30 #include "../../binary.h"
31 #include "../../codec.h"
37 template <
typename Msg1,
typename Msg2>
39 std::pair<std::size_t, std::size_t> compute_hashes()
41 std::size_t hash1 = codec.load<Msg1>();
43 std::cout << Msg1::descriptor()->full_name() <<
": " << dccl::hash_as_string(hash1)
45 std::size_t hash2 = codec.load<Msg2>();
47 std::cout << Msg2::descriptor()->full_name() <<
": " << dccl::hash_as_string(hash2)
49 return std::make_pair(hash1, hash2);
52 template <
typename Msg1,
typename Msg2>
void expect_same()
54 auto hashes = compute_hashes<Msg1, Msg2>();
55 assert(hashes.first == hashes.second);
58 template <
typename Msg1,
typename Msg2>
void expect_different()
60 auto hashes = compute_hashes<Msg1, Msg2>();
61 assert(hashes.first != hashes.second);
64 int main(
int ,
char* [])
66 dccl::dlog.
connect(dccl::logger::ALL, &std::cerr);
68 expect_same<TestMsg, TestMsgNoHashableChanges>();
69 expect_different<TestMsg, TestMsgNewID>();
70 expect_different<TestMsg, TestMsgNewEnum>();
71 expect_different<TestMsg, TestMsgNewBounds>();
72 expect_different<TestMsgV2, TestMsgV3>();
73 expect_different<TestMsgV3, TestMsgV4>();
76 std::cout <<
"TestMsg desc: " << TestMsg::descriptor() << std::endl;
78 auto hash = codec.load<TestMsg>();
79 codec.info<TestMsg>();
81 TestMsg msg_in, msg_out;
82 msg_in.set_e(TestMsg::VALUE1);
83 msg_in.set_hash_req(0x1234);
85 std::cout <<
"Message in:\n" << msg_in.DebugString() << std::endl;
86 std::cout <<
"Try encode..." << std::endl;
88 codec.encode(&bytes, msg_in);
89 std::cout <<
"... got bytes (hex): " <<
dccl::hex_encode(bytes) << std::endl;
91 std::cout <<
"Try decode..." << std::endl;
92 std::cout << codec.max_size(msg_in.GetDescriptor()) << std::endl;
94 codec.decode(bytes, &msg_out);
96 std::cout <<
"... got Message out:\n" << msg_out.DebugString() << std::endl;
98 msg_in.set_hash_opt(hash & 0xFFFF);
99 msg_in.set_hash_req(hash & 0xFFFFFFFF);
100 std::cout << hash << std::endl;
101 std::cout <<
"Message in (with hash):\n" << msg_in.DebugString() << std::endl;
103 assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
104 codec.unload<TestMsg>();
108 std::cout <<
"TestMsg desc: " << TestMsg::descriptor() << std::endl;
110 codec.load<TestMsg>();
113 TestMsgNewEnum msg_out;
115 msg_in.set_e(TestMsg::VALUE1);
116 msg_in.set_hash_req(0x1234);
118 std::cout <<
"Message in:\n" << msg_in.DebugString() << std::endl;
119 std::cout <<
"Try encode..." << std::endl;
121 codec.encode(&bytes, msg_in);
122 std::cout <<
"... got bytes (hex): " <<
dccl::hex_encode(bytes) << std::endl;
124 std::cout <<
"Try decode..." << std::endl;
125 std::cout << codec.max_size(msg_in.GetDescriptor()) << std::endl;
127 codec.unload<TestMsg>();
128 codec.load<TestMsgNewEnum>();
132 codec.decode(bytes, &msg_out);
135 catch (
const std::exception& e)
138 std::cout <<
"Caught expected exception: " << e.what() << std::endl;
143 std::cout <<
"TestMsgMultiHash desc: " << TestMsgMultiHash::descriptor() << std::endl;
145 auto hash = codec.load<TestMsgMultiHash>();
146 codec.info<TestMsgMultiHash>();
148 TestMsgMultiHash msg_in, msg_out;
150 std::cout <<
"Message in:\n" << msg_in.DebugString() << std::endl;
151 std::cout <<
"Try encode..." << std::endl;
153 codec.encode(&bytes, msg_in);
154 std::cout <<
"... got bytes (hex): " <<
dccl::hex_encode(bytes) << std::endl;
156 std::cout <<
"Try decode..." << std::endl;
157 std::cout << codec.max_size(msg_in.GetDescriptor()) << std::endl;
159 codec.decode(bytes, &msg_out);
161 std::cout <<
"... got Message out:\n" << msg_out.DebugString() << std::endl;
163 msg_in.set_hash4(hash & ((1 << 4) - 1));
164 msg_in.set_hash6(hash & ((1 << 6) - 1));
165 msg_in.set_hash8(hash & ((1 << 8) - 1));
166 msg_in.set_hash13(hash & ((1 << 13) - 1));
167 msg_in.set_hash26(hash & ((1 << 26) - 1));
168 std::cout << hash << std::endl;
169 std::cout <<
"Message in (with hash):\n" << msg_in.DebugString() << std::endl;
171 assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
172 codec.unload<TestMsgMultiHash>();
177 codec.load<TestMsgHashMaxTooLarge>();
180 catch (
const std::exception& e)
183 std::cout <<
"Caught expected exception: " << e.what() << std::endl;
186 std::cout <<
"All tests passed" << std::endl;