27#include "../../arithmetic/field_codec_arithmetic.h"
28#include "../../codec.h"
30#include "test_arithmetic.pb.h"
32#include "../../binary.h"
33using namespace dccl::test::arith;
36 const google::protobuf::Message& msg_in,
bool set_model =
true)
44 void* dl_handle = dlopen(DCCL_ARITHMETIC_NAME, RTLD_LAZY);
47 std::cerr <<
"Failed to open " << DCCL_ARITHMETIC_NAME << std::endl;
50 codec.load_library(dl_handle);
55 model.set_name(
"model");
56 dccl::arith::ModelManager::set_model(codec, model);
59 if (dccl::dlog.is(dccl::logger::INFO))
60 codec.info(msg_in.GetDescriptor(), &dccl::dlog);
62 codec.load(msg_in.GetDescriptor());
64 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Message in:\n"
65 << msg_in.DebugString() << std::endl;
67 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Try encode..." << std::endl;
69 codec.encode(&bytes, msg_in);
70 dccl::dlog.
is(dccl::logger::INFO) &&
71 dccl::dlog <<
"... got bytes (hex): " <<
dccl::hex_encode(bytes) << std::endl;
73 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"Try decode..." << std::endl;
75 std::shared_ptr<google::protobuf::Message> msg_out(msg_in.New());
76 codec.decode(bytes, msg_out.get());
78 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"... got Message out:\n"
79 << msg_out->DebugString() << std::endl;
81 assert(msg_in.SerializeAsString() == msg_out->SerializeAsString());
86int main(
int argc,
char* argv[])
89 bool legacy_verbose =
false;
90 for (
int i = 1; i < argc; ++i)
92 if (argv[i] && argv[i][0] ==
'-' && argv[i][1] ==
'v' && argv[i][2] ==
'\0')
94 else if (argv[i] && std::string(argv[i]) ==
"1")
95 legacy_verbose =
true;
99 dccl::dlog.
connect(dccl::logger::ALL, &std::cerr);
100 else if (legacy_verbose)
101 dccl::dlog.
connect(dccl::logger::DEBUG3_PLUS, &std::cerr);
103 dccl::dlog.
connect(dccl::logger::WARN_PLUS, &std::cerr);
111 model.set_eof_frequency(4);
113 model.add_value_bound(0);
114 model.add_frequency(5);
116 model.add_value_bound(1);
117 model.add_frequency(1);
119 model.add_value_bound(2);
121 model.set_out_of_range_frequency(0);
123 ArithmeticDoubleTestMsg msg_in;
130 run_test(model, msg_in);
137 model.add_value_bound(100.0);
138 model.add_frequency(100);
140 model.add_value_bound(100.1);
141 model.add_frequency(100);
143 model.add_value_bound(100.2);
144 model.add_frequency(100);
146 model.add_value_bound(100.3);
147 model.add_frequency(100);
149 model.add_value_bound(100.4);
150 model.add_frequency(90);
152 model.add_value_bound(100.5);
153 model.add_frequency(125);
155 model.add_value_bound(100.6);
156 model.add_frequency(125);
158 model.add_value_bound(100.7);
159 model.add_frequency(125);
161 model.add_value_bound(100.8);
163 model.set_eof_frequency(25);
164 model.set_out_of_range_frequency(10);
166 ArithmeticDoubleTestMsg msg_in;
168 msg_in.add_value(100.5);
169 msg_in.add_value(100.7);
170 msg_in.add_value(100.2);
172 run_test(model, msg_in);
179 model.set_eof_frequency(10);
180 model.set_out_of_range_frequency(0);
182 model.add_value_bound(1);
183 model.add_frequency(2);
185 model.add_value_bound(2);
186 model.add_frequency(3);
188 model.add_value_bound(3);
189 model.add_frequency(85);
191 model.add_value_bound(4);
193 ArithmeticEnumTestMsg msg_in;
195 msg_in.add_value(ENUM_C);
196 msg_in.add_value(ENUM_C);
197 msg_in.add_value(ENUM_C);
198 msg_in.add_value(ENUM_C);
200 run_test(model, msg_in);
207 model.set_eof_frequency(10);
208 model.set_out_of_range_frequency(0);
210 model.add_value_bound(1);
211 model.add_frequency(2);
213 model.add_value_bound(2);
214 model.add_frequency(3);
216 model.add_value_bound(3);
217 model.add_frequency(85);
219 model.add_value_bound(4);
221 ArithmeticEnumTestMsg msg_in;
223 msg_in.add_value(ENUM_A);
224 msg_in.add_value(ENUM_A);
225 msg_in.add_value(ENUM_A);
226 msg_in.add_value(ENUM_A);
228 run_test(model, msg_in);
234 model.set_eof_frequency(10);
235 model.set_out_of_range_frequency(0);
237 model.add_value_bound(1);
238 model.add_frequency(2);
240 model.add_value_bound(2);
241 model.add_frequency(3);
243 model.add_value_bound(3);
244 model.add_frequency(85);
246 model.add_value_bound(4);
248 ArithmeticSingleEnumTestMsg msg_in;
250 msg_in.set_value(ENUM_B);
252 run_test(model, msg_in);
259 model.set_eof_frequency(1);
261 model.add_value_bound(0);
262 model.add_frequency(1);
264 model.add_value_bound(1);
265 model.add_frequency(1);
267 model.add_value_bound(2);
269 model.set_out_of_range_frequency(1);
271 ArithmeticDouble3TestMsg msg_in;
278 model.set_is_adaptive(
true);
279 run_test(model, msg_in);
280 run_test(model, msg_in,
false);
281 run_test(model, msg_in,
false);
282 run_test(model, msg_in,
false);
290 model.set_eof_frequency(0);
291 model.set_out_of_range_frequency(0);
293 model.add_value_bound(1);
294 model.add_frequency(2);
296 model.add_value_bound(2);
297 model.add_frequency(1);
299 model.add_value_bound(3);
300 model.add_frequency(3);
302 model.add_value_bound(4);
303 model.add_frequency(1);
305 model.add_value_bound(5);
306 model.add_frequency(1);
308 model.add_value_bound(6);
310 ArithmeticEnum2TestMsg msg_in;
312 msg_in.add_value(ENUM2_A);
313 msg_in.add_value(ENUM2_B);
314 msg_in.add_value(ENUM2_C);
315 msg_in.add_value(ENUM2_C);
316 msg_in.add_value(ENUM2_E);
317 msg_in.add_value(ENUM2_D);
318 msg_in.add_value(ENUM2_A);
319 msg_in.add_value(ENUM2_C);
321 run_test(model, msg_in);
326 srand(time(
nullptr));
327 for (
unsigned i = 0; i <= ArithmeticDouble2TestMsg::descriptor()
328 ->FindFieldByName(
"value")
330 .GetExtension(dccl::field)
337 dccl::int32 low = -(rand() % std::numeric_limits<dccl::int32>::max());
338 dccl::int32 high = rand() % std::numeric_limits<dccl::int32>::max();
340 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"low: " << low <<
", high: " << high
346 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"symbols: " << symbols << std::endl;
349 dccl::arith::Model::freq_type each_max_freq =
350 dccl::arith::Model::MAX_FREQUENCY / (symbols + 2);
352 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"each_max_freq: " << each_max_freq
355 model.set_eof_frequency(rand() % each_max_freq + 1);
356 model.set_out_of_range_frequency(rand() % each_max_freq + 1);
358 model.add_value_bound(low);
359 model.add_frequency(rand() % each_max_freq + 1);
360 for (
int j = 1; j < symbols; ++j)
364 dccl::int32 remaining_range = high - model.value_bound(j - 1);
365 model.add_value_bound(model.value_bound(j - 1) +
366 rand() % (remaining_range / symbols - j) + 1);
367 model.add_frequency(rand() % each_max_freq + 1);
370 model.add_value_bound(high);
372 ArithmeticDouble2TestMsg msg_in;
374 for (
unsigned j = 0; j < i; ++j) msg_in.add_value(model.value_bound(rand() % symbols));
376 run_test(model, msg_in);
378 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"end random test #" << i << std::endl;
381 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"all tests passed" << std::endl;
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)
google::protobuf::int32 int32
a signed 32 bit integer
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...