DCCL v4
test.cpp
1 // Copyright 2019:
2 // GobySoft, LLC (2013-)
3 // Community contributors (see AUTHORS file)
4 // File authors:
5 // Kyle Guilbert <kguilbert@aphysci.com>
6 //
7 //
8 // This file is part of the Dynamic Compact Control Language Library
9 // ("DCCL").
10 //
11 // DCCL is free software: you can redistribute it and/or modify
12 // it under the terms of the GNU Lesser General Public License as published by
13 // the Free Software Foundation, either version 2.1 of the License, or
14 // (at your option) any later version.
15 //
16 // DCCL is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU Lesser General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public License
22 // along with DCCL. If not, see <http://www.gnu.org/licenses/>.
23 // tests required versus optional encoding of fields using a presence bit
24 
25 #include "../../codec.h"
26 #include "test/dccl_presence/test.pb.h"
27 using namespace dccl::test;
28 
29 void test1(dccl::Codec&);
30 void test2(dccl::Codec&);
31 
32 int main(int /*argc*/, char* /*argv*/[])
33 {
34  dccl::dlog.connect(dccl::logger::ALL, &std::cerr);
35 
36  dccl::Codec codec;
37  codec.load<PresenceMsg>();
38  codec.info<PresenceMsg>(&dccl::dlog);
39  test1(codec);
40  test2(codec);
41 
42  std::cout << "all tests passed" << std::endl;
43 }
44 
45 // optional fields all left empty
46 void test1(dccl::Codec& codec)
47 {
48  PresenceMsg msg_in;
49 
50  msg_in.set_req_i32(-100);
51  msg_in.set_req_i64(65535);
52  msg_in.set_req_ui32(1022);
53  msg_in.set_req_ui64(101);
54  msg_in.set_req_float(900.12345);
55  msg_in.set_req_double(900.12345678);
56  msg_in.set_req_enum(ENUM2_C);
57 
58  std::string encoded;
59  codec.encode(&encoded, msg_in);
60 
61  assert(encoded.size() == 17);
62 
63  PresenceMsg msg_out;
64  codec.decode(encoded, &msg_out);
65 
66  assert(msg_in.req_i32() == msg_out.req_i32());
67  assert(msg_in.req_i64() == msg_out.req_i64());
68  assert(msg_in.req_ui32() == msg_out.req_ui32());
69  assert(msg_in.req_ui64() == msg_out.req_ui64());
70  assert(fabsf(msg_in.req_float() - msg_out.req_float()) < 1e-4);
71  assert(fabs(msg_in.req_double() - msg_out.req_double()) < 1e-7);
72  assert(msg_in.req_enum() == msg_out.req_enum());
73 
74  assert(!msg_out.has_opt_i32());
75  assert(!msg_out.has_opt_i64());
76  assert(!msg_out.has_opt_ui32());
77  assert(!msg_out.has_opt_ui64());
78  assert(!msg_out.has_opt_float());
79  assert(!msg_out.has_opt_double());
80  assert(!msg_out.has_opt_enum());
81 
82  assert(msg_out.repeat_i32_size() == 0);
83  assert(msg_out.repeat_enum_size() == 0);
84 
85  std::cout << "test1 passed" << std::endl;
86 }
87 
88 // all fields populated
89 void test2(dccl::Codec& codec)
90 {
91  PresenceMsg msg_in;
92 
93  msg_in.set_req_i32(500);
94  msg_in.set_req_i64(0);
95  msg_in.set_req_ui32(0);
96  msg_in.set_req_ui64(100);
97  msg_in.set_req_float(-900.12345);
98  msg_in.set_req_double(-900.12345678);
99  msg_in.set_req_enum(ENUM2_A);
100 
101  msg_in.set_opt_i32(-100);
102  msg_in.set_opt_i64(65535);
103  msg_in.set_opt_ui32(0);
104  msg_in.set_opt_ui64(1123);
105  msg_in.set_opt_float(-900.12345);
106  msg_in.set_opt_double(900.12345678);
107  msg_in.set_opt_enum(ENUM2_A);
108  msg_in.set_opt_bool(true);
109  msg_in.set_opt_str("ABCDE");
110  msg_in.set_opt_bytes("XY");
111 
112  msg_in.add_repeat_i32(500);
113  msg_in.add_repeat_enum(ENUM2_A);
114  msg_in.add_repeat_enum(ENUM2_B);
115  msg_in.add_repeat_enum(ENUM2_C);
116 
117  std::string encoded;
118  codec.encode(&encoded, msg_in);
119 
120  assert(encoded.size() == 41);
121 
122  PresenceMsg msg_out;
123  codec.decode(encoded, &msg_out);
124 
125  assert(msg_in.req_i32() == msg_out.req_i32());
126  assert(msg_in.req_i64() == msg_out.req_i64());
127  assert(msg_in.req_ui32() == msg_out.req_ui32());
128  assert(msg_in.req_ui64() == msg_out.req_ui64());
129  assert(fabsf(msg_in.req_float() - msg_out.req_float()) < 1e-4);
130  assert(fabs(msg_in.req_double() - msg_out.req_double()) < 1e-7);
131  assert(msg_in.req_enum() == msg_out.req_enum());
132 
133  assert(msg_out.has_opt_i32());
134  assert(msg_out.has_opt_i64());
135  assert(msg_out.has_opt_ui32());
136  assert(msg_out.has_opt_ui64());
137  assert(msg_out.has_opt_float());
138  assert(msg_out.has_opt_double());
139  assert(msg_out.has_opt_enum());
140 
141  assert(msg_in.opt_i32() == msg_out.opt_i32());
142  assert(msg_in.opt_i64() == msg_out.opt_i64());
143  assert(msg_in.opt_ui32() == msg_out.opt_ui32());
144  assert(msg_in.opt_ui64() == msg_out.opt_ui64());
145  assert(fabsf(msg_in.opt_float() - msg_out.opt_float()) < 1e-4);
146  assert(fabs(msg_in.opt_double() - msg_out.opt_double()) < 1e-7);
147  assert(msg_in.opt_enum() == msg_out.opt_enum());
148 
149  assert(msg_in.repeat_i32_size() == msg_out.repeat_i32_size());
150  assert(std::equal(msg_in.repeat_i32().begin(), msg_in.repeat_i32().end(),
151  msg_out.repeat_i32().begin()));
152  assert(msg_in.repeat_enum_size() == msg_out.repeat_enum_size());
153  assert(std::equal(msg_in.repeat_enum().begin(), msg_in.repeat_enum().end(),
154  msg_out.repeat_enum().begin()));
155 
156  std::cout << "test2 passed" << std::endl;
157 }
dccl::test
Unit test namespace.
Definition: test.cpp:45
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