DCCL v4
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"
33 using namespace dccl::test;
34 
35 template <typename Message> void run_test(dccl::Codec& codec, Message& msg_in)
36 {
37  std::cout << "Try encode..." << std::endl;
38  std::string bytes;
39  codec.encode(&bytes, msg_in);
40  std::cout << "... got bytes (hex): " << dccl::hex_encode(bytes) << std::endl;
41 
42  std::cout << "Try decode..." << std::endl;
43 
44  auto msg_out = codec.decode<std::unique_ptr<google::protobuf::Message>>(bytes);
45  std::cout << "... got Message out:\n" << msg_out->DebugString() << std::endl;
46  assert(msg_in.SerializeAsString() == msg_out->SerializeAsString());
47 }
48 
49 int main(int /*argc*/, char* /*argv*/ [])
50 {
51  dccl::dlog.connect(dccl::logger::ALL, &std::cerr);
52 
53  dccl::Codec codec;
54  codec.load_library("libtest_autoload" SHARED_LIBRARY_SUFFIX);
55 
56  codec.info_all(&std::cout);
57 
58  TestMessage1 msg_in1;
59  msg_in1.set_a(10);
60  run_test(codec, msg_in1);
61 
62  TestMessage2 msg_in2;
63  msg_in2.set_b(5);
64  run_test(codec, msg_in2);
65 
66  TestMessage3 msg_in3;
67  msg_in3.set_c(53);
68  run_test(codec, msg_in3);
69 
70  TestMessage3SupersetName msg_in4;
71  msg_in4.set_d(12);
72  run_test(codec, msg_in4);
73 
74  std::cout << "all tests passed" << std::endl;
75 }
dccl::test
Unit test namespace.
Definition: test.cpp:44
dccl::Codec
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
Definition: codec.h:62
dccl::Logger::connect
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
dccl::hex_encode
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
Message