DCCL v4
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 dccl::dlog.connect(dccl::logger::ALL, &std::cerr);
45
46 {
47 dccl::Codec codec;
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 codec.info(msg_in1.GetDescriptor(), &std::cout);
75 std::cout << "Message in:\n" << msg_in1.DebugString() << std::endl;
76 codec.load(msg_in1.GetDescriptor());
77 std::cout << "Try encode..." << std::endl;
78 std::string bytes1;
79 codec.encode(&bytes1, msg_in1);
80 std::cout << "... got bytes (hex): " << dccl::hex_encode(bytes1) << std::endl;
81
82 // test that adding garbage to the end does not affect decoding
83 bytes1 += std::string(10, '\0');
84
85 std::cout << "Try decode..." << std::endl;
86
87 auto* msg_out1 = codec.decode<GobyMessage*>(bytes1);
88 std::cout << "... got Message out:\n" << msg_out1->DebugString() << std::endl;
89 assert(msg_in1.SerializeAsString() == msg_out1->SerializeAsString());
90 delete msg_out1;
91 }
92
93 // test non-standard clock for time to allow post processing of dccl.time messages
94 {
95 dccl::Codec codec;
96 // some fixed historic time
97 constexpr dccl::int64 past_now = 1597743788000000;
98
99 // received one second later
100 dccl::v2::TimeCodecClock::set_clock<DummyClock<past_now + 1000000>>();
101
102 TimeTest msg_in1;
103 // some time in the past
104 msg_in1.set_time(past_now);
105
106 std::cout << "Message in:\n" << msg_in1.DebugString() << std::endl;
107 codec.load(msg_in1.GetDescriptor());
108 std::cout << "Try encode..." << std::endl;
109 std::string bytes1;
110 codec.encode(&bytes1, msg_in1);
111 std::cout << "... got bytes (hex): " << dccl::hex_encode(bytes1) << std::endl;
112
113 std::cout << "Try decode..." << std::endl;
114 auto msg_out1 = codec.decode<std::unique_ptr<google::protobuf::Message>>(bytes1);
115 std::cout << "... got Message out:\n" << msg_out1->DebugString() << std::endl;
116 assert(msg_in1.SerializeAsString() == msg_out1->SerializeAsString());
117 }
118
119 std::cout << "all tests passed" << std::endl;
120}
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
Definition codec.h:63
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:214
Unit test namespace.
Definition test.cpp:45
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:100