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