DCCL v4
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 all protobuf types with _default codecs, repeat and non repeat
23 
24 #include <fstream>
25 
26 #include "../../native_protobuf/dccl_native_protobuf.h"
27 #include <google/protobuf/descriptor.pb.h>
28 
29 #include "../../binary.h"
30 #include "../../codec.h"
31 #include "test.pb.h"
32 using namespace dccl::test;
33 
34 dccl::Codec codec;
35 
36 int main(int /*argc*/, char* /*argv*/ [])
37 {
38  dccl::dlog.connect(dccl::logger::ALL, &std::cerr);
39  codec.load<TestMsg>();
40  codec.info<TestMsg>();
41  {
42  TestMsg msg_in;
43  msg_in.add_a(0);
44  msg_in.add_a(1);
45  msg_in.add_a(2);
46 
47  msg_in.add_b(10);
48  msg_in.add_b(11);
49 
50  msg_in.add_c(20);
51  msg_in.add_c(21);
52  msg_in.add_c(22);
53 
54  std::cout << "Message in:\n" << msg_in.DebugString() << std::endl;
55  std::cout << "Try encode..." << std::endl;
56  std::string bytes;
57  codec.encode(&bytes, msg_in);
58  std::cout << "... got bytes (hex): " << dccl::hex_encode(bytes) << std::endl;
59 
60  std::cout << "Try decode..." << std::endl;
61  std::cout << codec.max_size(msg_in.GetDescriptor()) << std::endl;
62 
63  TestMsg msg_out;
64  codec.decode(bytes, &msg_out);
65 
66  std::cout << "... got Message out:\n" << msg_out.DebugString() << std::endl;
67  assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
68  }
69 
70  {
71  TestMsg msg_in;
72  msg_in.add_a(0);
73  msg_in.add_a(1);
74  msg_in.add_a(2);
75  msg_in.add_a(3);
76  msg_in.add_a(4);
77  msg_in.add_a(5);
78 
79  msg_in.add_b(10);
80  msg_in.add_b(11);
81 
82  msg_in.add_c(20);
83  msg_in.add_c(21);
84  msg_in.add_c(22);
85 
86  std::cout << "Message in:\n" << msg_in.DebugString() << std::endl;
87  std::cout << "Try encode..." << std::endl;
88  std::string bytes;
89  codec.encode(&bytes, msg_in);
90  std::cout << "... got bytes (hex): " << dccl::hex_encode(bytes) << std::endl;
91 
92  std::cout << "Try decode..." << std::endl;
93  std::cout << codec.max_size(msg_in.GetDescriptor()) << std::endl;
94 
95  TestMsg msg_out;
96  codec.decode(bytes, &msg_out);
97 
98  msg_in.mutable_a()->RemoveLast();
99 
100  std::cout << "... got Message out:\n" << msg_out.DebugString() << std::endl;
101  assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
102  }
103 
104  {
105  TestMsg msg_in;
106  msg_in.add_a(0);
107  msg_in.add_a(1);
108  msg_in.add_a(2);
109  msg_in.add_a(3);
110  msg_in.add_a(4);
111 
112  msg_in.add_c(20);
113  msg_in.add_c(21);
114  msg_in.add_c(22);
115 
116  std::cout << "Message in:\n" << msg_in.DebugString() << std::endl;
117  std::cout << "Try encode..." << std::endl;
118  std::string bytes;
119  codec.encode(&bytes, msg_in);
120  std::cout << "... got bytes (hex): " << dccl::hex_encode(bytes) << std::endl;
121 
122  std::cout << "Try decode..." << std::endl;
123  std::cout << codec.max_size(msg_in.GetDescriptor()) << std::endl;
124 
125  TestMsg msg_out;
126  codec.decode(bytes, &msg_out);
127 
128  auto bmin = dccl::test::TestMsg::descriptor()
129  ->FindFieldByName("b")
130  ->options()
131  .GetExtension(dccl::field)
132  .min();
133  msg_in.add_b(bmin);
134  msg_in.add_b(bmin);
135 
136  std::cout << "... got Message out:\n" << msg_out.DebugString() << std::endl;
137  assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
138  }
139 
140  try
141  {
142  // should throw exception on missing max repeat
143  codec.load<InvalidTestMsgMissingMaxRepeat>();
144  assert(false);
145  }
146  catch (const dccl::Exception& e)
147  {
148  // expected
149  }
150 
151  try
152  {
153  // should throw exception
154  codec.load<InvalidTestMsgMaxRepeatLessThanOne>();
155  assert(false);
156  }
157  catch (const dccl::Exception& e)
158  {
159  // expected
160  }
161 
162  try
163  {
164  // should throw exception
165  codec.load<InvalidTestMsgMaxRepeatLessThanMinRepeat>();
166  assert(false);
167  }
168  catch (const dccl::Exception& e)
169  {
170  // expected
171  }
172 
173  std::cout << "All tests passed." << std::endl;
174 }
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
Definition: codec.h:63
Exception class for DCCL.
Definition: exception.h:48
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