29#include "../../binary.h"
30#include "../../codec.h"
34void decode_check(
const std::string& encoded);
38int main(
int argc,
char* argv[])
41 for (
int i = 1; i < argc; ++i)
43 if (argv[i] && argv[i][0] ==
'-' && argv[i][1] ==
'v' && argv[i][2] ==
'\0')
47 dccl::dlog.
connect(verbose ? dccl::logger::ALL :
dccl::logger::WARN_PLUS, &std::cerr);
50 msg_in.set_double_default_optional(++i + 0.1);
51 msg_in.set_float_default_optional(++i + 0.2);
53 msg_in.set_int32_default_optional(++i);
54 msg_in.set_int64_default_optional(-++i);
55 msg_in.set_uint32_default_optional(++i);
56 msg_in.set_uint64_default_optional(++i);
57 msg_in.set_sint32_default_optional(-++i);
58 msg_in.set_sint64_default_optional(++i);
59 msg_in.set_fixed32_default_optional(++i);
60 msg_in.set_fixed64_default_optional(++i);
61 msg_in.set_sfixed32_default_optional(++i);
62 msg_in.set_sfixed64_default_optional(-++i);
64 msg_in.set_bool_default_optional(
true);
66 msg_in.set_string_default_optional(
"abc123");
69 msg_in.set_enum_default_optional(ENUM_C);
70 msg_in.mutable_msg_default_optional()->set_val(++i + 0.3);
71 msg_in.mutable_msg_default_optional()->mutable_msg()->set_val(++i);
73 msg_in.set_double_default_required(++i + 0.1);
74 msg_in.set_float_default_required(++i + 0.2);
76 msg_in.set_int32_default_required(++i);
77 msg_in.set_int64_default_required(-++i);
78 msg_in.set_uint32_default_required(++i);
79 msg_in.set_uint64_default_required(++i);
80 msg_in.set_sint32_default_required(-++i);
81 msg_in.set_sint64_default_required(++i);
82 msg_in.set_fixed32_default_required(++i);
83 msg_in.set_fixed64_default_required(++i);
84 msg_in.set_sfixed32_default_required(++i);
85 msg_in.set_sfixed64_default_required(-++i);
87 msg_in.set_bool_default_required(
true);
89 msg_in.set_string_default_required(
"abc123");
92 msg_in.set_enum_default_required(ENUM_C);
93 msg_in.mutable_msg_default_required()->set_val(++i + 0.3);
94 msg_in.mutable_msg_default_required()->mutable_msg()->set_val(++i);
96 for (
int j = 0; j < 2; ++j)
98 msg_in.add_double_default_repeat(++i + 0.1);
99 msg_in.add_float_default_repeat(++i + 0.2);
101 msg_in.add_int32_default_repeat(++i);
102 msg_in.add_int64_default_repeat(-++i);
103 msg_in.add_uint32_default_repeat(++i);
104 msg_in.add_uint64_default_repeat(++i);
105 msg_in.add_sint32_default_repeat(-++i);
106 msg_in.add_sint64_default_repeat(++i);
107 msg_in.add_fixed32_default_repeat(++i);
108 msg_in.add_fixed64_default_repeat(++i);
109 msg_in.add_sfixed32_default_repeat(++i);
110 msg_in.add_sfixed64_default_repeat(-++i);
112 msg_in.add_bool_default_repeat(
true);
114 msg_in.add_string_default_repeat(
"abc123");
121 msg_in.add_enum_default_repeat(
static_cast<Enum1
>((++i % 3) + 1));
122 EmbeddedMsg1* em_msg = msg_in.add_msg_default_repeat();
123 em_msg->set_val(++i + 0.3);
124 em_msg->mutable_msg()->set_val(++i);
127 codec.info(msg_in.GetDescriptor());
129 std::ofstream fout(
"/tmp/testmessage.pb");
130 msg_in.SerializeToOstream(&fout);
132 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Message in:\n" << msg_in.DebugString() << std::endl;
134 codec.load(msg_in.GetDescriptor());
136 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Try encode..." << std::endl;
138 codec.encode(&bytes, msg_in);
139 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"... got bytes (hex): " <<
dccl::hex_encode(bytes) << std::endl;
141 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Try decode..." << std::endl;
148 dccl::hex_decode(
"047f277b16b95660c0b0188000d8c0132858800008002d4c6c2c4666264084c88cea2eb30"
149 "4cdeb67cc2b00b4464a805626e0014e800b2e60800378410b131b8b91990188109951dd65"
150 "96a0b154cd7805106b1c2256c2af03094b1f97886a14824914538682f85c54259a4c359d6"
151 "c62ae372d86898dc5304c6c2c46ffeedd1200aabbcc52f594da0c01d5dfa65a00"));
154 std::string random = bytes;
155 for (
unsigned i = 0; i < 10; ++i)
157 random[(rand() % (bytes.size() - 1) + 1)] = rand() % 256;
158 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Using junk bytes: " <<
dccl::hex_encode(random) << std::endl;
163 codec.decode(random, &msg_out);
170 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"all tests passed" << std::endl;
173void decode_check(
const std::string& encoded)
176 codec.decode(encoded, &msg_out);
178 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"... got Message out:\n" << msg_out.DebugString() << std::endl;
181 msg_in.set_string_default_repeat(0,
"abc1");
182 msg_in.set_string_default_repeat(1,
"abc1");
184 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 ...
Exception class for 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...
void hex_decode(const std::string &in, std::string *out)
Decodes a (little-endian) hexadecimal string to a byte string. Index 0 and 1 (first byte) of in are w...