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