27 #include <google/protobuf/descriptor.pb.h>
29 #include "../../binary.h"
30 #include "../../codec.h"
34 void decode_check(
const TestMsg& msg_in);
37 int main(
int ,
char* [])
39 dccl::dlog.
connect(dccl::logger::ALL, &std::cerr);
41 codec.info<TestMsg>();
42 codec.load<TestMsg>();
45 codec.set_strict(
true);
52 msg_in.set_b(std::string(9,
'1'));
67 msg_in.set_b(std::string(9,
'1'));
70 bool expected_out_of_range_exception =
false;
71 assert(expected_out_of_range_exception);
75 assert(e.field() == TestMsg::descriptor()->FindFieldByName(
"d"));
76 std::cout <<
"Caught (as expected) " << e.what() << std::endl;
86 msg_in.set_b(std::string(9,
'1'));
89 bool expected_out_of_range_exception =
false;
90 assert(expected_out_of_range_exception);
94 assert(e.field() == TestMsg::descriptor()->FindFieldByName(
"i"));
95 std::cout <<
"Caught (as expected) " << e.what() << std::endl;
104 msg_in.set_s2(
"foobar1234546789");
105 msg_in.set_b(std::string(9,
'1'));
107 decode_check(msg_in);
108 bool expected_out_of_range_exception =
false;
109 assert(expected_out_of_range_exception);
113 assert(e.field() == TestMsg::descriptor()->FindFieldByName(
"s2"));
114 std::cout <<
"Caught (as expected) " << e.what() << std::endl;
123 msg_in.set_s(
"foobar1234546789");
124 msg_in.set_b(std::string(9,
'1'));
126 decode_check(msg_in);
127 bool expected_out_of_range_exception =
false;
128 assert(expected_out_of_range_exception);
132 assert(e.field() == TestMsg::descriptor()->FindFieldByName(
"s"));
133 std::cout <<
"Caught (as expected) " << e.what() << std::endl;
143 msg_in.set_b(std::string(12,
'1'));
145 decode_check(msg_in);
146 bool expected_out_of_range_exception =
false;
147 assert(expected_out_of_range_exception);
151 assert(e.field() == TestMsg::descriptor()->FindFieldByName(
"b"));
152 std::cout <<
"Caught (as expected) " << e.what() << std::endl;
162 msg_in.set_vb(std::string(12,
'1'));
164 decode_check(msg_in);
165 bool expected_out_of_range_exception =
false;
166 assert(expected_out_of_range_exception);
170 assert(e.field() == TestMsg::descriptor()->FindFieldByName(
"vb"));
171 std::cout <<
"Caught (as expected) " << e.what() << std::endl;
181 msg_in.set_b(std::string(9,
'1'));
187 decode_check(msg_in);
191 assert(e.field() == TestMsg::descriptor()->FindFieldByName(
"ri"));
192 std::cout <<
"Caught (as expected) " << e.what() << std::endl;
196 codec.set_strict(
false);
203 msg_in.set_b(std::string(9,
'1'));
208 decode_check(msg_in);
217 msg_in.set_b(std::string(9,
'1'));
219 decode_check(msg_in);
228 msg_in.set_b(std::string(9,
'1'));
230 decode_check(msg_in);
238 msg_in.set_s2(
"foobar1234546789");
239 msg_in.set_b(std::string(9,
'1'));
241 decode_check(msg_in);
249 msg_in.set_s(
"foobar1234546789");
250 msg_in.set_b(std::string(9,
'1'));
252 decode_check(msg_in);
261 msg_in.set_b(std::string(12,
'1'));
263 decode_check(msg_in);
272 msg_in.set_vb(std::string(12,
'1'));
274 decode_check(msg_in);
283 msg_in.set_b(std::string(9,
'1'));
289 decode_check(msg_in);
292 std::cout <<
"all tests passed" << std::endl;
295 void decode_check(
const TestMsg& msg_in)
297 std::cout <<
"Message in:\n" << msg_in.DebugString() << std::endl;
299 std::cout <<
"Try encode (in bounds)..." << std::endl;
301 codec.encode(&bytes, msg_in);
302 std::cout <<
"... got bytes (hex): " <<
dccl::hex_encode(bytes) << std::endl;
304 std::cout <<
"Try decode..." << std::endl;
307 codec.decode(bytes, &msg_out);
309 std::cout <<
"... got Message out:\n" << msg_out.DebugString() << std::endl;