DCCL v3
test.cpp
1 // Copyright 2009-2017 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (for 2013-)
3 // Massachusetts Institute of Technology (for 2007-2014)
4 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Dynamic Compact Control Language Library
8 // ("DCCL").
9 //
10 // DCCL is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // DCCL is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with DCCL. If not, see <http://www.gnu.org/licenses/>.
22 // tests bounds on DefaultNumericFieldCodec
23 
24 #include "dccl/codec.h"
25 #include "test.pb.h"
26 using namespace dccl::test;
27 
28 using dccl::operator<<;
29 
30 int 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 **: Field a failed validation: [(dccl.field).max-(dccl.field).min]*10^(dccl.field).precision must fit in a double-precision floating point value. Please increase min, decrease max, or decrease precision." << std::endl;
48  }
49 
50 
51  NumericMsg msg_in;
52 
53  msg_in.set_a(10.12345678);
54  msg_in.set_b(11.42106);
55  msg_in.set_u1(18446744073709500000ull);
56  msg_in.set_u2(0);
57 
58  std::string encoded;
59  codec.encode(&encoded, msg_in);
60 
61  NumericMsg msg_out;
62  codec.decode(encoded, &msg_out);
63 
64  msg_in.set_b(11.4211);
65  assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
66 
67  // Check negative precision encoding
68  const int NUM_TESTS = 8;
69  int test_values[NUM_TESTS][4] = {
70  // a_set, a_result, b_set, b_result
71  {20, 20, -500000, -500000},
72  {0, 0, 254000, 254000},
73  {10, 10, -257000, -257000},
74  {-10, -10, -499000, -499000},
75  {-20, -20, 500000, 500000},
76  {-19, -20, 499999, 500000},
77  {6, 10, -123400, -123000},
78  {0, 0, 0, 0},
79  };
80  for (int i=0; i < NUM_TESTS; ++i) {
81  NegativePrecisionNumericMsg msg_in_neg, msg_out_neg;
82  std::string enc;
83  msg_in_neg.set_a(test_values[i][0]);
84  msg_in_neg.set_b(test_values[i][2]);
85 
86  codec.encode(&enc, msg_in_neg);
87  codec.decode(enc, &msg_out_neg);
88 
89  std::cout << "msg_in: " << msg_in_neg.ShortDebugString() << std::endl;
90  std::cout << "msg_out: " << msg_out_neg.ShortDebugString() << std::endl;
91 
92  assert(msg_out_neg.a() == test_values[i][1]);
93  assert(msg_out_neg.b() == test_values[i][3]);
94  }
95 
96  std::cout << "all tests passed" << std::endl;
97 }
98 
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
Definition: codec.h:56
void load()
All messages must be explicited loaded and validated (size checks, option extensions checks...
Definition: codec.h:96
void info(std::ostream *os=0) const
Writes a human readable summary (including field sizes) of the provided DCCL type to the stream provi...
Definition: codec.h:139
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:161
Exception class for DCCL.
Definition: exception.h:30
Unit test namespace.
Definition: test.pb.h:36
CharIterator decode(CharIterator begin, CharIterator end, google::protobuf::Message *msg, bool header_only=false)
Decode a DCCL message when the type is known at compile time.
Definition: codec.h:405
void encode(std::string *bytes, const google::protobuf::Message &msg, bool header_only=false)
Encodes a DCCL message.
Definition: codec.cpp:266