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 fixed id header
25 
26 #include "../../codec.h"
27 #include "test.pb.h"
28 using namespace dccl::test;
29 
30 int main(int /*argc*/, char* /*argv*/[])
31 {
32  dccl::dlog.connect(dccl::logger::ALL, &std::cerr);
33 
34  dccl::Codec codec;
35 
36  {
37  ShortIDMsg short_id_msg;
38  codec.load(short_id_msg.GetDescriptor());
39  codec.info(short_id_msg.GetDescriptor(), &dccl::dlog);
40 
41  std::string encoded;
42  assert(codec.size(short_id_msg) == 1);
43  codec.encode(&encoded, short_id_msg);
44  assert(codec.id(encoded) == 2);
45  codec.decode(encoded, &short_id_msg);
46  }
47 
48  {
49  LongIDMsg long_id_msg;
50  std::string encoded;
51  codec.load(long_id_msg.GetDescriptor());
52  codec.info(long_id_msg.GetDescriptor(), &dccl::dlog);
53  assert(codec.size(long_id_msg) == 2);
54  codec.encode(&encoded, long_id_msg);
55  assert(codec.id(encoded) == 10000);
56  assert(encoded.size() == 2);
57  codec.decode(encoded, &long_id_msg);
58 
59  // try decoding zero and single byte (should fail)
60  try
61  {
62  codec.id(std::string());
63  assert(false);
64  }
65  catch (const dccl::Exception& e)
66  {
67  // expected
68  std::cout << "Expected exception: " << e.what() << std::endl;
69  }
70  try
71  {
72  codec.id(encoded.substr(0, 1));
73  assert(false);
74  }
75  catch (const dccl::Exception& e)
76  {
77  // expected
78  std::cout << "Expected exception: " << e.what() << std::endl;
79  }
80  }
81 
82  {
83  ShortIDEdgeMsg short_id_edge_msg;
84  std::string encoded;
85  codec.load(short_id_edge_msg.GetDescriptor());
86  codec.info(short_id_edge_msg.GetDescriptor(), &dccl::dlog);
87  assert(codec.size(short_id_edge_msg) == 1);
88  codec.encode(&encoded, short_id_edge_msg);
89  assert(codec.id(encoded) == 127);
90  codec.decode(encoded, &short_id_edge_msg);
91  }
92 
93  {
94  LongIDEdgeMsg long_id_edge_msg;
95  std::string encoded;
96  codec.load(long_id_edge_msg.GetDescriptor());
97  codec.info(long_id_edge_msg.GetDescriptor(), &dccl::dlog);
98  codec.encode(&encoded, long_id_edge_msg);
99  assert(codec.id(encoded) == 128);
100  codec.decode(encoded, &long_id_edge_msg);
101  assert(codec.size(long_id_edge_msg) == 2);
102  }
103 
104  {
105  TooLongIDMsg too_long_id_msg;
106  // should fail validation
107  try
108  {
109  codec.load(too_long_id_msg.GetDescriptor());
110  assert(false);
111  }
112  catch (dccl::Exception& e)
113  {
114  }
115  }
116 
117  {
118  ShortIDMsgWithData short_id_msg_with_data;
119  std::string encoded;
120  codec.load(short_id_msg_with_data.GetDescriptor());
121  codec.info(short_id_msg_with_data.GetDescriptor(), &dccl::dlog);
122 
123  short_id_msg_with_data.set_in_head(42);
124  short_id_msg_with_data.set_in_body(37);
125  codec.encode(&encoded, short_id_msg_with_data);
126  assert(codec.id(encoded) == 3);
127  codec.decode(encoded, &short_id_msg_with_data);
128  }
129 
130  std::cout << "all tests passed" << std::endl;
131 }
dccl::test
Unit test namespace.
Definition: test.cpp:44
dccl::Exception
Exception class for DCCL.
Definition: exception.h:46
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