29 #include "../../arithmetic/field_codec_arithmetic.h"
30 #include "../../codec.h"
32 #include "test_arithmetic.pb.h"
36 void arithmetic_run_test(
dccl::Codec& codec, dccl::arith::protobuf::ArithmeticModel& model,
37 const google::protobuf::Message& msg_in,
bool set_model =
true)
41 model.set_name(
"model");
42 dccl::arith::ModelManager::set_model(codec, model);
45 codec.load(msg_in.GetDescriptor());
46 codec.info(msg_in.GetDescriptor());
52 codec.encode(&bytes, msg_in);
57 std::shared_ptr<google::protobuf::Message> msg_out(msg_in.New());
58 codec.decode(bytes, msg_out.get());
62 assert(msg_in.SerializeAsString() == msg_out->SerializeAsString());
65 void decode_check(
dccl::Codec& codec,
const std::string& encoded, TestMsg msg_in);
66 void run(
int thread,
int num_iterations);
67 int main(
int ,
char* [])
70 std::thread t1([]() { run(1, 100); });
71 std::thread t2([]() { run(2, 100); });
72 std::thread t3([]() { run(3, 100); });
73 std::thread t4([]() { run(4, 100); });
74 std::thread t5([]() { run(5, 100); });
75 std::thread t6([]() { run(6, 100); });
76 std::thread t7([]() { run(7, 100); });
77 std::thread t8([]() { run(8, 100); });
78 std::thread t9([]() { run(9, 100); });
79 std::thread t10([]() { run(10, 100); });
92 dccl::dlog.
connect(dccl::logger::ALL, &std::cerr);
94 std::thread t1([]() { run(1, 10); });
95 std::thread t2([]() { run(2, 10); });
96 std::thread t3([]() { run(3, 10); });
97 std::thread t4([]() { run(4, 10); });
98 std::thread t5([]() { run(5, 10); });
99 std::thread t6([]() { run(6, 10); });
100 std::thread t7([]() { run(7, 10); });
101 std::thread t8([]() { run(8, 10); });
102 std::thread t9([]() { run(9, 10); });
103 std::thread t10([]() { run(10, 10); });
116 std::cout <<
"all tests passed" << std::endl;
119 void run(
int thread,
int num_iterations)
122 codec.load_library(DCCL_ARITHMETIC_NAME);
123 codec.load<TestMsg>();
124 for (
int m = 0; m < num_iterations; ++m)
126 std::cout <<
"Thread " << thread <<
", it: " << m << std::endl;
127 codec.info<TestMsg>();
130 msg_in.set_double_default_optional(++i + 0.1);
131 msg_in.set_float_default_optional(++i + 0.2);
133 msg_in.set_int32_default_optional(++i);
134 msg_in.set_int64_default_optional(-++i);
135 msg_in.set_uint32_default_optional(++i);
136 msg_in.set_uint64_default_optional(++i);
137 msg_in.set_sint32_default_optional(-++i);
138 msg_in.set_sint64_default_optional(++i);
139 msg_in.set_fixed32_default_optional(++i);
140 msg_in.set_fixed64_default_optional(++i);
141 msg_in.set_sfixed32_default_optional(++i);
142 msg_in.set_sfixed64_default_optional(-++i);
144 msg_in.set_bool_default_optional(
true);
146 msg_in.set_string_default_optional(
"abc123");
149 msg_in.set_enum_default_optional(ENUM_C);
150 msg_in.mutable_msg_default_optional()->set_val(++i + 0.3);
151 msg_in.mutable_msg_default_optional()->mutable_msg()->set_val(++i);
153 msg_in.set_double_default_required(++i + 0.1);
154 msg_in.set_float_default_required(++i + 0.2);
156 msg_in.set_int32_default_required(++i);
157 msg_in.set_int64_default_required(-++i);
158 msg_in.set_uint32_default_required(++i);
159 msg_in.set_uint64_default_required(++i);
160 msg_in.set_sint32_default_required(-++i);
161 msg_in.set_sint64_default_required(++i);
162 msg_in.set_fixed32_default_required(++i);
163 msg_in.set_fixed64_default_required(++i);
164 msg_in.set_sfixed32_default_required(++i);
165 msg_in.set_sfixed64_default_required(-++i);
167 msg_in.set_bool_default_required(
true);
169 msg_in.set_string_default_required(
"abc123");
172 msg_in.set_enum_default_required(ENUM_C);
173 msg_in.mutable_msg_default_required()->set_val(++i + 0.3);
174 msg_in.mutable_msg_default_required()->mutable_msg()->set_val(++i);
176 for (
int j = 0; j < 2; ++j)
178 msg_in.add_double_default_repeat(++i + 0.1);
179 msg_in.add_float_default_repeat(++i + 0.2);
181 msg_in.add_int32_default_repeat(++i);
182 msg_in.add_int64_default_repeat(-++i);
183 msg_in.add_uint32_default_repeat(++i);
184 msg_in.add_uint64_default_repeat(++i);
185 msg_in.add_sint32_default_repeat(-++i);
186 msg_in.add_sint64_default_repeat(++i);
187 msg_in.add_fixed32_default_repeat(++i);
188 msg_in.add_fixed64_default_repeat(++i);
189 msg_in.add_sfixed32_default_repeat(++i);
190 msg_in.add_sfixed64_default_repeat(-++i);
192 msg_in.add_bool_default_repeat(
true);
194 msg_in.add_string_default_repeat(
"abc123");
201 msg_in.add_enum_default_repeat(
static_cast<Enum1
>((++i % 3) + 1));
202 EmbeddedMsg1* em_msg = msg_in.add_msg_default_repeat();
203 em_msg->set_val(++i + 0.3);
204 em_msg->mutable_msg()->set_val(++i);
208 codec.encode(&bytes, msg_in);
210 decode_check(codec, bytes, msg_in);
214 dccl::arith::protobuf::ArithmeticModel model;
216 model.set_eof_frequency(4);
218 model.add_value_bound(0);
219 model.add_frequency(5);
221 model.add_value_bound(1);
222 model.add_frequency(1);
224 model.add_value_bound(2);
226 model.set_out_of_range_frequency(0);
228 dccl::test::arith::ArithmeticDoubleTestMsg msg_in;
235 arithmetic_run_test(codec, model, msg_in);
240 void decode_check(
dccl::Codec& codec,
const std::string& encoded, TestMsg msg_in)
243 codec.decode(encoded, &msg_out);
248 msg_in.set_string_default_repeat(0,
"abc1");
249 msg_in.set_string_default_repeat(1,
"abc1");
251 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 ...
void connect(int verbosity_mask, Slot slot)
Connect the output of one or more given verbosities to a slot (function pointer or similar)
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...