24 #include <google/protobuf/descriptor.pb.h> 26 #include "dccl/arithmetic/field_codec_arithmetic.h" 27 #include "goby/acomms/dccl.h" 29 #include "goby/common/time.h" 30 #include "goby/util/as.h" 31 #include "goby/util/binary.h" 34 using goby::acomms::operator<<;
36 void run_test(dccl::arith::protobuf::ArithmeticModel& model,
41 void* dl_handle = dlopen(
"libdccl_arithmetic" SHARED_LIBRARY_SUFFIX, RTLD_LAZY);
45 std::cerr <<
"Failed to open libdccl_arithmetic" SHARED_LIBRARY_SUFFIX << std::endl;
48 codec->load_shared_library_codecs(dl_handle);
52 model.set_name(
"model");
53 dccl::arith::ModelManager::set_model(model);
56 codec->info(msg_in.GetDescriptor(), &std::cout);
58 codec->validate(msg_in.GetDescriptor());
60 std::cout <<
"Message in:\n" << msg_in.DebugString() << std::endl;
62 std::cout <<
"Try encode..." << std::endl;
64 codec->encode(&bytes, msg_in);
65 std::cout <<
"... got bytes (hex): " << goby::util::hex_encode(bytes) << std::endl;
67 std::cout <<
"Try decode..." << std::endl;
69 boost::shared_ptr<google::protobuf::Message> msg_out(msg_in.New());
70 codec->decode(bytes, msg_out.get());
72 std::cout <<
"... got Message out:\n" << msg_out->DebugString() << std::endl;
74 assert(msg_in.SerializeAsString() == msg_out->SerializeAsString());
78 int main(
int argc,
char* argv[])
80 if (argc > 1 && goby::util::as<bool>(argv[1]) == 1)
92 dccl::arith::protobuf::ArithmeticModel model;
94 model.set_eof_frequency(4);
96 model.add_value_bound(0);
97 model.add_frequency(5);
99 model.add_value_bound(1);
100 model.add_frequency(1);
102 model.add_value_bound(2);
104 model.set_out_of_range_frequency(0);
113 run_test(model, msg_in);
118 dccl::arith::protobuf::ArithmeticModel model;
120 model.add_value_bound(100.0);
121 model.add_frequency(100);
123 model.add_value_bound(100.1);
124 model.add_frequency(100);
126 model.add_value_bound(100.2);
127 model.add_frequency(100);
129 model.add_value_bound(100.3);
130 model.add_frequency(100);
132 model.add_value_bound(100.4);
133 model.add_frequency(90);
135 model.add_value_bound(100.5);
136 model.add_frequency(125);
138 model.add_value_bound(100.6);
139 model.add_frequency(125);
141 model.add_value_bound(100.7);
142 model.add_frequency(125);
144 model.add_value_bound(100.8);
146 model.set_eof_frequency(25);
147 model.set_out_of_range_frequency(10);
151 msg_in.add_value(100.5);
152 msg_in.add_value(100.7);
153 msg_in.add_value(100.2);
155 run_test(model, msg_in);
160 dccl::arith::protobuf::ArithmeticModel model;
162 model.set_eof_frequency(10);
163 model.set_out_of_range_frequency(0);
165 model.add_value_bound(1);
166 model.add_frequency(2);
168 model.add_value_bound(2);
169 model.add_frequency(3);
171 model.add_value_bound(3);
172 model.add_frequency(85);
174 model.add_value_bound(4);
178 msg_in.add_value(ENUM_C);
179 msg_in.add_value(ENUM_C);
180 msg_in.add_value(ENUM_C);
181 msg_in.add_value(ENUM_C);
183 run_test(model, msg_in);
188 dccl::arith::protobuf::ArithmeticModel model;
190 model.set_eof_frequency(10);
191 model.set_out_of_range_frequency(0);
193 model.add_value_bound(1);
194 model.add_frequency(2);
196 model.add_value_bound(2);
197 model.add_frequency(3);
199 model.add_value_bound(3);
200 model.add_frequency(85);
202 model.add_value_bound(4);
206 msg_in.add_value(ENUM_A);
207 msg_in.add_value(ENUM_A);
208 msg_in.add_value(ENUM_A);
209 msg_in.add_value(ENUM_A);
211 run_test(model, msg_in);
215 dccl::arith::protobuf::ArithmeticModel model;
217 model.set_eof_frequency(10);
218 model.set_out_of_range_frequency(0);
220 model.add_value_bound(1);
221 model.add_frequency(2);
223 model.add_value_bound(2);
224 model.add_frequency(3);
226 model.add_value_bound(3);
227 model.add_frequency(85);
229 model.add_value_bound(4);
233 msg_in.set_value(ENUM_B);
235 run_test(model, msg_in);
240 dccl::arith::protobuf::ArithmeticModel model;
242 model.set_eof_frequency(1);
244 model.add_value_bound(0);
245 model.add_frequency(1);
247 model.add_value_bound(1);
248 model.add_frequency(1);
250 model.add_value_bound(2);
252 model.set_out_of_range_frequency(1);
261 model.set_is_adaptive(
true);
262 run_test(model, msg_in);
263 run_test(model, msg_in,
false);
264 run_test(model, msg_in,
false);
265 run_test(model, msg_in,
false);
271 dccl::arith::protobuf::ArithmeticModel model;
273 model.set_eof_frequency(0);
274 model.set_out_of_range_frequency(0);
276 model.add_value_bound(1);
277 model.add_frequency(2);
279 model.add_value_bound(2);
280 model.add_frequency(1);
282 model.add_value_bound(3);
283 model.add_frequency(3);
285 model.add_value_bound(4);
286 model.add_frequency(1);
288 model.add_value_bound(5);
289 model.add_frequency(1);
291 model.add_value_bound(6);
295 msg_in.add_value(ENUM2_A);
296 msg_in.add_value(ENUM2_B);
297 msg_in.add_value(ENUM2_C);
298 msg_in.add_value(ENUM2_C);
299 msg_in.add_value(ENUM2_E);
300 msg_in.add_value(ENUM2_D);
301 msg_in.add_value(ENUM2_A);
302 msg_in.add_value(ENUM2_C);
304 run_test(model, msg_in);
310 for (
unsigned i = 0; i <= ArithmeticDouble2TestMsg::descriptor()
311 ->FindFieldByName(
"value")
313 .GetExtension(dccl::field)
317 dccl::arith::protobuf::ArithmeticModel model;
320 goby::int32 low = -(rand() % std::numeric_limits<goby::int32>::max());
321 goby::int32 high = rand() % std::numeric_limits<goby::int32>::max();
323 std::cout <<
"low: " << low <<
", high: " << high << std::endl;
328 std::cout <<
"symbols: " << symbols << std::endl;
331 dccl::arith::Model::freq_type each_max_freq =
332 dccl::arith::Model::MAX_FREQUENCY / (symbols + 2);
333 std::cout <<
"each_max_freq: " << each_max_freq << std::endl;
335 model.set_eof_frequency(rand() % each_max_freq + 1);
336 model.set_out_of_range_frequency(rand() % each_max_freq + 1);
338 model.add_value_bound(low);
339 model.add_frequency(rand() % each_max_freq + 1);
340 for (
int j = 1; j < symbols; ++j)
344 goby::int32 remaining_range = high - model.value_bound(j - 1);
345 model.add_value_bound(model.value_bound(j - 1) +
346 rand() % (remaining_range / symbols - j) + 1);
347 model.add_frequency(rand() % each_max_freq + 1);
350 model.add_value_bound(high);
354 for (
unsigned j = 0; j < i; ++j) msg_in.add_value(model.value_bound(rand() % symbols));
356 run_test(model, msg_in);
358 std::cout <<
"end random test #" << i << std::endl;
361 std::cout <<
"all tests passed" << std::endl;
void set_name(const std::string &s)
Set the name of the application that the logger is serving.
static DCCLCodec * get()
DCCLCodec is a singleton class; use this to get a pointer to the class.
common::FlexOstream glog
Access the Goby logger through this object.
void add_stream(logger::Verbosity verbosity=logger::VERBOSE, std::ostream *os=0)
Attach a stream object (e.g. std::cout, std::ofstream, ...) to the logger with desired verbosity...
google::protobuf::int32 int32
a signed 32 bit integer