DCCL v5
Loading...
Searching...
No Matches
test.cpp
1// Copyright 2011-2023:
2// GobySoft, LLC (2013-)
3// Massachusetts Institute of Technology (2007-2014)
4// Community contributors (see AUTHORS file)
5// File authors:
6// Toby Schneider <toby@gobysoft.org>
7//
8//
9// This file is part of the Dynamic Compact Control Language Library
10// ("DCCL").
11//
12// DCCL is free software: you can redistribute it and/or modify
13// it under the terms of the GNU Lesser General Public License as published by
14// the Free Software Foundation, either version 2.1 of the License, or
15// (at your option) any later version.
16//
17// DCCL is distributed in the hope that it will be useful,
18// but WITHOUT ANY WARRANTY; without even the implied warranty of
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20// GNU Lesser General Public License for more details.
21//
22// You should have received a copy of the GNU Lesser General Public License
23// along with DCCL. If not, see <http://www.gnu.org/licenses/>.
24// tests proper encoding of standard Goby header
25
26#include "../../codec.h"
27#include "test.pb.h"
28
29#include <sys/time.h>
30
31#include "../../binary.h"
32
33using namespace dccl::test;
34
35int main(int argc, char* argv[])
36{
37 bool verbose = false;
38 for (int i = 1; i < argc; ++i)
39 {
40 if (argv[i] && argv[i][0] == '-' && argv[i][1] == 'v' && argv[i][2] == '\0')
41 verbose = true;
42 }
43
44 dccl::dlog.connect(verbose ? dccl::logger::ALL : dccl::logger::WARN_PLUS, &std::cerr);
45
46 dccl::Codec codec;
47
48 GobyMessage msg_in1;
49
50 msg_in1.set_telegram("hello!");
51
52 timeval t;
53 gettimeofday(&t, nullptr);
54 dccl::int64 now = 1000000 * static_cast<dccl::int64>(t.tv_sec);
55
56 msg_in1.mutable_header()->set_time(now);
57 msg_in1.mutable_header()->set_time_signed(now);
58 msg_in1.mutable_header()->set_time_double(now / 1000000);
59
60 dccl::int64 past = now / 1000000.0 - 200000; // Pick a time 2+ days in the past.
61 dccl::int64 future = now / 1000000.0 + 200000; // Pick a time 2+ days in the future.
62 msg_in1.mutable_header()->set_pasttime_double(past);
63 msg_in1.mutable_header()->set_futuretime_double(future);
64
65 dccl::int64 msec = t.tv_usec / 1000 * 1000;
66 msg_in1.mutable_header()->set_time_precision(now + msec);
67 msg_in1.mutable_header()->set_time_double_precision((now + t.tv_usec) / 1000000.0);
68
69 msg_in1.mutable_header()->set_source_platform(1);
70 msg_in1.mutable_header()->set_dest_platform(3);
71 msg_in1.mutable_header()->set_dest_type(Header::PUBLISH_OTHER);
72 msg_in1.set_const_int(3);
73
74 if (dccl::dlog.is(dccl::logger::INFO))
75 {
76 codec.info(msg_in1.GetDescriptor(), &dccl::dlog);
77 }
78 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Message in:\n" << msg_in1.DebugString() << std::endl;
79 codec.load(msg_in1.GetDescriptor());
80 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try encode..." << std::endl;
81 std::string bytes1;
82 codec.encode(&bytes1, msg_in1);
83 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got bytes (hex): " << dccl::hex_encode(bytes1) << std::endl;
84
85 // test that adding garbage to the end does not affect decoding
86 bytes1 += std::string(10, '\0');
87
88 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try decode..." << std::endl;
89
90 auto* msg_out1 = codec.decode<GobyMessage*>(bytes1);
91 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got Message out:\n" << msg_out1->DebugString() << std::endl;
92 assert(msg_in1.SerializeAsString() == msg_out1->SerializeAsString());
93 delete msg_out1;
94
95 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "all tests passed" << std::endl;
96}
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
Definition codec.h:61
bool is(logger::Verbosity verbosity, logger::Group group=logger::GENERAL)
Indicates the verbosity of the Logger until the next std::flush or std::endl. The boolean return is u...
Definition logger.h:191
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:213
Unit test namespace.
Definition test.cpp:45
Dynamic Compact Control Language namespace.
Definition any.h:28
google::protobuf::int64 int64
a signed 64 bit integer
Definition common.h:62
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:95