26 #include "../../binary.h"
27 #include "../../codec.h"
34 int main(
int ,
char* [])
36 dccl::dlog.
connect(dccl::logger::ALL, &std::cerr);
45 msg_in1.set_int32_val(1);
46 msg_in2.set_bool_val(
false);
47 msg_in3.set_string_val(
"string1");
48 msg_in4.set_string_val(
"string2");
50 std::list<const google::protobuf::Message*> msgs;
51 msgs.push_back(&msg_in1);
52 msgs.push_back(&msg_in2);
53 msgs.push_back(&msg_in3);
54 msgs.push_back(&msg_in4);
56 std::list<const google::protobuf::Descriptor*> descs;
57 descs.push_back(msg_in1.GetDescriptor());
58 descs.push_back(msg_in2.GetDescriptor());
59 descs.push_back(msg_in3.GetDescriptor());
60 descs.push_back(msg_in4.GetDescriptor());
62 for (
auto desc : descs)
64 codec.info(desc, &std::cout);
72 std::cout <<
"Message " << ++i <<
" in:\n" << msg->DebugString() << std::endl;
73 std::cout <<
"Try encode..." << std::endl;
74 codec.encode(&bytes1, *msg);
76 bytes1 += std::string(4,
'\0');
78 std::cout <<
"... got bytes (hex): " <<
dccl::hex_encode(bytes1) << std::endl;
79 std::cout <<
"Try decode..." << std::endl;
83 std::list<std::shared_ptr<google::protobuf::Message>> msgs_out;
86 std::string::iterator begin = bytes1.begin(), end = bytes1.end();
89 auto it = codec.loaded().find(codec.id(begin, end));
90 if (it == codec.loaded().end())
93 std::shared_ptr<google::protobuf::Message> msg =
95 begin = codec.decode(begin, end, msg.get());
96 msgs_out.push_back(msg);
101 std::cout << e.what() << std::endl;
104 std::list<const google::protobuf::Message*>::const_iterator in_it = msgs.begin();
106 assert(msgs.size() == msgs_out.size());
108 for (
const auto& it : msgs_out)
111 std::cout <<
"... got Message " << ++i <<
" out:\n" << it->DebugString() << std::endl;
112 assert((*in_it)->SerializeAsString() == it->SerializeAsString());
119 std::list<std::shared_ptr<google::protobuf::Message>> msgs_out;
122 while (!bytes1.empty())
124 codec.decode<std::shared_ptr<google::protobuf::Message>>(&bytes1));
128 std::cout << e.what() << std::endl;
131 std::list<const google::protobuf::Message*>::const_iterator in_it = msgs.begin();
133 assert(msgs.size() == msgs_out.size());
135 for (
const auto& it : msgs_out)
138 std::cout <<
"... got Message " << ++i <<
" out:\n" << it->DebugString() << std::endl;
139 assert((*in_it)->SerializeAsString() == it->SerializeAsString());
144 std::cout <<
"all tests passed" << std::endl;