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 }
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 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:98
Dynamic Compact Control Language namespace.
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