DCCL v3
test.cpp
1 // Copyright 2009-2017 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (for 2013-)
3 // Massachusetts Institute of Technology (for 2007-2014)
4 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Dynamic Compact Control Language Library
8 // ("DCCL").
9 //
10 // DCCL is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // DCCL is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with DCCL. If not, see <http://www.gnu.org/licenses/>.
22 // tests proper encoding of standard Goby header
23 
24 #include "dccl/codec.h"
25 #include "test.pb.h"
26 
27 #include <sys/time.h>
28 
29 #include "dccl/binary.h"
30 using namespace dccl::test;
31 
32 using dccl::operator<<;
33 
34 
35 
36 int main(int argc, char* argv[])
37 {
38  dccl::dlog.connect(dccl::logger::ALL, &std::cerr);
39 
40  dccl::Codec codec;
41 
42  GobyMessage msg_in1;
43 
44  msg_in1.set_telegram("hello!");
45 
46  timeval t;
47  gettimeofday(&t, 0);
48  dccl::int64 now = 1000000 * static_cast<dccl::int64>(t.tv_sec);
49 
50  msg_in1.mutable_header()->set_time(now);
51  msg_in1.mutable_header()->set_time_signed(now);
52  msg_in1.mutable_header()->set_time_double(now / 1000000);
53 
54  dccl::int64 past = now / 1000000.0 - 200000; // Pick a time 2+ days in the past.
55  dccl::int64 future = now / 1000000.0 + 200000; // Pick a time 2+ days in the future.
56  msg_in1.mutable_header()->set_pasttime_double(past);
57  msg_in1.mutable_header()->set_futuretime_double(future);
58 
59  dccl::int64 msec = t.tv_usec / 1000 * 1000;
60  msg_in1.mutable_header()->set_time_precision(now + msec);
61  msg_in1.mutable_header()->set_time_double_precision((now + t.tv_usec) / 1000000.0);
62 
63 
64  msg_in1.mutable_header()->set_source_platform(1);
65  msg_in1.mutable_header()->set_dest_platform(3);
66  msg_in1.mutable_header()->set_dest_type(Header::PUBLISH_OTHER);
67  msg_in1.set_const_int(3);
68 
69  codec.info(msg_in1.GetDescriptor(), &std::cout);
70  std::cout << "Message in:\n" << msg_in1.DebugString() << std::endl;
71  codec.load(msg_in1.GetDescriptor());
72  std::cout << "Try encode..." << std::endl;
73  std::string bytes1;
74  codec.encode(&bytes1, msg_in1);
75  std::cout << "... got bytes (hex): " << dccl::hex_encode(bytes1) << std::endl;
76 
77  // test that adding garbage to the end does not affect decoding
78  bytes1 += std::string(10, '\0');
79 
80  std::cout << "Try decode..." << std::endl;
81 
82  GobyMessage* msg_out1 = codec.decode<GobyMessage*>(bytes1);
83  std::cout << "... got Message out:\n" << msg_out1->DebugString() << std::endl;
84  assert(msg_in1.SerializeAsString() == msg_out1->SerializeAsString());
85  delete msg_out1;
86 
87  std::cout << "all tests passed" << std::endl;
88 }
89 
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
Definition: codec.h:56
void info(std::ostream *os=0, int user_id=-1) const
Writes a human readable summary (including field sizes) of the provided DCCL type to the stream provi...
Definition: codec.h:155
void load()
All messages must be explicited loaded and validated (size checks, option extensions checks...
Definition: codec.h:96
void encode(std::string *bytes, const google::protobuf::Message &msg, bool header_only=false, int user_id=-1)
Encodes a DCCL message.
Definition: codec.cpp:273
google::protobuf::int64 int64
a signed 64 bit integer
Definition: common.h:61
void connect(int verbosity_mask, Slot slot)
Connect the output of one or more given verbosities to a slot (function pointer or similar) ...
Definition: logger.h:161
void hex_encode(CharIterator begin, CharIterator end, std::string *out, bool upper_case=false)
Encodes a (little-endian) hexadecimal string from a byte string. Index 0 of begin is written to index...
Definition: binary.h:100
Unit test namespace.
Definition: test.cpp:44
CharIterator decode(CharIterator begin, CharIterator end, google::protobuf::Message *msg, bool header_only=false)
Decode a DCCL message when the type is known at compile time.
Definition: codec.h:439