DCCL v4
test.cpp
1 // Copyright 2019-2023:
2 // GobySoft, LLC (2013-)
3 // Community contributors (see AUTHORS file)
4 // File authors:
5 // Kyle Guilbert <kguilbert@aphysci.com>
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 required versus optional encoding of fields using a presence bit
25 
26 #include "../../codec.h"
27 #include "test/dccl_presence/test.pb.h"
28 using namespace dccl::test;
29 
30 void test1(dccl::Codec&);
31 void test2(dccl::Codec&);
32 
33 int main(int /*argc*/, char* /*argv*/[])
34 {
35  dccl::dlog.connect(dccl::logger::ALL, &std::cerr);
36 
37  dccl::Codec codec;
38  codec.load<PresenceMsg>();
39  codec.info<PresenceMsg>(&dccl::dlog);
40  test1(codec);
41  test2(codec);
42 
43  std::cout << "all tests passed" << std::endl;
44 }
45 
46 // optional fields all left empty
47 void test1(dccl::Codec& codec)
48 {
49  PresenceMsg msg_in;
50 
51  msg_in.set_req_i32(-100);
52  msg_in.set_req_i64(65535);
53  msg_in.set_req_ui32(1022);
54  msg_in.set_req_ui64(101);
55  msg_in.set_req_float(900.12345);
56  msg_in.set_req_double(900.12345678);
57  msg_in.set_req_enum(ENUM2_C);
58 
59  std::string encoded;
60  codec.encode(&encoded, msg_in);
61 
62  assert(encoded.size() == 17);
63 
64  PresenceMsg msg_out;
65  codec.decode(encoded, &msg_out);
66 
67  assert(msg_in.req_i32() == msg_out.req_i32());
68  assert(msg_in.req_i64() == msg_out.req_i64());
69  assert(msg_in.req_ui32() == msg_out.req_ui32());
70  assert(msg_in.req_ui64() == msg_out.req_ui64());
71  assert(fabsf(msg_in.req_float() - msg_out.req_float()) < 1e-4);
72  assert(fabs(msg_in.req_double() - msg_out.req_double()) < 1e-7);
73  assert(msg_in.req_enum() == msg_out.req_enum());
74 
75  assert(!msg_out.has_opt_i32());
76  assert(!msg_out.has_opt_i64());
77  assert(!msg_out.has_opt_ui32());
78  assert(!msg_out.has_opt_ui64());
79  assert(!msg_out.has_opt_float());
80  assert(!msg_out.has_opt_double());
81  assert(!msg_out.has_opt_enum());
82 
83  assert(msg_out.repeat_i32_size() == 0);
84  assert(msg_out.repeat_enum_size() == 0);
85 
86  std::cout << "test1 passed" << std::endl;
87 }
88 
89 // all fields populated
90 void test2(dccl::Codec& codec)
91 {
92  PresenceMsg msg_in;
93 
94  msg_in.set_req_i32(500);
95  msg_in.set_req_i64(0);
96  msg_in.set_req_ui32(0);
97  msg_in.set_req_ui64(100);
98  msg_in.set_req_float(-900.12345);
99  msg_in.set_req_double(-900.12345678);
100  msg_in.set_req_enum(ENUM2_A);
101 
102  msg_in.set_opt_i32(-100);
103  msg_in.set_opt_i64(65535);
104  msg_in.set_opt_ui32(0);
105  msg_in.set_opt_ui64(1123);
106  msg_in.set_opt_float(-900.12345);
107  msg_in.set_opt_double(900.12345678);
108  msg_in.set_opt_enum(ENUM2_A);
109  msg_in.set_opt_bool(true);
110  msg_in.set_opt_str("ABCDE");
111  msg_in.set_opt_bytes("XY");
112 
113  msg_in.add_repeat_i32(500);
114  msg_in.add_repeat_enum(ENUM2_A);
115  msg_in.add_repeat_enum(ENUM2_B);
116  msg_in.add_repeat_enum(ENUM2_C);
117 
118  std::string encoded;
119  codec.encode(&encoded, msg_in);
120 
121  assert(encoded.size() == 41);
122 
123  PresenceMsg msg_out;
124  codec.decode(encoded, &msg_out);
125 
126  assert(msg_in.req_i32() == msg_out.req_i32());
127  assert(msg_in.req_i64() == msg_out.req_i64());
128  assert(msg_in.req_ui32() == msg_out.req_ui32());
129  assert(msg_in.req_ui64() == msg_out.req_ui64());
130  assert(fabsf(msg_in.req_float() - msg_out.req_float()) < 1e-4);
131  assert(fabs(msg_in.req_double() - msg_out.req_double()) < 1e-7);
132  assert(msg_in.req_enum() == msg_out.req_enum());
133 
134  assert(msg_out.has_opt_i32());
135  assert(msg_out.has_opt_i64());
136  assert(msg_out.has_opt_ui32());
137  assert(msg_out.has_opt_ui64());
138  assert(msg_out.has_opt_float());
139  assert(msg_out.has_opt_double());
140  assert(msg_out.has_opt_enum());
141 
142  assert(msg_in.opt_i32() == msg_out.opt_i32());
143  assert(msg_in.opt_i64() == msg_out.opt_i64());
144  assert(msg_in.opt_ui32() == msg_out.opt_ui32());
145  assert(msg_in.opt_ui64() == msg_out.opt_ui64());
146  assert(fabsf(msg_in.opt_float() - msg_out.opt_float()) < 1e-4);
147  assert(fabs(msg_in.opt_double() - msg_out.opt_double()) < 1e-7);
148  assert(msg_in.opt_enum() == msg_out.opt_enum());
149 
150  assert(msg_in.repeat_i32_size() == msg_out.repeat_i32_size());
151  assert(std::equal(msg_in.repeat_i32().begin(), msg_in.repeat_i32().end(),
152  msg_out.repeat_i32().begin()));
153  assert(msg_in.repeat_enum_size() == msg_out.repeat_enum_size());
154  assert(std::equal(msg_in.repeat_enum().begin(), msg_in.repeat_enum().end(),
155  msg_out.repeat_enum().begin()));
156 
157  std::cout << "test2 passed" << std::endl;
158 }
dccl::test
Unit test namespace.
Definition: test.cpp:44
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