DCCL v4
test.cpp
1 // Copyright 2014-2023:
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 //
8 //
9 // This file is part of the Dynamic Compact Control Language Library
10 // ("DCCL").
11 //
12 // DCCL is free software: you can redistribute it and/or modify
13 // it under the terms of the GNU Lesser General Public License as published by
14 // the Free Software Foundation, either version 2.1 of the License, or
15 // (at your option) any later version.
16 //
17 // DCCL is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public License
23 // along with DCCL. If not, see <http://www.gnu.org/licenses/>.
24 // tests all protobuf types with _default codecs, repeat and non repeat
25 
26 #include <fstream>
27 
28 #include <google/protobuf/descriptor.pb.h>
29 
30 #include "../../codec.h"
31 #include "../../codecs2/field_codec_default.h"
32 
33 #include "../../binary.h"
34 #include "test.pb.h"
35 
36 using namespace dccl::test;
37 
38 int main(int /*argc*/, char* /*argv*/ [])
39 {
40  // dccl::dlog.connect(dccl::logger::ALL, &std::cerr);
41  // check the empty messages
42  dccl::Codec codec;
43 
44  codec.info<TestMsg>(&std::cout);
45  codec.load<TestMsg>();
46 
47  {
48  TestMsg msg_in, msg_out;
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  // check partially full messages
73  {
74  TestMsg msg_in, msg_out;
75 
76  msg_in.mutable_msg1()->set_val(0.1);
77  msg_in.add_msg1_repeat()->set_val(0.11);
78  msg_in.add_msg1_repeat()->set_val(0.12);
79  msg_in.add_msg1_repeat()->set_val(0.13);
80  msg_in.mutable_msg2()->set_val(0.2);
81  msg_in.add_msg2_repeat()->set_val(0.21);
82  msg_in.add_msg2_repeat()->set_val(0.22);
83  msg_in.add_msg2_repeat()->set_val(0.23);
84 
85  std::cout << "Message in:\n" << msg_in.DebugString() << std::endl;
86 
87  std::cout << "Try encode..." << std::endl;
88  std::string bytes;
89  codec.encode(&bytes, msg_in);
90  std::cout << "... got bytes (hex): " << dccl::hex_encode(bytes) << std::endl;
91 
92  std::cout << "Try decode..." << std::endl;
93 
94  codec.decode(bytes, &msg_out);
95 
96  std::cout << "... got Message out:\n" << msg_out.DebugString() << std::endl;
97 
98  assert(msg_out.has_msg1());
99  assert(msg_out.msg1_repeat_size() == 3);
100  assert(msg_out.has_msg2());
101  assert(msg_out.msg2_repeat_size() == 3);
102  assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
103  }
104 
105  // check message with set submessage but not children
106  {
107  TestMsg msg_in, msg_out;
108 
109  msg_in.mutable_msg1();
110  msg_in.mutable_msg2()->set_val(1);
111  std::cout << "Message in:\n" << msg_in.DebugString() << std::endl;
112 
113  std::cout << "Try encode..." << std::endl;
114  std::string bytes;
115  codec.encode(&bytes, msg_in);
116  std::cout << "... got bytes (hex): " << dccl::hex_encode(bytes) << std::endl;
117 
118  std::cout << "Try decode..." << std::endl;
119 
120  codec.decode(bytes, &msg_out);
121 
122  std::cout << "... got Message out:\n" << msg_out.DebugString() << std::endl;
123 
124  assert(msg_out.has_msg1());
125  assert(msg_out.has_msg2());
126  assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
127  }
128 
129  std::cout << "all tests passed" << std::endl;
130 }
dccl::test
Unit test namespace.
Definition: test.cpp:44
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