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