27 #include <google/protobuf/descriptor.pb.h>
29 #include "../../arithmetic/field_codec_arithmetic.h"
30 #include "../../codec.h"
32 #include "test_arithmetic.pb.h"
34 #include "../../binary.h"
35 using namespace dccl::test::arith;
47 void* dl_handle = dlopen(DCCL_ARITHMETIC_NAME, RTLD_LAZY);
50 std::cerr <<
"Failed to open " << DCCL_ARITHMETIC_NAME << std::endl;
53 codec.load_library(dl_handle);
58 model.set_name(
"model");
59 dccl::arith::ModelManager::set_model(codec, model);
62 codec.info(msg_in.GetDescriptor(), &std::cout);
64 codec.load(msg_in.GetDescriptor());
66 std::cout <<
"Message in:\n" << msg_in.DebugString() << std::endl;
68 std::cout <<
"Try encode..." << std::endl;
70 codec.encode(&bytes, msg_in);
71 std::cout <<
"... got bytes (hex): " <<
dccl::hex_encode(bytes) << std::endl;
73 std::cout <<
"Try decode..." << std::endl;
75 std::shared_ptr<google::protobuf::Message> msg_out(msg_in.New());
76 codec.decode(bytes, msg_out.get());
78 std::cout <<
"... got Message out:\n" << msg_out->DebugString() << std::endl;
80 assert(msg_in.SerializeAsString() == msg_out->SerializeAsString());
85 int main(
int argc,
char* argv[])
87 if (argc > 1 && std::string(argv[1]) ==
"1")
88 dccl::dlog.
connect(dccl::logger::DEBUG3_PLUS, &std::cerr);
90 dccl::dlog.
connect(dccl::logger::DEBUG2_PLUS, &std::cerr);
98 model.set_eof_frequency(4);
100 model.add_value_bound(0);
101 model.add_frequency(5);
103 model.add_value_bound(1);
104 model.add_frequency(1);
106 model.add_value_bound(2);
108 model.set_out_of_range_frequency(0);
110 ArithmeticDoubleTestMsg msg_in;
117 run_test(model, msg_in);
124 model.add_value_bound(100.0);
125 model.add_frequency(100);
127 model.add_value_bound(100.1);
128 model.add_frequency(100);
130 model.add_value_bound(100.2);
131 model.add_frequency(100);
133 model.add_value_bound(100.3);
134 model.add_frequency(100);
136 model.add_value_bound(100.4);
137 model.add_frequency(90);
139 model.add_value_bound(100.5);
140 model.add_frequency(125);
142 model.add_value_bound(100.6);
143 model.add_frequency(125);
145 model.add_value_bound(100.7);
146 model.add_frequency(125);
148 model.add_value_bound(100.8);
150 model.set_eof_frequency(25);
151 model.set_out_of_range_frequency(10);
153 ArithmeticDoubleTestMsg msg_in;
155 msg_in.add_value(100.5);
156 msg_in.add_value(100.7);
157 msg_in.add_value(100.2);
159 run_test(model, msg_in);
166 model.set_eof_frequency(10);
167 model.set_out_of_range_frequency(0);
169 model.add_value_bound(1);
170 model.add_frequency(2);
172 model.add_value_bound(2);
173 model.add_frequency(3);
175 model.add_value_bound(3);
176 model.add_frequency(85);
178 model.add_value_bound(4);
180 ArithmeticEnumTestMsg msg_in;
182 msg_in.add_value(ENUM_C);
183 msg_in.add_value(ENUM_C);
184 msg_in.add_value(ENUM_C);
185 msg_in.add_value(ENUM_C);
187 run_test(model, msg_in);
194 model.set_eof_frequency(10);
195 model.set_out_of_range_frequency(0);
197 model.add_value_bound(1);
198 model.add_frequency(2);
200 model.add_value_bound(2);
201 model.add_frequency(3);
203 model.add_value_bound(3);
204 model.add_frequency(85);
206 model.add_value_bound(4);
208 ArithmeticEnumTestMsg msg_in;
210 msg_in.add_value(ENUM_A);
211 msg_in.add_value(ENUM_A);
212 msg_in.add_value(ENUM_A);
213 msg_in.add_value(ENUM_A);
215 run_test(model, msg_in);
221 model.set_eof_frequency(10);
222 model.set_out_of_range_frequency(0);
224 model.add_value_bound(1);
225 model.add_frequency(2);
227 model.add_value_bound(2);
228 model.add_frequency(3);
230 model.add_value_bound(3);
231 model.add_frequency(85);
233 model.add_value_bound(4);
235 ArithmeticSingleEnumTestMsg msg_in;
237 msg_in.set_value(ENUM_B);
239 run_test(model, msg_in);
246 model.set_eof_frequency(1);
248 model.add_value_bound(0);
249 model.add_frequency(1);
251 model.add_value_bound(1);
252 model.add_frequency(1);
254 model.add_value_bound(2);
256 model.set_out_of_range_frequency(1);
258 ArithmeticDouble3TestMsg msg_in;
265 model.set_is_adaptive(
true);
266 run_test(model, msg_in);
267 run_test(model, msg_in,
false);
268 run_test(model, msg_in,
false);
269 run_test(model, msg_in,
false);
277 model.set_eof_frequency(0);
278 model.set_out_of_range_frequency(0);
280 model.add_value_bound(1);
281 model.add_frequency(2);
283 model.add_value_bound(2);
284 model.add_frequency(1);
286 model.add_value_bound(3);
287 model.add_frequency(3);
289 model.add_value_bound(4);
290 model.add_frequency(1);
292 model.add_value_bound(5);
293 model.add_frequency(1);
295 model.add_value_bound(6);
297 ArithmeticEnum2TestMsg msg_in;
299 msg_in.add_value(ENUM2_A);
300 msg_in.add_value(ENUM2_B);
301 msg_in.add_value(ENUM2_C);
302 msg_in.add_value(ENUM2_C);
303 msg_in.add_value(ENUM2_E);
304 msg_in.add_value(ENUM2_D);
305 msg_in.add_value(ENUM2_A);
306 msg_in.add_value(ENUM2_C);
308 run_test(model, msg_in);
313 srand(time(
nullptr));
314 for (
unsigned i = 0; i <= ArithmeticDouble2TestMsg::descriptor()
315 ->FindFieldByName(
"value")
317 .GetExtension(dccl::field)
324 dccl::int32 low = -(rand() % std::numeric_limits<dccl::int32>::max());
325 dccl::int32 high = rand() % std::numeric_limits<dccl::int32>::max();
327 std::cout <<
"low: " << low <<
", high: " << high << std::endl;
332 std::cout <<
"symbols: " << symbols << std::endl;
335 dccl::arith::Model::freq_type each_max_freq =
336 dccl::arith::Model::MAX_FREQUENCY / (symbols + 2);
338 std::cout <<
"each_max_freq: " << each_max_freq << std::endl;
340 model.set_eof_frequency(rand() % each_max_freq + 1);
341 model.set_out_of_range_frequency(rand() % each_max_freq + 1);
343 model.add_value_bound(low);
344 model.add_frequency(rand() % each_max_freq + 1);
345 for (
int j = 1; j < symbols; ++j)
349 dccl::int32 remaining_range = high - model.value_bound(j - 1);
350 model.add_value_bound(model.value_bound(j - 1) +
351 rand() % (remaining_range / symbols - j) + 1);
352 model.add_frequency(rand() % each_max_freq + 1);
355 model.add_value_bound(high);
357 ArithmeticDouble2TestMsg msg_in;
359 for (
unsigned j = 0; j < i; ++j) msg_in.add_value(model.value_bound(rand() % symbols));
361 run_test(model, msg_in);
363 std::cout <<
"end random test #" << i << std::endl;
366 std::cout <<
"all tests passed" << std::endl;