24 #include <boost/foreach.hpp> 25 #include <google/protobuf/descriptor.pb.h> 27 #include "goby/acomms/dccl.h" 28 #include "goby/common/time.h" 29 #include "goby/util/as.h" 30 #include "goby/util/binary.h" 33 void decode_check(
const std::string& encoded);
34 using goby::acomms::operator<<;
38 int main(
int argc,
char* argv[])
48 msg_in.set_double_default_optional(++i + 0.1);
49 msg_in.set_float_default_optional(++i + 0.2);
51 msg_in.set_int32_default_optional(++i);
52 msg_in.set_int64_default_optional(-++i);
53 msg_in.set_uint32_default_optional(++i);
54 msg_in.set_uint64_default_optional(++i);
55 msg_in.set_sint32_default_optional(-++i);
56 msg_in.set_sint64_default_optional(++i);
57 msg_in.set_fixed32_default_optional(++i);
58 msg_in.set_fixed64_default_optional(++i);
59 msg_in.set_sfixed32_default_optional(++i);
60 msg_in.set_sfixed64_default_optional(-++i);
62 msg_in.set_bool_default_optional(
true);
64 msg_in.set_string_default_optional(
"abc123");
65 msg_in.set_bytes_default_optional(goby::util::hex_decode(
"00112233aabbcc1234"));
67 msg_in.set_enum_default_optional(ENUM_C);
68 msg_in.mutable_msg_default_optional()->set_val(++i + 0.3);
69 msg_in.mutable_msg_default_optional()->mutable_msg()->set_val(++i);
71 msg_in.set_double_default_required(++i + 0.1);
72 msg_in.set_float_default_required(++i + 0.2);
74 msg_in.set_int32_default_required(++i);
75 msg_in.set_int64_default_required(-++i);
76 msg_in.set_uint32_default_required(++i);
77 msg_in.set_uint64_default_required(++i);
78 msg_in.set_sint32_default_required(-++i);
79 msg_in.set_sint64_default_required(++i);
80 msg_in.set_fixed32_default_required(++i);
81 msg_in.set_fixed64_default_required(++i);
82 msg_in.set_sfixed32_default_required(++i);
83 msg_in.set_sfixed64_default_required(-++i);
85 msg_in.set_bool_default_required(
true);
87 msg_in.set_string_default_required(
"abc123");
88 msg_in.set_bytes_default_required(goby::util::hex_decode(
"00112233aabbcc1234"));
90 msg_in.set_enum_default_required(ENUM_C);
91 msg_in.mutable_msg_default_required()->set_val(++i + 0.3);
92 msg_in.mutable_msg_default_required()->mutable_msg()->set_val(++i);
94 for (
int j = 0; j < 2; ++j)
96 msg_in.add_double_default_repeat(++i + 0.1);
97 msg_in.add_float_default_repeat(++i + 0.2);
99 msg_in.add_int32_default_repeat(++i);
100 msg_in.add_int64_default_repeat(-++i);
101 msg_in.add_uint32_default_repeat(++i);
102 msg_in.add_uint64_default_repeat(++i);
103 msg_in.add_sint32_default_repeat(-++i);
104 msg_in.add_sint64_default_repeat(++i);
105 msg_in.add_fixed32_default_repeat(++i);
106 msg_in.add_fixed64_default_repeat(++i);
107 msg_in.add_sfixed32_default_repeat(++i);
108 msg_in.add_sfixed64_default_repeat(-++i);
110 msg_in.add_bool_default_repeat(
true);
112 msg_in.add_string_default_repeat(
"abc123");
115 msg_in.add_bytes_default_repeat(goby::util::hex_decode(
"00aabbcc"));
117 msg_in.add_bytes_default_repeat(goby::util::hex_decode(
"ffeedd12"));
119 msg_in.add_enum_default_repeat(static_cast<Enum1>((++i % 3) + 1));
121 em_msg->set_val(++i + 0.3);
122 em_msg->mutable_msg()->set_val(++i);
125 codec->info(msg_in.GetDescriptor(), &std::cout);
127 std::ofstream fout(
"/tmp/testmessage.pb");
128 msg_in.SerializeToOstream(&fout);
130 std::cout <<
"Message in:\n" << msg_in.DebugString() << std::endl;
132 codec->validate(msg_in.GetDescriptor());
134 std::cout <<
"Try encode..." << std::endl;
136 codec->encode(&bytes, msg_in);
137 std::cout <<
"... got bytes (hex): " << goby::util::hex_encode(bytes) << std::endl;
139 std::cout <<
"Try decode..." << std::endl;
143 decode_check(goby::util::hex_decode(
144 "047f277b9628060000b95660c0b0188000d8c0132858800008000dc2c4c6626466024488cca8ee324bd05c3f23" 145 "af0000ad9112a09509788013e0820b18e0005ed0204c6c2c4666062042644675975982c65235f10a00ad718a58" 146 "01000000905f27121600000000a0170050640300309201001a0b00007d320a0000a61a0070b20100a81b00d09c" 147 "6f0000a0401026361643102636160300f0dfbd5b2280ea2e330f3da59a2100aabfa55a00000000000000000000" 151 std::string random = bytes;
152 for (
unsigned i = 0; i < 10; ++i)
154 random[(rand() % (bytes.size() - 1) + 1)] = rand() % 256;
155 std::cout <<
"Using junk bytes: " << goby::util::hex_encode(random) << std::endl;
160 codec->decode(random, &msg_out);
162 catch (goby::acomms::DCCLException&)
167 std::cout <<
"all tests passed" << std::endl;
170 void decode_check(
const std::string& encoded)
173 codec->decode(encoded, &msg_out);
175 std::cout <<
"... got Message out:\n" << msg_out.DebugString() << std::endl;
178 msg_in.set_string_default_repeat(0,
"abc1");
179 msg_in.set_string_default_repeat(1,
"abc1");
181 assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
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...