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// Chris Murphy <cmurphy@aphysci.com>
8//
9//
10// This file is part of the Dynamic Compact Control Language Library
11// ("DCCL").
12//
13// DCCL is free software: you can redistribute it and/or modify
14// it under the terms of the GNU Lesser General Public License as published by
15// the Free Software Foundation, either version 2.1 of the License, or
16// (at your option) any later version.
17//
18// DCCL is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU Lesser General Public License for more details.
22//
23// You should have received a copy of the GNU Lesser General Public License
24// along with DCCL. If not, see <http://www.gnu.org/licenses/>.
25// tests proper encoding of standard Goby header
26
27#include "../../codec.h"
28#include "../../codecs2/field_codec_default.h"
29#include "test.pb.h"
30
31#include <sys/time.h>
32
33#include "../../binary.h"
34using namespace dccl::test;
35
36template <dccl::int64 fixed_time_usec> struct DummyClock
37{
38 using time_point = std::chrono::time_point<std::chrono::system_clock>;
39 static time_point now() { return time_point(std::chrono::microseconds(fixed_time_usec)); }
40};
41
42int main(int argc, char* argv[])
43{
44 bool verbose = false;
45 for (int i = 1; i < argc; ++i)
46 {
47 if (argv[i] && argv[i][0] == '-' && argv[i][1] == 'v' && argv[i][2] == '\0')
48 verbose = true;
49 }
50
51 dccl::dlog.connect(verbose ? dccl::logger::ALL : dccl::logger::WARN_PLUS, &std::cerr);
52
53 {
54 dccl::Codec codec;
55 GobyMessage msg_in1;
56
57 msg_in1.set_telegram("hello!");
58
59 timeval t;
60 gettimeofday(&t, nullptr);
61 dccl::int64 now = 1000000 * static_cast<dccl::int64>(t.tv_sec);
62
63 msg_in1.mutable_header()->set_time(now);
64 msg_in1.mutable_header()->set_time_signed(now);
65 msg_in1.mutable_header()->set_time_double(now / 1000000);
66
67 dccl::int64 past = now / 1000000.0 - 200000; // Pick a time 2+ days in the past.
68 dccl::int64 future = now / 1000000.0 + 200000; // Pick a time 2+ days in the future.
69 msg_in1.mutable_header()->set_pasttime_double(past);
70 msg_in1.mutable_header()->set_futuretime_double(future);
71
72 dccl::int64 msec = t.tv_usec / 1000 * 1000;
73 msg_in1.mutable_header()->set_time_precision(now + msec);
74 msg_in1.mutable_header()->set_time_double_precision((now + t.tv_usec) / 1000000.0);
75
76 msg_in1.mutable_header()->set_source_platform(1);
77 msg_in1.mutable_header()->set_dest_platform(3);
78 msg_in1.mutable_header()->set_dest_type(Header::PUBLISH_OTHER);
79 msg_in1.set_const_int(3);
80
81 if (dccl::dlog.is(dccl::logger::INFO))
82 {
83 codec.info(msg_in1.GetDescriptor(), &dccl::dlog);
84 }
85 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Message in:\n" << msg_in1.DebugString() << std::endl;
86 codec.load(msg_in1.GetDescriptor());
87 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try encode..." << std::endl;
88 std::string bytes1;
89 codec.encode(&bytes1, msg_in1);
90 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got bytes (hex): " << dccl::hex_encode(bytes1) << std::endl;
91
92 // test that adding garbage to the end does not affect decoding
93 bytes1 += std::string(10, '\0');
94
95 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try decode..." << std::endl;
96
97 auto* msg_out1 = codec.decode<GobyMessage*>(bytes1);
98 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got Message out:\n" << msg_out1->DebugString() << std::endl;
99 assert(msg_in1.SerializeAsString() == msg_out1->SerializeAsString());
100 delete msg_out1;
101 }
102
103 // test non-standard clock for time to allow post processing of dccl.time messages
104 {
105 dccl::Codec codec;
106 // some fixed historic time
107 constexpr dccl::int64 past_now = 1597743788000000;
108
109 // received one second later
110 dccl::v2::TimeCodecClock::set_clock<DummyClock<past_now + 1000000>>();
111
112 TimeTest msg_in1;
113 // some time in the past
114 msg_in1.set_time(past_now);
115
116 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Message in:\n" << msg_in1.DebugString() << std::endl;
117 codec.load(msg_in1.GetDescriptor());
118 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try encode..." << std::endl;
119 std::string bytes1;
120 codec.encode(&bytes1, msg_in1);
121 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got bytes (hex): " << dccl::hex_encode(bytes1) << std::endl;
122
123 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try decode..." << std::endl;
124 auto msg_out1 = codec.decode<std::unique_ptr<google::protobuf::Message>>(bytes1);
125 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got Message out:\n" << msg_out1->DebugString() << std::endl;
126 assert(msg_in1.SerializeAsString() == msg_out1->SerializeAsString());
127 }
128
129 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "all tests passed" << std::endl;
130}
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