24 #include "basic_node_report.pb.h" 25 #include "goby/common/logger.h" 26 #include "goby/moos/moos_translator.h" 27 #include "goby/util/binary.h" 28 #include "goby/util/sci.h" 33 void populate_test_msg(
TestMsg* msg_in);
34 void run_one_in_one_out_test(
MOOSTranslator& translator,
int i,
bool hex_encode);
36 int main(
int argc,
char* argv[])
42 entry.set_protobuf_name(
"TestMsg");
45 parser->set_technique(protobuf::TranslatorEntry::TECHNIQUE_PROTOBUF_TEXT_FORMAT);
46 parser->set_moos_var(
"TEST_MSG_1");
49 serializer->set_technique(protobuf::TranslatorEntry::TECHNIQUE_PROTOBUF_TEXT_FORMAT);
50 serializer->set_moos_var(
"TEST_MSG_1");
52 const double LAT_ORIGIN = 42.5;
53 const double LON_ORIGIN = 10.8;
56 TRANSLATOR_TEST_DIR
"/modemidlookup.txt");
59 geodesy.Initialise(LAT_ORIGIN, LON_ORIGIN);
62 run_one_in_one_out_test(translator, 0,
false);
64 std::set<protobuf::TranslatorEntry> entries;
67 entry.set_protobuf_name(
"TestMsg");
70 parser->set_technique(protobuf::TranslatorEntry::TECHNIQUE_PROTOBUF_NATIVE_ENCODED);
71 parser->set_moos_var(
"TEST_MSG_1");
74 serializer->set_technique(protobuf::TranslatorEntry::TECHNIQUE_PROTOBUF_NATIVE_ENCODED);
75 serializer->set_moos_var(
"TEST_MSG_1");
77 entries.insert(entry);
80 translator.clear_entry(
"TestMsg");
81 translator.add_entry(entries);
84 run_one_in_one_out_test(translator, 1,
true);
88 entry.set_protobuf_name(
"TestMsg");
91 parser->set_technique(
92 protobuf::TranslatorEntry::TECHNIQUE_COMMA_SEPARATED_KEY_EQUALS_VALUE_PAIRS);
93 parser->set_moos_var(
"TEST_MSG_1");
96 serializer->set_technique(
97 protobuf::TranslatorEntry::TECHNIQUE_COMMA_SEPARATED_KEY_EQUALS_VALUE_PAIRS);
98 serializer->set_moos_var(
"TEST_MSG_1");
100 translator.clear_entry(entry.protobuf_name());
101 translator.add_entry(entry);
105 run_one_in_one_out_test(translator, 2,
false);
109 entry.set_protobuf_name(
"TestMsg");
112 parser->set_technique(protobuf::TranslatorEntry::TECHNIQUE_PREFIXED_PROTOBUF_NATIVE_HEX);
113 parser->set_moos_var(
"TEST_MSG_1");
116 serializer->set_technique(
117 protobuf::TranslatorEntry::TECHNIQUE_PREFIXED_PROTOBUF_NATIVE_HEX);
118 serializer->set_moos_var(
"TEST_MSG_1");
120 translator.clear_entry(entry.protobuf_name());
121 translator.add_entry(entry);
125 run_one_in_one_out_test(translator, 3,
false);
127 std::string format_str =
"NAME=%1%,X=%202%,Y=%3%,HEADING=%201%,REPEAT={%10%}";
129 std::string repeat_format_str =
130 format_str +
",REPEAT={%10.0%,%10.1%,%10.2%,%10.3%,%10.4%,%10.5%,%10.6%,%10.7%,%10.8%,%" 131 "10.9%,%10.10%,%10.11%}";
133 entry.set_protobuf_name(
"BasicNodeReport");
136 parser->set_technique(protobuf::TranslatorEntry::TECHNIQUE_FORMAT);
137 parser->set_moos_var(
"NODE_REPORT");
138 parser->set_format(repeat_format_str);
141 serializer->set_technique(protobuf::TranslatorEntry::TECHNIQUE_FORMAT);
142 serializer->set_moos_var(
"NODE_REPORT");
143 serializer->set_format(repeat_format_str);
145 translator.clear_entry(entry.protobuf_name());
146 translator.add_entry(entry);
152 report.set_name(
"unicorn");
154 report.set_y(1023.5);
155 report.set_heading(240);
156 report.add_repeat(1);
157 report.add_repeat(-1);
158 report.add_repeat(2);
159 report.add_repeat(-2);
160 report.add_repeat(3);
161 report.add_repeat(-3);
162 report.add_repeat(4);
163 report.add_repeat(-4);
164 report.add_repeat(5);
165 report.add_repeat(-5);
166 report.add_repeat(6);
167 report.add_repeat(-6);
169 std::multimap<std::string, CMOOSMsg> moos_msgs = translator.protobuf_to_moos(report);
171 for (std::multimap<std::string, CMOOSMsg>::const_iterator it = moos_msgs.begin(),
175 goby::glog <<
"Variable: " << it->first <<
"\n" 176 <<
"Value: " << it->second.GetString() << std::endl;
177 assert(it->second.GetString() ==
178 "NAME=unicorn,X=550,Y=1023.5,HEADING=240,REPEAT={1,-1,2,-2,3,-3,4,-4,5,-5,6,-6}," 179 "REPEAT={1,-1,2,-2,3,-3,4,-4,5,-5,6,-6}");
182 typedef std::auto_ptr<google::protobuf::Message> GoogleProtobufMessagePointer;
183 GoogleProtobufMessagePointer report_out =
184 translator.moos_to_protobuf<GoogleProtobufMessagePointer>(moos_msgs,
"BasicNodeReport");
187 goby::glog << report_out->DebugString() << std::endl;
188 assert(report_out->SerializeAsString() == report.SerializeAsString());
192 entry.set_protobuf_name(
"BasicNodeReport");
195 parser->set_technique(protobuf::TranslatorEntry::TECHNIQUE_FORMAT);
196 parser->set_moos_var(
"NAV_X");
197 parser->set_format(
"%202%");
199 parser = entry.add_create();
200 parser->set_technique(protobuf::TranslatorEntry::TECHNIQUE_FORMAT);
201 parser->set_moos_var(
"VEHICLE_NAME");
203 algo_in->set_name(
"to_lower");
204 algo_in->set_primary_field(1);
205 parser->set_format(
"%1%");
207 parser = entry.add_create();
208 parser->set_technique(
209 protobuf::TranslatorEntry::TECHNIQUE_COMMA_SEPARATED_KEY_EQUALS_VALUE_PAIRS);
210 parser->set_moos_var(
"NAV_HEADING");
211 algo_in = parser->add_algorithm();
212 algo_in->set_name(
"angle_0_360");
213 algo_in->set_primary_field(201);
215 parser = entry.add_create();
216 parser->set_technique(protobuf::TranslatorEntry::TECHNIQUE_FORMAT);
217 parser->set_moos_var(
"NAV_Y");
218 parser->set_format(
"%3%");
221 serializer->set_technique(protobuf::TranslatorEntry::TECHNIQUE_FORMAT);
222 serializer->set_moos_var(
"NODE_REPORT_FORMAT");
223 serializer->set_format(format_str +
";LAT=%100%;LON=%101%;X+Y=%104%,X-Y=%105%");
226 serializer->add_algorithm();
227 algo_out->set_name(
"utm_x2lon");
228 algo_out->set_output_virtual_field(101);
229 algo_out->set_primary_field(202);
230 algo_out->add_reference_field(3);
232 algo_out = serializer->add_algorithm();
233 algo_out->set_name(
"utm_y2lat");
234 algo_out->set_output_virtual_field(100);
235 algo_out->set_primary_field(3);
236 algo_out->add_reference_field(202);
238 algo_out = serializer->add_algorithm();
239 algo_out->set_name(
"name2modem_id");
240 algo_out->set_output_virtual_field(102);
241 algo_out->set_primary_field(1);
243 algo_out = serializer->add_algorithm();
244 algo_out->set_name(
"name2modem_id");
245 algo_out->set_output_virtual_field(103);
246 algo_out->set_primary_field(1);
248 algo_out = serializer->add_algorithm();
249 algo_out->set_name(
"modem_id2type");
250 algo_out->set_output_virtual_field(103);
251 algo_out->set_primary_field(1);
253 algo_out = serializer->add_algorithm();
254 algo_out->set_name(
"to_upper");
255 algo_out->set_output_virtual_field(103);
256 algo_out->set_primary_field(1);
258 algo_out = serializer->add_algorithm();
259 algo_out->set_name(
"add");
260 algo_out->set_output_virtual_field(104);
261 algo_out->set_primary_field(202);
262 algo_out->add_reference_field(3);
264 algo_out = serializer->add_algorithm();
265 algo_out->set_name(
"subtract");
266 algo_out->set_output_virtual_field(105);
267 algo_out->set_primary_field(202);
268 algo_out->add_reference_field(3);
271 serializer2->CopyFrom(*serializer);
272 serializer2->clear_format();
273 serializer2->set_technique(
274 protobuf::TranslatorEntry::TECHNIQUE_COMMA_SEPARATED_KEY_EQUALS_VALUE_PAIRS);
275 serializer2->set_moos_var(
"NODE_REPORT_KEY_VALUE");
277 translator.clear_entry(entry.protobuf_name());
278 translator.add_entry(entry);
284 moos_msgs.insert(std::make_pair(
"NAV_X",
CMOOSMsg(MOOS_NOTIFY,
"NAV_X", report.x())));
285 moos_msgs.insert(std::make_pair(
"NAV_Y",
CMOOSMsg(MOOS_NOTIFY,
"NAV_Y", report.y())));
287 std::make_pair(
"NAV_HEADING",
CMOOSMsg(MOOS_NOTIFY,
"NAV_HEADING",
"heading=-120")));
289 std::make_pair(
"VEHICLE_NAME",
CMOOSMsg(MOOS_NOTIFY,
"VEHICLE_NAME",
"UNICORN")));
292 translator.moos_to_protobuf<GoogleProtobufMessagePointer>(moos_msgs,
"BasicNodeReport");
294 report.clear_repeat();
297 goby::glog << report.DebugString() << std::endl;
299 goby::glog << report_out->DebugString() << std::endl;
301 assert(report_out->SerializeAsString() == report.SerializeAsString());
303 moos_msgs = translator.protobuf_to_moos(*report_out);
305 double expected_lat = 0, expected_lon = 0;
306 geodesy.UTM2LatLong(report.x(), report.y(), expected_lat, expected_lon);
307 const int LAT_INT_DIGITS = 2;
308 const int LON_INT_DIGITS = 3;
309 expected_lat = goby::util::unbiased_round(expected_lat, std::numeric_limits<double>::digits10 -
311 expected_lon = goby::util::unbiased_round(expected_lon, std::numeric_limits<double>::digits10 -
314 std::stringstream expected_lat_ss, expected_lon_ss;
315 expected_lat_ss << std::setprecision(std::numeric_limits<double>::digits10) << expected_lat;
316 expected_lon_ss << std::setprecision(std::numeric_limits<double>::digits10) << expected_lon;
317 boost::format expected_lat_fmt(
"%1%");
318 boost::format expected_lon_fmt(
"%1%");
319 std::string expected_lat_fmt_str =
321 boost::io::group(std::setprecision(std::numeric_limits<double>::digits10), expected_lat))
323 std::string expected_lon_fmt_str =
325 boost::io::group(std::setprecision(std::numeric_limits<double>::digits10), expected_lon))
327 std::string expected_lat_key_values_str = expected_lat_ss.str();
328 std::string expected_lon_key_values_str = expected_lon_ss.str();
330 for (std::multimap<std::string, CMOOSMsg>::const_iterator it = moos_msgs.begin(),
334 goby::glog <<
"Variable: " << it->first <<
"\n" 335 <<
"Value: " << it->second.GetString() << std::endl;
337 goby::glog <<
"Expected lat (FORMAT): " << expected_lat_fmt_str << std::endl;
338 goby::glog <<
"Expected lon (FORMAT): " << expected_lon_fmt_str << std::endl;
339 goby::glog <<
"Expected lat (KEY_VALUES): " << expected_lat_key_values_str << std::endl;
340 goby::glog <<
"Expected lon (KEY_VALUES): " << expected_lon_key_values_str << std::endl;
342 if (it->first ==
"NODE_REPORT_FORMAT")
343 assert(it->second.GetString() ==
344 std::string(
"NAME=unicorn,X=550,Y=1023.5,HEADING=240,REPEAT={};LAT=") +
345 expected_lat_fmt_str +
";LON=" + expected_lon_fmt_str +
346 ";X+Y=1573.5,X-Y=-473.5");
347 else if (it->first ==
"NODE_REPORT_KEY_VALUE")
348 assert(it->second.GetString() ==
349 std::string(
"Name=unicorn,x=550,y=1023.5,heading=240,utm_y2lat(y)=") +
350 expected_lat_key_values_str +
353 expected_lon_key_values_str +
354 ",name2modem_id(Name)=3,name2modem_id+modem_id2type+to_upper(" 355 "Name)=AUV,add(x)=1573.5,subtract(x)=-473.5");
358 std::string sub_message_format_str =
"em.val=%17:1%";
361 entry.set_protobuf_name(
"TestMsg");
364 parser->set_technique(protobuf::TranslatorEntry::TECHNIQUE_FORMAT);
365 parser->set_moos_var(
"TEST_MSG_1");
366 parser->set_format(sub_message_format_str);
369 serializer->set_technique(protobuf::TranslatorEntry::TECHNIQUE_FORMAT);
370 serializer->set_moos_var(
"TEST_MSG_1");
371 serializer->set_format(sub_message_format_str);
373 translator.clear_entry(entry.protobuf_name());
374 translator.add_entry(entry);
380 embedded_test.mutable_msg_default_optional()->set_val(19.998);
381 moos_msgs = translator.protobuf_to_moos(embedded_test);
383 for (std::multimap<std::string, CMOOSMsg>::const_iterator it = moos_msgs.begin(),
387 goby::glog <<
"Variable: " << it->first <<
"\n" 388 <<
"Value: " << it->second.GetString() << std::endl;
389 assert(it->second.GetString() ==
"em.val=19.998");
392 typedef std::auto_ptr<google::protobuf::Message> GoogleProtobufMessagePointer;
393 GoogleProtobufMessagePointer embedded_test_out =
394 translator.moos_to_protobuf<GoogleProtobufMessagePointer>(moos_msgs,
"TestMsg");
397 goby::glog << embedded_test_out->DebugString() << std::endl;
398 assert(embedded_test_out->SerializePartialAsString() ==
399 embedded_test.SerializePartialAsString());
401 sub_message_format_str =
402 "em0.val=%117.0:1%,1uint64=%106.1%,0uint64=%106.0%.2uint64=%106.2%:em1.val=%117.1:1%,dbl0=%" 403 "101.0%,dbl1=%101.1%,dbl2=%101.2%,dbl3=%101.3%,em0.em1.val=%37:2:1%";
406 entry.set_protobuf_name(
"TestMsg");
409 parser->set_technique(protobuf::TranslatorEntry::TECHNIQUE_FORMAT);
410 parser->set_moos_var(
"TEST_MSG_1");
411 parser->set_format(sub_message_format_str);
414 serializer->set_technique(protobuf::TranslatorEntry::TECHNIQUE_FORMAT);
415 serializer->set_moos_var(
"TEST_MSG_1");
416 serializer->set_format(sub_message_format_str);
418 translator.clear_entry(entry.protobuf_name());
419 translator.add_entry(entry);
424 embedded_test.Clear();
425 embedded_test.add_msg_default_repeat()->set_val(21.123);
426 embedded_test.add_msg_default_repeat()->set_val(100.5);
427 embedded_test.mutable_msg_default_required()->mutable_msg()->set_val(45);
428 embedded_test.add_uint64_default_repeat(0);
429 embedded_test.add_uint64_default_repeat(100);
430 embedded_test.add_uint64_default_repeat(200);
431 moos_msgs = translator.protobuf_to_moos(embedded_test);
433 for (std::multimap<std::string, CMOOSMsg>::const_iterator it = moos_msgs.begin(),
437 goby::glog <<
"Variable: " << it->first <<
"\n" 438 <<
"Value: " << it->second.GetString() << std::endl;
439 assert(it->second.GetString() ==
440 "em0.val=21.123,1uint64=100,0uint64=0.2uint64=200:em1.val=100.5,dbl0=nan,dbl1=nan," 441 "dbl2=nan,dbl3=nan,em0.em1.val=45");
444 typedef std::auto_ptr<google::protobuf::Message> GoogleProtobufMessagePointer;
446 translator.moos_to_protobuf<GoogleProtobufMessagePointer>(moos_msgs,
"TestMsg");
449 goby::glog << embedded_test_out->DebugString() << std::endl;
451 embedded_test.add_double_default_repeat(std::numeric_limits<double>::quiet_NaN());
452 embedded_test.add_double_default_repeat(std::numeric_limits<double>::quiet_NaN());
453 embedded_test.add_double_default_repeat(std::numeric_limits<double>::quiet_NaN());
454 embedded_test.add_double_default_repeat(std::numeric_limits<double>::quiet_NaN());
455 assert(embedded_test_out->SerializePartialAsString() ==
456 embedded_test.SerializePartialAsString());
458 std::cout <<
"all tests passed" << std::endl;
461 void run_one_in_one_out_test(
MOOSTranslator& translator,
int i,
bool hex_encode)
464 populate_test_msg(&msg);
466 std::multimap<std::string, CMOOSMsg> moos_msgs = translator.protobuf_to_moos(msg);
468 for (std::multimap<std::string, CMOOSMsg>::const_iterator it = moos_msgs.begin(),
472 goby::glog <<
"Variable: " << it->first <<
"\n" 474 << (hex_encode ? goby::util::hex_encode(it->second.GetString())
475 : it->second.GetString())
484 protobuf::TranslatorEntry::TECHNIQUE_PROTOBUF_TEXT_FORMAT>::serialize(&test,
488 assert(it->second.GetString() == test);
489 assert(it->first ==
"TEST_MSG_1");
495 assert(it->second.GetString() == msg.SerializeAsString());
496 assert(it->first ==
"TEST_MSG_1");
504 protobuf::TranslatorEntry::TECHNIQUE_COMMA_SEPARATED_KEY_EQUALS_VALUE_PAIRS>::
505 serialize(&test, msg,
506 google::protobuf::RepeatedPtrField<
508 assert(it->second.GetString() == test);
509 assert(it->first ==
"TEST_MSG_1");
517 protobuf::TranslatorEntry::TECHNIQUE_PREFIXED_PROTOBUF_NATIVE_HEX>::
518 parse(it->second.GetString(), &msg_out);
520 assert(msg.SerializeAsString() == msg_out.SerializeAsString());
521 assert(it->first ==
"TEST_MSG_1");
525 default: assert(
false);
531 typedef std::auto_ptr<google::protobuf::Message> GoogleProtobufMessagePointer;
532 GoogleProtobufMessagePointer msg_out =
533 translator.moos_to_protobuf<GoogleProtobufMessagePointer>(moos_msgs,
"TestMsg");
536 goby::glog << msg_out->DebugString() << std::endl;
537 assert(msg_out->SerializeAsString() == msg.SerializeAsString());
540 void populate_test_msg(
TestMsg* msg_in)
543 msg_in->set_double_default_optional(++i + 0.1);
544 msg_in->set_float_default_optional(++i + 0.2);
546 msg_in->set_int32_default_optional(++i);
547 msg_in->set_int64_default_optional(-++i);
548 msg_in->set_uint32_default_optional(++i);
549 msg_in->set_uint64_default_optional(++i);
550 msg_in->set_sint32_default_optional(-++i);
551 msg_in->set_sint64_default_optional(++i);
552 msg_in->set_fixed32_default_optional(++i);
553 msg_in->set_fixed64_default_optional(++i);
554 msg_in->set_sfixed32_default_optional(++i);
555 msg_in->set_sfixed64_default_optional(-++i);
557 msg_in->set_bool_default_optional(
true);
559 msg_in->set_string_default_optional(
"abc123");
560 msg_in->set_bytes_default_optional(goby::util::hex_decode(
"00112233aabbcc1234"));
562 msg_in->set_enum_default_optional(ENUM_C);
563 msg_in->mutable_msg_default_optional()->set_val(++i + 0.3);
564 msg_in->mutable_msg_default_optional()->mutable_msg()->set_val(++i);
566 msg_in->set_double_default_required(++i + 0.1);
567 msg_in->set_float_default_required(++i + 0.2);
569 msg_in->set_int32_default_required(++i);
570 msg_in->set_int64_default_required(-++i);
571 msg_in->set_uint32_default_required(++i);
572 msg_in->set_uint64_default_required(++i);
573 msg_in->set_sint32_default_required(-++i);
574 msg_in->set_sint64_default_required(++i);
575 msg_in->set_fixed32_default_required(++i);
576 msg_in->set_fixed64_default_required(++i);
577 msg_in->set_sfixed32_default_required(++i);
578 msg_in->set_sfixed64_default_required(-++i);
580 msg_in->set_bool_default_required(
true);
582 msg_in->set_string_default_required(
"abc123");
583 msg_in->set_bytes_default_required(goby::util::hex_decode(
"00112233aabbcc1234"));
585 msg_in->set_enum_default_required(ENUM_C);
586 msg_in->mutable_msg_default_required()->set_val(++i + 0.3);
587 msg_in->mutable_msg_default_required()->mutable_msg()->set_val(++i);
589 for (
int j = 0; j < 2; ++j)
591 msg_in->add_double_default_repeat(++i + 0.1);
592 msg_in->add_float_default_repeat(++i + 0.2);
594 msg_in->add_int32_default_repeat(++i);
595 msg_in->add_int64_default_repeat(-++i);
596 msg_in->add_uint32_default_repeat(++i);
597 msg_in->add_uint64_default_repeat(++i);
598 msg_in->add_sint32_default_repeat(-++i);
599 msg_in->add_sint64_default_repeat(++i);
600 msg_in->add_fixed32_default_repeat(++i);
601 msg_in->add_fixed64_default_repeat(++i);
602 msg_in->add_sfixed32_default_repeat(++i);
603 msg_in->add_sfixed64_default_repeat(-++i);
605 msg_in->add_bool_default_repeat(
true);
607 msg_in->add_string_default_repeat(
"abc123");
610 msg_in->add_bytes_default_repeat(goby::util::hex_decode(
"00aabbcc"));
612 msg_in->add_bytes_default_repeat(goby::util::hex_decode(
"ffeedd12"));
614 msg_in->add_enum_default_repeat(static_cast<Enum1>((++i % 3) + 1));
615 EmbeddedMsg1* em_msg = msg_in->add_msg_default_repeat();
616 em_msg->set_val(++i + 0.3);
617 em_msg->mutable_msg()->set_val(++i);
void set_name(const std::string &s)
Set the name of the application that the logger is serving.
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...