27#include "../../arithmetic/field_codec_arithmetic.h"
28#include "../../codec.h"
30#include "test_arithmetic.pb.h"
35 const google::protobuf::Message& msg_in,
bool set_model =
true)
39 model.set_name(
"model");
40 dccl::arith::ModelManager::set_model(codec, model);
43 codec.load(msg_in.GetDescriptor());
44 codec.info(msg_in.GetDescriptor());
50 codec.encode(&bytes, msg_in);
55 std::shared_ptr<google::protobuf::Message> msg_out(msg_in.New());
56 codec.decode(bytes, msg_out.get());
60 assert(msg_in.SerializeAsString() == msg_out->SerializeAsString());
63void decode_check(
dccl::Codec& codec,
const std::string& encoded, TestMsg msg_in);
64void run(
int thread,
int num_iterations);
65int main(
int argc,
char* argv[])
68 for (
int i = 1; i < argc; ++i)
70 if (argv[i] && argv[i][0] ==
'-' && argv[i][1] ==
'v' && argv[i][2] ==
'\0')
74 dccl::dlog.
connect(verbose ? dccl::logger::ALL :
dccl::logger::WARN_PLUS, &std::cerr);
77 std::thread t1([]() { run(1, 100); });
78 std::thread t2([]() { run(2, 100); });
79 std::thread t3([]() { run(3, 100); });
80 std::thread t4([]() { run(4, 100); });
81 std::thread t5([]() { run(5, 100); });
82 std::thread t6([]() { run(6, 100); });
83 std::thread t7([]() { run(7, 100); });
84 std::thread t8([]() { run(8, 100); });
85 std::thread t9([]() { run(9, 100); });
86 std::thread t10([]() { run(10, 100); });
100 std::thread t1([]() { run(1, 10); });
101 std::thread t2([]() { run(2, 10); });
102 std::thread t3([]() { run(3, 10); });
103 std::thread t4([]() { run(4, 10); });
104 std::thread t5([]() { run(5, 10); });
105 std::thread t6([]() { run(6, 10); });
106 std::thread t7([]() { run(7, 10); });
107 std::thread t8([]() { run(8, 10); });
108 std::thread t9([]() { run(9, 10); });
109 std::thread t10([]() { run(10, 10); });
122 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"all tests passed" << std::endl;
125void run(
int thread,
int num_iterations)
128 codec.load_library(DCCL_ARITHMETIC_NAME);
129 codec.load<TestMsg>();
130 for (
int m = 0; m < num_iterations; ++m)
132 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Thread " << thread <<
", it: " << m << std::endl;
133 codec.info<TestMsg>();
136 msg_in.set_double_default_optional(++i + 0.1);
137 msg_in.set_float_default_optional(++i + 0.2);
139 msg_in.set_int32_default_optional(++i);
140 msg_in.set_int64_default_optional(-++i);
141 msg_in.set_uint32_default_optional(++i);
142 msg_in.set_uint64_default_optional(++i);
143 msg_in.set_sint32_default_optional(-++i);
144 msg_in.set_sint64_default_optional(++i);
145 msg_in.set_fixed32_default_optional(++i);
146 msg_in.set_fixed64_default_optional(++i);
147 msg_in.set_sfixed32_default_optional(++i);
148 msg_in.set_sfixed64_default_optional(-++i);
150 msg_in.set_bool_default_optional(
true);
152 msg_in.set_string_default_optional(
"abc123");
155 msg_in.set_enum_default_optional(ENUM_C);
156 msg_in.mutable_msg_default_optional()->set_val(++i + 0.3);
157 msg_in.mutable_msg_default_optional()->mutable_msg()->set_val(++i);
159 msg_in.set_double_default_required(++i + 0.1);
160 msg_in.set_float_default_required(++i + 0.2);
162 msg_in.set_int32_default_required(++i);
163 msg_in.set_int64_default_required(-++i);
164 msg_in.set_uint32_default_required(++i);
165 msg_in.set_uint64_default_required(++i);
166 msg_in.set_sint32_default_required(-++i);
167 msg_in.set_sint64_default_required(++i);
168 msg_in.set_fixed32_default_required(++i);
169 msg_in.set_fixed64_default_required(++i);
170 msg_in.set_sfixed32_default_required(++i);
171 msg_in.set_sfixed64_default_required(-++i);
173 msg_in.set_bool_default_required(
true);
175 msg_in.set_string_default_required(
"abc123");
178 msg_in.set_enum_default_required(ENUM_C);
179 msg_in.mutable_msg_default_required()->set_val(++i + 0.3);
180 msg_in.mutable_msg_default_required()->mutable_msg()->set_val(++i);
182 for (
int j = 0; j < 2; ++j)
184 msg_in.add_double_default_repeat(++i + 0.1);
185 msg_in.add_float_default_repeat(++i + 0.2);
187 msg_in.add_int32_default_repeat(++i);
188 msg_in.add_int64_default_repeat(-++i);
189 msg_in.add_uint32_default_repeat(++i);
190 msg_in.add_uint64_default_repeat(++i);
191 msg_in.add_sint32_default_repeat(-++i);
192 msg_in.add_sint64_default_repeat(++i);
193 msg_in.add_fixed32_default_repeat(++i);
194 msg_in.add_fixed64_default_repeat(++i);
195 msg_in.add_sfixed32_default_repeat(++i);
196 msg_in.add_sfixed64_default_repeat(-++i);
198 msg_in.add_bool_default_repeat(
true);
200 msg_in.add_string_default_repeat(
"abc123");
207 msg_in.add_enum_default_repeat(
static_cast<Enum1
>((++i % 3) + 1));
208 EmbeddedMsg1* em_msg = msg_in.add_msg_default_repeat();
209 em_msg->set_val(++i + 0.3);
210 em_msg->mutable_msg()->set_val(++i);
214 codec.encode(&bytes, msg_in);
216 decode_check(codec, bytes, msg_in);
222 model.set_eof_frequency(4);
224 model.add_value_bound(0);
225 model.add_frequency(5);
227 model.add_value_bound(1);
228 model.add_frequency(1);
230 model.add_value_bound(2);
232 model.set_out_of_range_frequency(0);
234 dccl::test::arith::ArithmeticDoubleTestMsg msg_in;
241 arithmetic_run_test(codec, model, msg_in);
246void decode_check(
dccl::Codec& codec,
const std::string& encoded, TestMsg msg_in)
249 codec.decode(encoded, &msg_out);
254 msg_in.set_string_default_repeat(0,
"abc1");
255 msg_in.set_string_default_repeat(1,
"abc1");
257 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_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...