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//
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 dccl::dlog.connect(dccl::logger::ALL, &std::cerr);
38
39 dccl::Codec codec;
40
41 GobyMessage msg_in1;
42
43 msg_in1.set_telegram("hello!");
44
45 timeval t;
46 gettimeofday(&t, nullptr);
47 dccl::int64 now = 1000000 * static_cast<dccl::int64>(t.tv_sec);
48
49 msg_in1.mutable_header()->set_time(now);
50 msg_in1.mutable_header()->set_time_signed(now);
51 msg_in1.mutable_header()->set_time_double(now / 1000000);
52
53 dccl::int64 past = now / 1000000.0 - 200000; // Pick a time 2+ days in the past.
54 dccl::int64 future = now / 1000000.0 + 200000; // Pick a time 2+ days in the future.
55 msg_in1.mutable_header()->set_pasttime_double(past);
56 msg_in1.mutable_header()->set_futuretime_double(future);
57
58 dccl::int64 msec = t.tv_usec / 1000 * 1000;
59 msg_in1.mutable_header()->set_time_precision(now + msec);
60 msg_in1.mutable_header()->set_time_double_precision((now + t.tv_usec) / 1000000.0);
61
62 msg_in1.mutable_header()->set_source_platform(1);
63 msg_in1.mutable_header()->set_dest_platform(3);
64 msg_in1.mutable_header()->set_dest_type(Header::PUBLISH_OTHER);
65 msg_in1.set_const_int(3);
66
67 codec.info(msg_in1.GetDescriptor(), &std::cout);
68 std::cout << "Message in:\n" << msg_in1.DebugString() << std::endl;
69 codec.load(msg_in1.GetDescriptor());
70 std::cout << "Try encode..." << std::endl;
71 std::string bytes1;
72 codec.encode(&bytes1, msg_in1);
73 std::cout << "... got bytes (hex): " << dccl::hex_encode(bytes1) << std::endl;
74
75 // test that adding garbage to the end does not affect decoding
76 bytes1 += std::string(10, '\0');
77
78 std::cout << "Try decode..." << std::endl;
79
80 auto* msg_out1 = codec.decode<GobyMessage*>(bytes1);
81 std::cout << "... got Message out:\n" << msg_out1->DebugString() << std::endl;
82 assert(msg_in1.SerializeAsString() == msg_out1->SerializeAsString());
83 delete msg_out1;
84
85 std::cout << "all tests passed" << std::endl;
86}
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