DCCL v4
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 // Davide Fenucci <davfen@noc.ac.uk>
8 //
9 //
10 // This file is part of the Dynamic Compact Control Language Library
11 // ("DCCL").
12 //
13 // DCCL is free software: you can redistribute it and/or modify
14 // it under the terms of the GNU Lesser General Public License as published by
15 // the Free Software Foundation, either version 2.1 of the License, or
16 // (at your option) any later version.
17 //
18 // DCCL is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU Lesser General Public License for more details.
22 //
23 // You should have received a copy of the GNU Lesser General Public License
24 // along with DCCL. If not, see <http://www.gnu.org/licenses/>.
25 // tests bounds on DefaultNumericFieldCodec
26 
27 #include "../../codec.h"
28 #include "test.pb.h"
29 using namespace dccl::test;
30 
31 int main(int /*argc*/, char* /*argv*/ [])
32 {
33  dccl::dlog.connect(dccl::logger::ALL, &std::cerr);
34 
35  dccl::Codec codec;
36 
37  codec.load<NumericMsg>();
38  codec.info<NumericMsg>(&dccl::dlog);
39 
40  try
41  {
42  codec.load<NegativeResolutionNumericMsg>();
43  bool message_should_fail_load = false;
44  assert(message_should_fail_load);
45  }
46  catch (dccl::Exception& e)
47  {
48  std::cout
49  << "** Note: this error is expected during proper execution of this unit test **: "
50  "Field a failed validation: (dccl.field).resolution must be greater than 0."
51  << std::endl;
52  }
53 
54  try
55  {
56  codec.load<BothResolutionAndPrecisionSetNumericMsg>();
57  bool message_should_fail_load = false;
58  assert(message_should_fail_load);
59  }
60  catch (dccl::Exception& e)
61  {
62  std::cout << "** Note: this error is expected during proper execution of this unit test "
63  "**: Field a failed validation: at most one of either (dccl.field).precision "
64  "or (dccl.field).resolution can be set."
65  << std::endl;
66  }
67 
68  try
69  {
70  codec.load<TooBigNumericMsg>();
71  bool message_should_fail_load = false;
72  assert(message_should_fail_load);
73  }
74  catch (dccl::Exception& e)
75  {
76  std::cout << "** Note: this error is expected during proper execution of this unit test "
77  "**: Field a failed validation: "
78  "[(dccl.field).max-(dccl.field).min]/(dccl.field).resolution must fit in a "
79  "double-precision floating point value. Please increase min, decrease max, or "
80  "decrease precision."
81  << std::endl;
82  }
83 
84  try
85  {
86  codec.load<MinNotMultipleOfResolution>();
87  bool message_should_fail_load = false;
88  assert(message_should_fail_load);
89  }
90  catch (dccl::Exception& e)
91  {
92  std::cout << "** Note: this error is expected during proper execution of this unit test "
93  "**: Field a failed validation: (dccl.field).min must be an exact multiple of "
94  "(dccl.field).resolution."
95  << std::endl;
96  }
97 
98  try
99  {
100  codec.load<MaxNotMultipleOfResolution>();
101  bool message_should_fail_load = false;
102  assert(message_should_fail_load);
103  }
104  catch (dccl::Exception& e)
105  {
106  std::cout << "** Note: this error is expected during proper execution of this unit test "
107  "**: Field a failed validation: (dccl.field).max must be an exact multiple of "
108  "(dccl.field).resolution."
109  << std::endl;
110  }
111 
112  NumericMsg msg_in;
113 
114  msg_in.set_a(10.12345678);
115  msg_in.set_b(11.42106);
116  msg_in.set_u1(18446744073709500000ull);
117  msg_in.set_u2(0);
118  msg_in.set_u3(10.2);
119  msg_in.set_u4(5.6);
120  msg_in.set_u5(1.95);
121  msg_in.set_u6(25500);
122 
123  std::string encoded;
124  codec.encode(&encoded, msg_in);
125 
126  NumericMsg msg_out;
127  codec.decode(encoded, &msg_out);
128 
129  msg_in.set_b(11.4211);
130  msg_in.set_u3(10.0);
131  msg_in.set_u4(6);
132  msg_in.set_u5(1.92);
133  assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
134 
135  std::cout << "all tests passed" << std::endl;
136 }
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