29#include "../../binary.h"
30#include "../../codec.h"
34#elif CODEC_VERSION == 5
43void decode_check(
const std::string& encoded);
49int main(
int argc,
char* argv[])
52 for (
int i = 1; i < argc; ++i)
54 if (argv[i] && argv[i][0] ==
'-' && argv[i][1] ==
'v' && argv[i][2] ==
'\0')
58 dccl::dlog.
connect(verbose ? dccl::logger::ALL :
dccl::logger::WARN_PLUS, &std::cerr);
64 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"all tests passed" << std::endl;
69 msg_in.set_state(TestMsg::STATE_1);
73 msg_in.set_c_center(50);
86 auto c = msg_in.add_child();
87 c->set_include_i(TestMsg::Child::NO);
92 auto c = msg_in.add_child();
93 c->set_include_i(TestMsg::Child::YES);
98 auto c = msg_in.add_child();
99 c->set_include_i(TestMsg::Child::NO);
105 auto c = msg_in.mutable_child2();
106 c->set_include_i(TestMsg::Child2::NO);
110 auto c = msg_in.mutable_child3();
111 c->set_include_i(TestMsg::Child3::YES);
114 auto sc = c->mutable_subchild();
115 sc->set_include_i(TestMsg::Child2::YES);
119 codec.info(msg_in.GetDescriptor());
121 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Message in:\n" << msg_in.DebugString() << std::endl;
123 codec.load(msg_in.GetDescriptor());
125 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Try encode..." << std::endl;
127 codec.encode(&bytes, msg_in);
128 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"... got bytes (hex): " <<
dccl::hex_encode(bytes) << std::endl;
130 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Try decode..." << std::endl;
136 msg_in.mutable_d()->erase(msg_in.mutable_d()->begin() + 3);
139 msg_in.mutable_child(0)->clear_i();
140 msg_in.mutable_child(0)->clear_i2();
143 msg_in.mutable_child(2)->clear_i();
144 msg_in.mutable_child(2)->clear_i2();
146 msg_in.mutable_child2()->clear_i();
153 msg_in.set_state(TestMsg::STATE_2);
165 auto c = msg_in.add_child();
166 c->set_include_i(TestMsg::Child::YES);
171 auto c = msg_in.add_child();
172 c->set_include_i(TestMsg::Child::NO);
175 auto c = msg_in.mutable_child2();
176 c->set_include_i(TestMsg::Child2::YES);
180 codec.info(msg_in.GetDescriptor());
182 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Message in:\n" << msg_in.DebugString() << std::endl;
184 codec.load(msg_in.GetDescriptor());
186 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Try encode..." << std::endl;
188 codec.encode(&bytes, msg_in);
189 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"... got bytes (hex): " <<
dccl::hex_encode(bytes) << std::endl;
191 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Try decode..." << std::endl;
193 msg_in.mutable_d()->erase(msg_in.mutable_d()->begin() + 3);
200 msg_in.set_state(TestMsg::STATE_2);
204 auto c = msg_in.add_child();
205 c->set_include_i(TestMsg::Child::YES);
210 auto c = msg_in.add_child();
211 c->set_include_i(TestMsg::Child::NO);
214 auto c = msg_in.mutable_child2();
215 c->set_include_i(TestMsg::Child2::NO);
218 codec.info(msg_in.GetDescriptor());
220 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Message in:\n" << msg_in.DebugString() << std::endl;
222 codec.load(msg_in.GetDescriptor());
224 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Try encode..." << std::endl;
225 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Min Size: " << codec.min_size(msg_in.GetDescriptor()) << std::endl;
226 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Max Size: " << codec.max_size(msg_in.GetDescriptor()) << std::endl;
227 std::vector<char> bytes(codec.size(msg_in), 0);
229 int s = codec.size(msg_in);
230 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Size: " << s << std::endl;
231 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Bytes Size: " << bytes.size() << std::endl;
233 codec.encode(bytes.data(), bytes.size(), msg_in);
234 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"... got bytes (hex): "
237 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Try decode..." << std::endl;
239 decode_check(std::string(bytes.begin(), bytes.end()));
244 msg_in.set_state(TestMsg::STATE_1);
246 msg_in.set_c_center(100);
251 auto c = msg_in.mutable_child2();
252 c->set_include_i(TestMsg::Child2::NO);
255 codec.info(msg_in.GetDescriptor());
257 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Message in:\n" << msg_in.DebugString() << std::endl;
259 codec.load(msg_in.GetDescriptor());
261 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Try encode..." << std::endl;
262 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Min Size: " << codec.min_size(msg_in.GetDescriptor()) << std::endl;
263 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Max Size: " << codec.max_size(msg_in.GetDescriptor()) << std::endl;
264 std::vector<char> bytes(codec.size(msg_in), 0);
266 int s = codec.size(msg_in);
267 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Size: " << s << std::endl;
268 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Bytes Size: " << bytes.size() << std::endl;
270 codec.encode(bytes.data(), bytes.size(), msg_in);
271 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"... got bytes (hex): "
274 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Try decode..." << std::endl;
278 decode_check(std::string(bytes.begin(), bytes.end()));
281void decode_check(
const std::string& encoded)
284 codec.decode(encoded, &msg_out);
286 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"... got Message out:\n" << msg_out.DebugString() << std::endl;
288 assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
bool is(logger::Verbosity verbosity, logger::Group group=logger::GENERAL)
Indicates the verbosity of the Logger until the next std::flush or std::endl. The boolean return is u...
void connect(int verbosity_mask, Slot slot)
Connect the output of one or more given verbosities to a slot (function pointer or similar)
Dynamic Compact Control Language namespace.
void hex_encode(CharIterator begin, CharIterator end, std::string *out, bool upper_case=false)
Encodes a (little-endian) hexadecimal string from a byte string. Index 0 of begin is written to index...