DCCL v4
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 bounds on DefaultNumericFieldCodec
25
26#include "../../codec.h"
27#include "test.pb.h"
28using namespace dccl::test;
29
30int main(int /*argc*/, char* /*argv*/ [])
31{
32 dccl::dlog.connect(dccl::logger::ALL, &std::cerr);
33
34 dccl::Codec codec;
35
36 codec.load<NumericMsg>();
37 codec.info<NumericMsg>(&dccl::dlog);
38
39 try
40 {
41 codec.load<TooBigNumericMsg>();
42 bool message_should_fail_load = false;
43 assert(message_should_fail_load);
44 }
45 catch (dccl::Exception& e)
46 {
47 std::cout << "** Note: this error is expected during proper execution of this unit test "
48 "**: Field a failed validation: "
49 "[(dccl.field).max-(dccl.field).min]/(dccl.field).resolution must fit in a "
50 "double-precision floating point value. Please increase min, decrease max, or "
51 "decrease precision."
52 << std::endl;
53 }
54
55 NumericMsg msg_in;
56
57 msg_in.set_a(10.12345678);
58 msg_in.set_b(11.42106);
59 msg_in.set_u1(18446744073709500000ull);
60 msg_in.set_u2(0);
61
62 std::string encoded;
63 codec.encode(&encoded, msg_in);
64
65 NumericMsg msg_out;
66 codec.decode(encoded, &msg_out);
67
68 msg_in.set_b(11.4211);
69 assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
70
71 // Check negative precision encoding
72 const int NUM_TESTS = 8;
73 int test_values[NUM_TESTS][4] = {
74 // a_set, a_result, b_set, b_result
75 {20, 20, -500000, -500000}, {0, 0, 254000, 254000},
76 {10, 10, -257000, -257000}, {-10, -10, -499000, -499000},
77 {-20, -20, 500000, 500000}, {-19, -20, 499999, 500000},
78 {6, 10, -123400, -123000}, {0, 0, 0, 0},
79 };
80 for (auto& test_value : test_values)
81 {
82 NegativePrecisionNumericMsg msg_in_neg, msg_out_neg;
83 std::string enc;
84 msg_in_neg.set_a(test_value[0]);
85 msg_in_neg.set_b(test_value[2]);
86
87 codec.encode(&enc, msg_in_neg);
88 codec.decode(enc, &msg_out_neg);
89
90 std::cout << "msg_in: " << msg_in_neg.ShortDebugString() << std::endl;
91 std::cout << "msg_out: " << msg_out_neg.ShortDebugString() << std::endl;
92
93 assert(msg_out_neg.a() == test_value[1]);
94 assert(msg_out_neg.b() == test_value[3]);
95 }
96
97 std::cout << "all tests passed" << std::endl;
98}
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
Definition codec.h:63
Exception class for DCCL.
Definition exception.h:47
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
Unit test namespace.
Definition test.cpp:45