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 //
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  codec.decode(encoded, &long_id_msg);
57  }
58 
59  {
60  ShortIDEdgeMsg short_id_edge_msg;
61  std::string encoded;
62  codec.load(short_id_edge_msg.GetDescriptor());
63  codec.info(short_id_edge_msg.GetDescriptor(), &dccl::dlog);
64  assert(codec.size(short_id_edge_msg) == 1);
65  codec.encode(&encoded, short_id_edge_msg);
66  assert(codec.id(encoded) == 127);
67  codec.decode(encoded, &short_id_edge_msg);
68  }
69 
70  {
71  LongIDEdgeMsg long_id_edge_msg;
72  std::string encoded;
73  codec.load(long_id_edge_msg.GetDescriptor());
74  codec.info(long_id_edge_msg.GetDescriptor(), &dccl::dlog);
75  codec.encode(&encoded, long_id_edge_msg);
76  assert(codec.id(encoded) == 128);
77  codec.decode(encoded, &long_id_edge_msg);
78  assert(codec.size(long_id_edge_msg) == 2);
79  }
80 
81  {
82  TooLongIDMsg too_long_id_msg;
83  // should fail validation
84  try
85  {
86  codec.load(too_long_id_msg.GetDescriptor());
87  assert(false);
88  }
89  catch (dccl::Exception& e)
90  {
91  }
92  }
93 
94  {
95  ShortIDMsgWithData short_id_msg_with_data;
96  std::string encoded;
97  codec.load(short_id_msg_with_data.GetDescriptor());
98  codec.info(short_id_msg_with_data.GetDescriptor(), &dccl::dlog);
99 
100  short_id_msg_with_data.set_in_head(42);
101  short_id_msg_with_data.set_in_body(37);
102  codec.encode(&encoded, short_id_msg_with_data);
103  assert(codec.id(encoded) == 3);
104  codec.decode(encoded, &short_id_msg_with_data);
105  }
106 
107  std::cout << "all tests passed" << std::endl;
108 }
dccl::test
Unit test namespace.
Definition: test.cpp:45
dccl::Exception
Exception class for DCCL.
Definition: exception.h:47
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