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/codecs2/field_codec_default.h"
30 
31 #include "test.pb.h"
32 #include "dccl/binary.h"
33 
34 using namespace dccl::test;
35 
36 int main(int argc, char* argv[])
37 {
38 // dccl::dlog.connect(dccl::logger::ALL, &std::cerr);
39  // check the empty messages
40  dccl::Codec codec;
41 
42  codec.info<TestMsg>(&std::cout);
43  codec.load<TestMsg>();
44 
45  {
46  TestMsg msg_in, msg_out;
47  int i = 0;
48 
49 
50  std::cout << "Message in:\n" << msg_in.DebugString() << std::endl;
51 
52  codec.load(msg_in.GetDescriptor());
53 
54  std::cout << "Try encode..." << std::endl;
55  std::string bytes;
56  codec.encode(&bytes, msg_in);
57  std::cout << "... got bytes (hex): " << dccl::hex_encode(bytes) << std::endl;
58 
59  std::cout << "Try decode..." << std::endl;
60 
61  codec.decode(bytes, &msg_out);
62 
63  std::cout << "... got Message out:\n" << msg_out.DebugString() << std::endl;
64 
65  assert(!msg_out.has_msg1());
66  assert(!msg_out.msg1_repeat_size());
67  assert(!msg_out.has_msg2());
68  assert(!msg_out.msg2_repeat_size());
69  assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
70  }
71 
72 
73  // check partially full messages
74  {
75  TestMsg msg_in, msg_out;
76 
77  msg_in.mutable_msg1()->set_val(0.1);
78  msg_in.add_msg1_repeat()->set_val(0.11);
79  msg_in.add_msg1_repeat()->set_val(0.12);
80  msg_in.add_msg1_repeat()->set_val(0.13);
81  msg_in.mutable_msg2()->set_val(0.2);
82  msg_in.add_msg2_repeat()->set_val(0.21);
83  msg_in.add_msg2_repeat()->set_val(0.22);
84  msg_in.add_msg2_repeat()->set_val(0.23);
85 
86  std::cout << "Message in:\n" << msg_in.DebugString() << std::endl;
87 
88  std::cout << "Try encode..." << std::endl;
89  std::string bytes;
90  codec.encode(&bytes, msg_in);
91  std::cout << "... got bytes (hex): " << dccl::hex_encode(bytes) << std::endl;
92 
93  std::cout << "Try decode..." << std::endl;
94 
95  codec.decode(bytes, &msg_out);
96 
97  std::cout << "... got Message out:\n" << msg_out.DebugString() << std::endl;
98 
99  assert(msg_out.has_msg1());
100  assert(msg_out.msg1_repeat_size() == 3);
101  assert(msg_out.has_msg2());
102  assert(msg_out.msg2_repeat_size() == 3);
103  assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
104  }
105 
106 
107  std::cout << "all tests passed" << std::endl;
108 }
109 
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
Definition: codec.h:56
void info(std::ostream *os=0, int user_id=-1) const
Writes a human readable summary (including field sizes) of the provided DCCL type to the stream provi...
Definition: codec.h:155
void load()
All messages must be explicited loaded and validated (size checks, option extensions checks...
Definition: codec.h:96
void encode(std::string *bytes, const google::protobuf::Message &msg, bool header_only=false, int user_id=-1)
Encodes a DCCL message.
Definition: codec.cpp:273
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
Unit test namespace.
Definition: test.cpp:44
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:439