DCCL v4
test.cpp
1 // Copyright 2011-2017:
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 "test1.pb.h"
29 #include "test2.pb.h"
30 
31 #include <sys/time.h>
32 
33 #include "../../binary.h"
34 using namespace dccl::test;
35 
36 template <typename Message> void run_test(dccl::Codec& codec, Message& msg_in)
37 {
38  std::cout << "Try encode..." << std::endl;
39  std::string bytes;
40  codec.encode(&bytes, msg_in);
41  std::cout << "... got bytes (hex): " << dccl::hex_encode(bytes) << std::endl;
42 
43  std::cout << "Try decode..." << std::endl;
44 
45  auto msg_out = codec.decode<std::unique_ptr<google::protobuf::Message>>(bytes);
46  std::cout << "... got Message out:\n" << msg_out->DebugString() << std::endl;
47  assert(msg_in.SerializeAsString() == msg_out->SerializeAsString());
48 }
49 
50 int main(int /*argc*/, char* /*argv*/ [])
51 {
52  dccl::dlog.connect(dccl::logger::ALL, &std::cerr);
53 
54  dccl::Codec codec;
55  codec.load_library("libtest_autoload" SHARED_LIBRARY_SUFFIX);
56 
57  codec.info_all(&std::cout);
58 
59  TestMessage1 msg_in1;
60  msg_in1.set_a(10);
61  run_test(codec, msg_in1);
62 
63  TestMessage2 msg_in2;
64  msg_in2.set_b(5);
65  run_test(codec, msg_in2);
66 
67  TestMessage3 msg_in3;
68  msg_in3.set_c(53);
69  run_test(codec, msg_in3);
70 
71  TestMessage3SupersetName msg_in4;
72  msg_in4.set_d(12);
73  run_test(codec, msg_in4);
74 
75  std::cout << "all tests passed" << std::endl;
76 }
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:46
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