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 // 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 receiving one of several static types
26 
27 #include "../../codec.h"
28 #include "test.pb.h"
29 
30 using namespace dccl::test;
31 
32 using dccl::operator<<;
33 
34 GobyMessage1 msg_in1;
35 GobyMessage2 msg_in2;
36 GobyMessage3 msg_in3;
37 dccl::Codec codec;
38 
39 void decode(const std::string& bytes);
40 
41 int main(int /*argc*/, char* /*argv*/ [])
42 {
43  dccl::dlog.connect(dccl::logger::ALL, &std::cerr);
44 
45  codec.load<GobyMessage1>();
46  codec.load<GobyMessage2>();
47  codec.load<GobyMessage3>();
48 
49  codec.info<GobyMessage1>(&dccl::dlog);
50  codec.info<GobyMessage2>(&dccl::dlog);
51  codec.info<GobyMessage3>(&dccl::dlog);
52 
53  msg_in1.set_int32_val(1);
54  msg_in2.set_bool_val(false);
55  msg_in3.set_string_val("string1");
56 
57  std::cout << "Try encode..." << std::endl;
58  std::string bytes1, bytes2, bytes3;
59  codec.encode(&bytes1, msg_in1);
60  std::cout << "... got bytes for GobyMessage1 (hex): " << dccl::hex_encode(bytes1) << std::endl;
61  codec.encode(&bytes2, msg_in2);
62  std::cout << "... got bytes for GobyMessage2 (hex): " << dccl::hex_encode(bytes2) << std::endl;
63  codec.encode(&bytes3, msg_in3);
64  std::cout << "... got bytes for GobyMessage3 (hex): " << dccl::hex_encode(bytes3) << std::endl;
65 
66  std::cout << "Try decode..." << std::endl;
67 
68  // mix up the order
69  decode(bytes2);
70  decode(bytes1);
71  decode(bytes3);
72 
73  std::cout << "all tests passed" << std::endl;
74 }
75 
76 void decode(const std::string& bytes)
77 {
78  unsigned dccl_id = codec.id(bytes);
79 
80  if (dccl_id == codec.id<GobyMessage1>())
81  {
82  GobyMessage1 msg_out1;
83  codec.decode(bytes, &msg_out1);
84 
85  std::cout << "Got..." << msg_out1 << std::endl;
86  assert(msg_out1.SerializeAsString() == msg_in1.SerializeAsString());
87  }
88  else if (dccl_id == codec.id<GobyMessage2>())
89  {
90  GobyMessage2 msg_out2;
91  codec.decode(bytes, &msg_out2);
92 
93  std::cout << "Got..." << msg_out2 << std::endl;
94  assert(msg_out2.SerializeAsString() == msg_in2.SerializeAsString());
95  }
96  else if (dccl_id == codec.id<GobyMessage3>())
97  {
98  GobyMessage3 msg_out3;
99  codec.decode(bytes, &msg_out3);
100 
101  std::cout << "Got..." << msg_out3 << std::endl;
102  assert(msg_out3.SerializeAsString() == msg_in3.SerializeAsString());
103  }
104 }
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