28 #include <google/protobuf/descriptor.pb.h>
30 #include "../../codec.h"
31 #include "../../codecs2/field_codec_default.h"
33 #include "../../binary.h"
38 int main(
int ,
char* [])
44 codec.info<TestMsg>(&std::cout);
45 codec.load<TestMsg>();
48 TestMsg msg_in, msg_out;
50 std::cout <<
"Message in:\n" << msg_in.DebugString() << std::endl;
52 codec.load(msg_in.GetDescriptor());
54 std::cout <<
"Try encode..." << std::endl;
56 codec.encode(&bytes, msg_in);
57 std::cout <<
"... got bytes (hex): " <<
dccl::hex_encode(bytes) << std::endl;
59 std::cout <<
"Try decode..." << std::endl;
61 codec.decode(bytes, &msg_out);
63 std::cout <<
"... got Message out:\n" << msg_out.DebugString() << std::endl;
65 assert(!msg_out.has_msg1());
66 assert(!msg_out.msg1_repeat_size());
67 assert(!msg_out.has_msg2());
68 assert(!msg_out.msg2_repeat_size());
69 assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
74 TestMsg msg_in, msg_out;
76 msg_in.mutable_msg1()->set_val(0.1);
77 msg_in.add_msg1_repeat()->set_val(0.11);
78 msg_in.add_msg1_repeat()->set_val(0.12);
79 msg_in.add_msg1_repeat()->set_val(0.13);
80 msg_in.mutable_msg2()->set_val(0.2);
81 msg_in.add_msg2_repeat()->set_val(0.21);
82 msg_in.add_msg2_repeat()->set_val(0.22);
83 msg_in.add_msg2_repeat()->set_val(0.23);
85 std::cout <<
"Message in:\n" << msg_in.DebugString() << std::endl;
87 std::cout <<
"Try encode..." << std::endl;
89 codec.encode(&bytes, msg_in);
90 std::cout <<
"... got bytes (hex): " <<
dccl::hex_encode(bytes) << std::endl;
92 std::cout <<
"Try decode..." << std::endl;
94 codec.decode(bytes, &msg_out);
96 std::cout <<
"... got Message out:\n" << msg_out.DebugString() << std::endl;
98 assert(msg_out.has_msg1());
99 assert(msg_out.msg1_repeat_size() == 3);
100 assert(msg_out.has_msg2());
101 assert(msg_out.msg2_repeat_size() == 3);
102 assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
107 TestMsg msg_in, msg_out;
109 msg_in.mutable_msg1();
110 msg_in.mutable_msg2()->set_val(1);
111 std::cout <<
"Message in:\n" << msg_in.DebugString() << std::endl;
113 std::cout <<
"Try encode..." << std::endl;
115 codec.encode(&bytes, msg_in);
116 std::cout <<
"... got bytes (hex): " <<
dccl::hex_encode(bytes) << std::endl;
118 std::cout <<
"Try decode..." << std::endl;
120 codec.decode(bytes, &msg_out);
122 std::cout <<
"... got Message out:\n" << msg_out.DebugString() << std::endl;
124 assert(msg_out.has_msg1());
125 assert(msg_out.has_msg2());
126 assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
129 std::cout <<
"all tests passed" << std::endl;