DCCL v5
Loading...
Searching...
No Matches
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 all protobuf types with _default codecs, repeat and non repeat
25
26#include <fstream>
27
28
29#include "../../binary.h"
30#include "../../codec.h"
31
32#if CODEC_VERSION == 4
33#include "test4.pb.h"
34#elif CODEC_VERSION == 5
35#include "test5.pb.h"
36#endif
37
38using namespace dccl::test;
39
40dccl::Codec codec;
41TestMsg msg_in;
42
43void decode_check(const std::string& encoded);
44void test0();
45void test1();
46void test2();
47void test3();
48
49int main(int argc, char* argv[])
50{
51 bool verbose = false;
52 for (int i = 1; i < argc; ++i)
53 {
54 if (argv[i] && argv[i][0] == '-' && argv[i][1] == 'v' && argv[i][2] == '\0')
55 verbose = true;
56 }
57
58 dccl::dlog.connect(verbose ? dccl::logger::ALL : dccl::logger::WARN_PLUS, &std::cerr);
59 test0();
60 test1();
61 test2();
62 // oneof
63 test3();
64 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "all tests passed" << std::endl;
65}
66
67void test0()
68{
69 msg_in.set_state(TestMsg::STATE_1);
70 msg_in.set_a(40);
71 msg_in.set_b(50);
72 msg_in.set_c(60);
73 msg_in.set_c_center(50);
74 msg_in.add_d(50);
75 msg_in.add_d(100);
76 msg_in.add_d(150);
77 msg_in.add_d(200);
78 msg_in.add_d(250);
79 msg_in.add_d(300);
80 // {
81 // auto c = msg_in.add_child();
82 // c->set_include_i(TestMsg::Child::YES);
83 // c->set_i(1);
84 // }
85 {
86 auto c = msg_in.add_child();
87 c->set_include_i(TestMsg::Child::NO);
88 c->set_i(25);
89 c->set_i2(25);
90 }
91 {
92 auto c = msg_in.add_child();
93 c->set_include_i(TestMsg::Child::YES);
94 c->set_i(45);
95 c->set_i2(45);
96 }
97 {
98 auto c = msg_in.add_child();
99 c->set_include_i(TestMsg::Child::NO);
100 c->set_i(15);
101 c->set_i2(15);
102 }
103
104 {
105 auto c = msg_in.mutable_child2();
106 c->set_include_i(TestMsg::Child2::NO);
107 c->set_i(13);
108 }
109 {
110 auto c = msg_in.mutable_child3();
111 c->set_include_i(TestMsg::Child3::YES);
112 c->set_i(14);
113
114 auto sc = c->mutable_subchild();
115 sc->set_include_i(TestMsg::Child2::YES);
116 sc->set_i(15);
117 }
118
119 codec.info(msg_in.GetDescriptor());
120
121 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Message in:\n" << msg_in.DebugString() << std::endl;
122
123 codec.load(msg_in.GetDescriptor());
124
125 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try encode..." << std::endl;
126 std::string bytes;
127 codec.encode(&bytes, msg_in);
128 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got bytes (hex): " << dccl::hex_encode(bytes) << std::endl;
129
130 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try decode..." << std::endl;
131
132 // b is omitted
133 msg_in.clear_b();
134
135 // d[3] is omitted
136 msg_in.mutable_d()->erase(msg_in.mutable_d()->begin() + 3);
137
138 // child[0].i is omitted
139 msg_in.mutable_child(0)->clear_i();
140 msg_in.mutable_child(0)->clear_i2();
141
142 // child[2].i is omitted
143 msg_in.mutable_child(2)->clear_i();
144 msg_in.mutable_child(2)->clear_i2();
145
146 msg_in.mutable_child2()->clear_i();
147
148 decode_check(bytes);
149}
150
151void test1()
152{
153 msg_in.set_state(TestMsg::STATE_2);
154 msg_in.set_b(15);
155 msg_in.add_d(100);
156 msg_in.add_d(150);
157 msg_in.add_d(200);
158
159 msg_in.add_d(0);
160
161 msg_in.add_d(250);
162 msg_in.add_d(300);
163
164 {
165 auto c = msg_in.add_child();
166 c->set_include_i(TestMsg::Child::YES);
167 c->set_i(50);
168 c->set_i2(45);
169 }
170 {
171 auto c = msg_in.add_child();
172 c->set_include_i(TestMsg::Child::NO);
173 }
174 {
175 auto c = msg_in.mutable_child2();
176 c->set_include_i(TestMsg::Child2::YES);
177 c->set_i(15);
178 }
179
180 codec.info(msg_in.GetDescriptor());
181
182 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Message in:\n" << msg_in.DebugString() << std::endl;
183
184 codec.load(msg_in.GetDescriptor());
185
186 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try encode..." << std::endl;
187 std::string bytes;
188 codec.encode(&bytes, msg_in);
189 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got bytes (hex): " << dccl::hex_encode(bytes) << std::endl;
190
191 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try decode..." << std::endl;
192
193 msg_in.mutable_d()->erase(msg_in.mutable_d()->begin() + 3);
194
195 decode_check(bytes);
196}
197
198void test2()
199{
200 msg_in.set_state(TestMsg::STATE_2);
201 msg_in.set_b(31);
202
203 {
204 auto c = msg_in.add_child();
205 c->set_include_i(TestMsg::Child::YES);
206 c->set_i(23);
207 c->set_i2(13);
208 }
209 {
210 auto c = msg_in.add_child();
211 c->set_include_i(TestMsg::Child::NO);
212 }
213 {
214 auto c = msg_in.mutable_child2();
215 c->set_include_i(TestMsg::Child2::NO);
216 }
217
218 codec.info(msg_in.GetDescriptor());
219
220 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Message in:\n" << msg_in.DebugString() << std::endl;
221
222 codec.load(msg_in.GetDescriptor());
223
224 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try encode..." << std::endl;
225 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Min Size: " << codec.min_size(msg_in.GetDescriptor()) << std::endl;
226 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Max Size: " << codec.max_size(msg_in.GetDescriptor()) << std::endl;
227 std::vector<char> bytes(codec.size(msg_in), 0);
228
229 int s = codec.size(msg_in);
230 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Size: " << s << std::endl;
231 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Bytes Size: " << bytes.size() << std::endl;
232
233 codec.encode(bytes.data(), bytes.size(), msg_in);
234 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got bytes (hex): "
235 << dccl::hex_encode(std::string(bytes.begin(), bytes.end())) << std::endl;
236
237 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try decode..." << std::endl;
238
239 decode_check(std::string(bytes.begin(), bytes.end()));
240}
241
242void test3()
243{
244 msg_in.set_state(TestMsg::STATE_1);
245 msg_in.set_a(10);
246 msg_in.set_c_center(100);
247 msg_in.set_c(100);
248
249 msg_in.set_y(13);
250 {
251 auto c = msg_in.mutable_child2();
252 c->set_include_i(TestMsg::Child2::NO);
253 }
254
255 codec.info(msg_in.GetDescriptor());
256
257 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Message in:\n" << msg_in.DebugString() << std::endl;
258
259 codec.load(msg_in.GetDescriptor());
260
261 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try encode..." << std::endl;
262 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Min Size: " << codec.min_size(msg_in.GetDescriptor()) << std::endl;
263 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Max Size: " << codec.max_size(msg_in.GetDescriptor()) << std::endl;
264 std::vector<char> bytes(codec.size(msg_in), 0);
265
266 int s = codec.size(msg_in);
267 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Size: " << s << std::endl;
268 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Bytes Size: " << bytes.size() << std::endl;
269
270 codec.encode(bytes.data(), bytes.size(), msg_in);
271 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got bytes (hex): "
272 << dccl::hex_encode(std::string(bytes.begin(), bytes.end())) << std::endl;
273
274 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try decode..." << std::endl;
275
276 // y is omitted
277 msg_in.clear_y();
278 decode_check(std::string(bytes.begin(), bytes.end()));
279}
280
281void decode_check(const std::string& encoded)
282{
283 TestMsg msg_out;
284 codec.decode(encoded, &msg_out);
285
286 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got Message out:\n" << msg_out.DebugString() << std::endl;
287
288 assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
289
290 msg_in.Clear();
291}
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
Definition codec.h:61
bool is(logger::Verbosity verbosity, logger::Group group=logger::GENERAL)
Indicates the verbosity of the Logger until the next std::flush or std::endl. The boolean return is u...
Definition logger.h:191
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:213
Unit test namespace.
Definition test.cpp:45
Dynamic Compact Control Language namespace.
Definition any.h:28
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:95