DCCL v4
test.cpp
1 // Copyright 2011-2022:
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<TooBigNumericMsg>();
43  bool message_should_fail_load = false;
44  assert(message_should_fail_load);
45  }
46  catch (dccl::Exception& e)
47  {
48  std::cout << "** Note: this error is expected during proper execution of this unit test "
49  "**: Field a failed validation: "
50  "[(dccl.field).max-(dccl.field).min]/(dccl.field).resolution must fit in a "
51  "double-precision floating point value. Please increase min, decrease max, or "
52  "decrease precision."
53  << std::endl;
54  }
55 
56  NumericMsg msg_in;
57 
58  msg_in.set_a(10.12345678);
59  msg_in.set_b(11.42106);
60  msg_in.set_u1(18446744073709500000ull);
61  msg_in.set_u2(0);
62 
63  std::string encoded;
64  codec.encode(&encoded, msg_in);
65 
66  NumericMsg msg_out;
67  codec.decode(encoded, &msg_out);
68 
69  msg_in.set_b(11.4211);
70  assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
71 
72  // Check negative precision encoding
73  const int NUM_TESTS = 8;
74  int test_values[NUM_TESTS][4] = {
75  // a_set, a_result, b_set, b_result
76  {20, 20, -500000, -500000}, {0, 0, 254000, 254000},
77  {10, 10, -257000, -257000}, {-10, -10, -499000, -499000},
78  {-20, -20, 500000, 500000}, {-19, -20, 499999, 500000},
79  {6, 10, -123400, -123000}, {0, 0, 0, 0},
80  };
81  for (auto& test_value : test_values)
82  {
83  NegativePrecisionNumericMsg msg_in_neg, msg_out_neg;
84  std::string enc;
85  msg_in_neg.set_a(test_value[0]);
86  msg_in_neg.set_b(test_value[2]);
87 
88  codec.encode(&enc, msg_in_neg);
89  codec.decode(enc, &msg_out_neg);
90 
91  std::cout << "msg_in: " << msg_in_neg.ShortDebugString() << std::endl;
92  std::cout << "msg_out: " << msg_out_neg.ShortDebugString() << std::endl;
93 
94  assert(msg_out_neg.a() == test_value[1]);
95  assert(msg_out_neg.b() == test_value[3]);
96  }
97 
98  std::cout << "all tests passed" << std::endl;
99 }
dccl::test
Unit test namespace.
Definition: test.cpp:45
dccl::Exception
Exception class for DCCL.
Definition: exception.h:47
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