28 #include <google/protobuf/descriptor.pb.h>
30 #include "../../binary.h"
31 #include "../../codec.h"
33 #if CODEC_VERSION == 3
35 #elif CODEC_VERSION == 4
44 void decode_check(
const std::string& encoded);
48 #if CODEC_VERSION == 4
52 int main(
int ,
char* [])
54 dccl::dlog.
connect(dccl::logger::ALL, &std::cerr);
58 #if CODEC_VERSION == 4
62 std::cout <<
"all tests passed" << std::endl;
67 msg_in.set_state(TestMsg::STATE_1);
71 msg_in.set_c_center(50);
84 auto c = msg_in.add_child();
85 c->set_include_i(TestMsg::Child::NO);
90 auto c = msg_in.add_child();
91 c->set_include_i(TestMsg::Child::YES);
96 auto c = msg_in.add_child();
97 c->set_include_i(TestMsg::Child::NO);
103 auto c = msg_in.mutable_child2();
104 c->set_include_i(TestMsg::Child2::NO);
108 auto c = msg_in.mutable_child3();
109 c->set_include_i(TestMsg::Child3::YES);
112 auto sc = c->mutable_subchild();
113 sc->set_include_i(TestMsg::Child2::YES);
117 codec.info(msg_in.GetDescriptor());
119 std::cout <<
"Message in:\n" << msg_in.DebugString() << std::endl;
121 codec.load(msg_in.GetDescriptor());
123 std::cout <<
"Try encode..." << std::endl;
125 codec.encode(&bytes, msg_in);
126 std::cout <<
"... got bytes (hex): " <<
dccl::hex_encode(bytes) << std::endl;
128 std::cout <<
"Try decode..." << std::endl;
134 msg_in.mutable_d()->erase(msg_in.mutable_d()->begin() + 3);
137 msg_in.mutable_child(0)->clear_i();
138 msg_in.mutable_child(0)->clear_i2();
141 msg_in.mutable_child(2)->clear_i();
142 msg_in.mutable_child(2)->clear_i2();
144 msg_in.mutable_child2()->clear_i();
151 msg_in.set_state(TestMsg::STATE_2);
163 auto c = msg_in.add_child();
164 c->set_include_i(TestMsg::Child::YES);
169 auto c = msg_in.add_child();
170 c->set_include_i(TestMsg::Child::NO);
173 auto c = msg_in.mutable_child2();
174 c->set_include_i(TestMsg::Child2::YES);
178 codec.info(msg_in.GetDescriptor());
180 std::cout <<
"Message in:\n" << msg_in.DebugString() << std::endl;
182 codec.load(msg_in.GetDescriptor());
184 std::cout <<
"Try encode..." << std::endl;
186 codec.encode(&bytes, msg_in);
187 std::cout <<
"... got bytes (hex): " <<
dccl::hex_encode(bytes) << std::endl;
189 std::cout <<
"Try decode..." << std::endl;
191 msg_in.mutable_d()->erase(msg_in.mutable_d()->begin() + 3);
198 msg_in.set_state(TestMsg::STATE_2);
202 auto c = msg_in.add_child();
203 c->set_include_i(TestMsg::Child::YES);
208 auto c = msg_in.add_child();
209 c->set_include_i(TestMsg::Child::NO);
212 auto c = msg_in.mutable_child2();
213 c->set_include_i(TestMsg::Child2::NO);
216 codec.info(msg_in.GetDescriptor());
218 std::cout <<
"Message in:\n" << msg_in.DebugString() << std::endl;
220 codec.load(msg_in.GetDescriptor());
222 std::cout <<
"Try encode..." << std::endl;
223 std::cout <<
"Min Size: " << codec.min_size(msg_in.GetDescriptor()) << std::endl;
224 std::cout <<
"Max Size: " << codec.max_size(msg_in.GetDescriptor()) << std::endl;
225 std::vector<char> bytes(codec.size(msg_in), 0);
227 int s = codec.size(msg_in);
228 std::cout <<
"Size: " << s << std::endl;
229 std::cout <<
"Bytes Size: " << bytes.size() << std::endl;
231 codec.encode(bytes.data(), bytes.size(), msg_in);
232 std::cout <<
"... got bytes (hex): "
235 std::cout <<
"Try decode..." << std::endl;
237 decode_check(std::string(bytes.begin(), bytes.end()));
240 #if CODEC_VERSION == 4
243 msg_in.set_state(TestMsg::STATE_1);
245 msg_in.set_c_center(100);
250 auto c = msg_in.mutable_child2();
251 c->set_include_i(TestMsg::Child2::NO);
254 codec.info(msg_in.GetDescriptor());
256 std::cout <<
"Message in:\n" << msg_in.DebugString() << std::endl;
258 codec.load(msg_in.GetDescriptor());
260 std::cout <<
"Try encode..." << std::endl;
261 std::cout <<
"Min Size: " << codec.min_size(msg_in.GetDescriptor()) << std::endl;
262 std::cout <<
"Max Size: " << codec.max_size(msg_in.GetDescriptor()) << std::endl;
263 std::vector<char> bytes(codec.size(msg_in), 0);
265 int s = codec.size(msg_in);
266 std::cout <<
"Size: " << s << std::endl;
267 std::cout <<
"Bytes Size: " << bytes.size() << std::endl;
269 codec.encode(bytes.data(), bytes.size(), msg_in);
270 std::cout <<
"... got bytes (hex): "
273 std::cout <<
"Try decode..." << std::endl;
277 decode_check(std::string(bytes.begin(), bytes.end()));
281 void decode_check(
const std::string& encoded)
284 codec.decode(encoded, &msg_out);
286 std::cout <<
"... got Message out:\n" << msg_out.DebugString() << std::endl;
288 assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());