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 all protobuf types with _default codecs, repeat and non repeat
26 
27 #include <fstream>
28 
29 #include <google/protobuf/descriptor.pb.h>
30 
31 #include "../../codec.h"
32 #include "../../codecs3/field_codec_default.h"
33 
34 #include "../../binary.h"
35 #include "test.pb.h"
36 using namespace dccl::test;
37 
38 template <typename Msg> void check(double val, bool should_pass);
39 dccl::Codec codec;
40 TestMsg msg_in;
41 TestMsgGroup msg_group_in;
42 
43 namespace dccl
44 {
45 namespace test
46 {
48 {
49  double max() override { return 100; }
50  double min() override { return -100; }
51  double precision() override { return 1; } // Deprecated
52  double resolution() override { return 0.1; }
53  void validate() override {}
54 };
55 } // namespace test
56 } // namespace dccl
57 
58 int main(int /*argc*/, char* /*argv*/ [])
59 {
60  // dccl::dlog.connect(dccl::logger::ALL, &std::cerr);
61 
62  codec.manager().add<dccl::test::TestCodec>("test.grouptest");
63  codec.manager()
64  .add<dccl::v3::DefaultMessageCodec, google::protobuf::FieldDescriptor::TYPE_MESSAGE>(
65  "test.grouptest");
66 
67  check<TestMsg>(50, true);
68  check<TestMsg>(-50, false);
69  check<TestMsgGroup>(50, true);
70  check<TestMsgGroup>(-50, true);
71  check<TestMsgVersion>(50, true);
72 
73  std::cout << "all tests passed" << std::endl;
74 }
75 
76 template <typename Msg> void check(double val, bool should_pass)
77 {
78  Msg msg_in;
79  int i = 0;
80  msg_in.set_d(++i + 0.1);
81  msg_in.add_d_repeat(12.1);
82  msg_in.add_d_repeat(12.2);
83  msg_in.add_d_repeat(12.3);
84 
85  msg_in.mutable_msg()->set_val(val);
86  msg_in.mutable_msg()->mutable_msg()->set_val(val);
87  codec.info(msg_in.GetDescriptor(), &std::cout);
88 
89  std::cout << "Message in:\n" << msg_in.DebugString() << std::endl;
90 
91  codec.load(msg_in.GetDescriptor());
92 
93  std::cout << "Try encode..." << std::endl;
94  std::string bytes;
95  codec.encode(&bytes, msg_in);
96  std::cout << "... got bytes (hex): " << dccl::hex_encode(bytes) << std::endl;
97 
98  std::cout << "Try decode..." << std::endl;
99 
100  Msg msg_out;
101  codec.decode(bytes, &msg_out);
102 
103  std::cout << "... got Message out:\n" << msg_out.DebugString() << std::endl;
104 
105  if (should_pass)
106  assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
107  else
108  assert(msg_in.SerializeAsString() != msg_out.SerializeAsString());
109 }
dccl::test
Unit test namespace.
Definition: test.cpp:45
dccl
Dynamic Compact Control Language namespace.
Definition: any.h:49
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::hex_encode
void hex_encode(CharIterator begin, CharIterator end, std::string *out, bool upper_case=false)
Encodes a (little-endian) hexadecimal string from a byte string. Index 0 of begin is written to index...
Definition: binary.h:100
dccl::test::TestCodec
Definition: test.cpp:47
dccl::v2::DefaultNumericFieldCodec
Provides a basic bounded arbitrary length numeric (double, float, uint32, uint64, int32,...
Definition: field_codec_default.h:54
dccl::v3::DefaultMessageCodec
Provides the default codec for encoding a base Google Protobuf message or an embedded message by call...
Definition: field_codec_default_message.h:38