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// 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"
29using namespace dccl::test;
30
31int main(int argc, char* argv[])
32{
33 bool verbose = false;
34 for (int i = 1; i < argc; ++i)
35 {
36 if (argv[i] && argv[i][0] == '-' && argv[i][1] == 'v' && argv[i][2] == '\0')
37 verbose = true;
38 }
39
40 dccl::dlog.connect(verbose ? dccl::logger::ALL : dccl::logger::WARN_PLUS, &std::cerr);
41
42 dccl::Codec codec;
43
44 codec.load<NumericMsg>();
45 codec.info<NumericMsg>(&dccl::dlog);
46
47 try
48 {
49 codec.load<NegativeResolutionNumericMsg>();
50 bool message_should_fail_load = false;
51 assert(message_should_fail_load);
52 }
53 catch (dccl::Exception& e)
54 {
55 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog
56 << "** Note: this error is expected during proper execution of this unit test **: "
57 "Field a failed validation: (dccl.field).resolution must be greater than 0."
58 << std::endl;
59 }
60
61 try
62 {
63 codec.load<BothResolutionAndPrecisionSetNumericMsg>();
64 bool message_should_fail_load = false;
65 assert(message_should_fail_load);
66 }
67 catch (dccl::Exception& e)
68 {
69 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "** Note: this error is expected during proper execution of this unit test "
70 "**: Field a failed validation: at most one of either (dccl.field).precision "
71 "or (dccl.field).resolution can be set."
72 << std::endl;
73 }
74
75 try
76 {
77 codec.load<TooBigNumericMsg>();
78 bool message_should_fail_load = false;
79 assert(message_should_fail_load);
80 }
81 catch (dccl::Exception& e)
82 {
83 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "** Note: this error is expected during proper execution of this unit test "
84 "**: Field a failed validation: "
85 "[(dccl.field).max-(dccl.field).min]/(dccl.field).resolution must fit in a "
86 "double-precision floating point value. Please increase min, decrease max, or "
87 "decrease precision."
88 << std::endl;
89 }
90
91 try
92 {
93 codec.load<MinNotMultipleOfResolution>();
94 bool message_should_fail_load = false;
95 assert(message_should_fail_load);
96 }
97 catch (dccl::Exception& e)
98 {
99 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "** Note: this error is expected during proper execution of this unit test "
100 "**: Field a failed validation: (dccl.field).min must be an exact multiple of "
101 "(dccl.field).resolution."
102 << std::endl;
103 }
104
105 try
106 {
107 codec.load<MaxNotMultipleOfResolution>();
108 bool message_should_fail_load = false;
109 assert(message_should_fail_load);
110 }
111 catch (dccl::Exception& e)
112 {
113 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "** Note: this error is expected during proper execution of this unit test "
114 "**: Field a failed validation: (dccl.field).max must be an exact multiple of "
115 "(dccl.field).resolution."
116 << std::endl;
117 }
118
119 NumericMsg msg_in;
120
121 msg_in.set_a(10.12345678);
122 msg_in.set_b(11.42106);
123 msg_in.set_u1(18446744073709500000ull);
124 msg_in.set_u2(0);
125 msg_in.set_u3(10.2);
126 msg_in.set_u4(5.6);
127 msg_in.set_u5(1.95);
128 msg_in.set_u6(25500);
129
130 std::string encoded;
131 codec.encode(&encoded, msg_in);
132
133 NumericMsg msg_out;
134 codec.decode(encoded, &msg_out);
135
136 msg_in.set_b(11.4211);
137 msg_in.set_u3(10.0);
138 msg_in.set_u4(6);
139 msg_in.set_u5(1.92);
140 assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
141
142 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "all tests passed" << std::endl;
143}
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