29#include <google/protobuf/descriptor.pb.h>
31#include "../../binary.h"
32#include "../../codec.h"
36void decode_check(
const std::string& encoded);
40int main(
int argc,
char* argv[])
43 for (
int i = 1; i < argc; ++i)
45 if (argv[i] && argv[i][0] ==
'-' && argv[i][1] ==
'v' && argv[i][2] ==
'\0')
49 dccl::dlog.
connect(verbose ? dccl::logger::ALL :
dccl::logger::WARN_PLUS, &std::cerr);
52 msg_in.set_double_default_optional(++i + 0.1);
53 msg_in.set_float_default_optional(++i + 0.2);
55 msg_in.set_int32_default_optional(++i);
56 msg_in.set_int64_default_optional(-++i);
57 msg_in.set_uint32_default_optional(++i);
58 msg_in.set_uint64_default_optional(++i);
59 msg_in.set_sint32_default_optional(-++i);
60 msg_in.set_sint64_default_optional(++i);
61 msg_in.set_fixed32_default_optional(++i);
62 msg_in.set_fixed64_default_optional(++i);
63 msg_in.set_sfixed32_default_optional(++i);
64 msg_in.set_sfixed64_default_optional(-++i);
66 msg_in.set_bool_default_optional(
true);
68 msg_in.set_string_default_optional(
"abc123");
71 msg_in.set_enum_default_optional(ENUM_C);
72 msg_in.mutable_msg_default_optional()->set_val(++i + 0.3);
73 msg_in.mutable_msg_default_optional()->mutable_msg()->set_val(++i);
75 msg_in.set_double_default_required(++i + 0.1);
76 msg_in.set_float_default_required(++i + 0.2);
78 msg_in.set_int32_default_required(++i);
79 msg_in.set_int64_default_required(-++i);
80 msg_in.set_uint32_default_required(++i);
81 msg_in.set_uint64_default_required(++i);
82 msg_in.set_sint32_default_required(-++i);
83 msg_in.set_sint64_default_required(++i);
84 msg_in.set_fixed32_default_required(++i);
85 msg_in.set_fixed64_default_required(++i);
86 msg_in.set_sfixed32_default_required(++i);
87 msg_in.set_sfixed64_default_required(-++i);
89 msg_in.set_bool_default_required(
true);
91 msg_in.set_string_default_required(
"abc123");
94 msg_in.set_enum_default_required(ENUM_C);
95 msg_in.mutable_msg_default_required()->set_val(++i + 0.3);
96 msg_in.mutable_msg_default_required()->mutable_msg()->set_val(++i);
98 for (
int j = 0; j < 2; ++j)
100 msg_in.add_double_default_repeat(++i + 0.1);
101 msg_in.add_float_default_repeat(++i + 0.2);
103 msg_in.add_int32_default_repeat(++i);
104 msg_in.add_int64_default_repeat(-++i);
105 msg_in.add_uint32_default_repeat(++i);
106 msg_in.add_uint64_default_repeat(++i);
107 msg_in.add_sint32_default_repeat(-++i);
108 msg_in.add_sint64_default_repeat(++i);
109 msg_in.add_fixed32_default_repeat(++i);
110 msg_in.add_fixed64_default_repeat(++i);
111 msg_in.add_sfixed32_default_repeat(++i);
112 msg_in.add_sfixed64_default_repeat(-++i);
114 msg_in.add_bool_default_repeat(
true);
116 msg_in.add_string_default_repeat(
"abc123");
123 msg_in.add_enum_default_repeat(
static_cast<Enum1
>((++i % 3) + 1));
124 EmbeddedMsg1* em_msg = msg_in.add_msg_default_repeat();
125 em_msg->set_val(++i + 0.3);
126 em_msg->mutable_msg()->set_val(++i);
129 codec.info(msg_in.GetDescriptor());
131 std::ofstream fout(
"/tmp/testmessage.pb");
132 msg_in.SerializeToOstream(&fout);
134 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Message in:\n" << msg_in.DebugString() << std::endl;
136 codec.load(msg_in.GetDescriptor());
138 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Try encode..." << std::endl;
140 codec.encode(&bytes, msg_in);
141 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"... got bytes (hex): " <<
dccl::hex_encode(bytes) << std::endl;
143 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Try decode..." << std::endl;
150 dccl::hex_decode(
"047f277b16b95660c0b0188000d8c0132858800008005b98d8588ccccc0488109951dd659"
151 "6a079fd8c7905d01a29015a99800738012eb880010ee0052d4c6c2c466626012244665477"
152 "9925682c55335e8158e310b1127e1d4858fab84454a3104ca2983214c4e7a22ad164aae96"
153 "41373bd69314c6c2c8661626331e2bf7bb70401547799a5ea29b51942f5b7a91600"));
156 std::string random = bytes;
157 for (
unsigned i = 0; i < 10; ++i)
159 random[(rand() % (bytes.size() - 1) + 1)] = rand() % 256;
160 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Using junk bytes: " <<
dccl::hex_encode(random) << std::endl;
165 codec.decode(random, &msg_out);
177 codec.encode(&bytes, empty);
182 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Expected verbose error encoding empty message: " << e.what() << std::endl;
185 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"all tests passed" << std::endl;
188void decode_check(
const std::string& encoded)
191 codec.decode(encoded, &msg_out);
193 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"... got Message out:\n" << msg_out.DebugString() << std::endl;
196 msg_in.set_string_default_repeat(0,
"abc1");
197 msg_in.set_string_default_repeat(1,
"abc1");
199 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...