DCCL v3
test.cpp
1 // Copyright 2009-2017 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (for 2013-)
3 // Massachusetts Institute of Technology (for 2007-2014)
4 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Dynamic Compact Control Language Library
8 // ("DCCL").
9 //
10 // DCCL is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // DCCL is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with DCCL. If not, see <http://www.gnu.org/licenses/>.
22 // tests fixed id header
23 
24 #include "dccl/codec.h"
25 #include "test.pb.h"
26 using namespace dccl::test;
27 
28 using dccl::operator<<;
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 
108  std::cout << "all tests passed" << std::endl;
109 }
110 
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
Definition: codec.h:56
void load()
All messages must be explicited loaded and validated (size checks, option extensions checks...
Definition: codec.h:96
unsigned size(const google::protobuf::Message &msg)
Provides the encoded size (in bytes) of msg. This is useful if you need to know the size of a message...
Definition: codec.cpp:390
unsigned id() const
Gives the DCCL id (defined by the custom message option extension "(dccl.msg).id" in the ...
Definition: codec.h:156
void info(std::ostream *os=0) const
Writes a human readable summary (including field sizes) of the provided DCCL type to the stream provi...
Definition: codec.h:139
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:161
Exception class for DCCL.
Definition: exception.h:30
Unit test namespace.
Definition: test.pb.h:36
CharIterator decode(CharIterator begin, CharIterator end, google::protobuf::Message *msg, bool header_only=false)
Decode a DCCL message when the type is known at compile time.
Definition: codec.h:405
void encode(std::string *bytes, const google::protobuf::Message &msg, bool header_only=false)
Encodes a DCCL message.
Definition: codec.cpp:266