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 "test1.pb.h"
28#include "test2.pb.h"
29
30#include <sys/time.h>
31
32#include "../../binary.h"
33using namespace dccl::test;
34
35static_assert(TestMessage1::DCCL_ID == 4);
36static_assert(TestMessage1::DCCL_MAX_BYTES == 64);
37
38static_assert(TestMessage2::DCCL_ID == 5);
39static_assert(TestMessage2::DCCL_MAX_BYTES == 64);
40
41template <typename Message> void run_test(dccl::Codec& codec, Message& msg_in)
42{
43 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try encode..." << std::endl;
44 std::string bytes;
45 codec.encode(&bytes, msg_in);
46 dccl::dlog.is(dccl::logger::INFO) &&
47 dccl::dlog << "... got bytes (hex): " << dccl::hex_encode(bytes) << std::endl;
48
49 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try decode..." << std::endl;
50
51 auto msg_out = codec.decode<std::unique_ptr<google::protobuf::Message>>(bytes);
52 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got Message out:\n"
53 << msg_out->DebugString() << std::endl;
54 assert(msg_in.SerializeAsString() == msg_out->SerializeAsString());
55}
56
57int main(int argc, char* argv[])
58{
59 bool verbose = false;
60 for (int i = 1; i < argc; ++i)
61 {
62 if (argv[i] && argv[i][0] == '-' && argv[i][1] == 'v' && argv[i][2] == '\0')
63 verbose = true;
64 }
65
66 dccl::dlog.connect(verbose ? dccl::logger::ALL : dccl::logger::WARN_PLUS, &std::cerr);
67
68 dccl::Codec codec;
69 codec.load_library("libtest_autoload" SHARED_LIBRARY_SUFFIX);
70
71 if (dccl::dlog.is(dccl::logger::INFO))
72 codec.info_all(&dccl::dlog);
73
74 TestMessage1 msg_in1;
75 msg_in1.set_a(10);
76 run_test(codec, msg_in1);
77
78 TestMessage2 msg_in2;
79 msg_in2.set_b(5);
80 run_test(codec, msg_in2);
81
82 TestMessage3 msg_in3;
83 msg_in3.set_c(53);
84 run_test(codec, msg_in3);
85
86 TestMessage3SupersetName msg_in4;
87 msg_in4.set_d(12);
88 run_test(codec, msg_in4);
89
90 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "all tests passed" << std::endl;
91}
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
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