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 "../../native_protobuf/dccl_native_protobuf.h"
30 #include <google/protobuf/descriptor.pb.h>
31 
32 #include "../../binary.h"
33 #include "../../codec.h"
34 #include "test.pb.h"
35 using namespace dccl::test;
36 
37 dccl::Codec codec;
38 
39 template <typename Msg1, typename Msg2>
40 
41 std::pair<std::size_t, std::size_t> compute_hashes()
42 {
43  std::size_t hash1 = codec.load<Msg1>();
44  codec.unload<Msg1>();
45  std::cout << Msg1::descriptor()->full_name() << ": " << dccl::hash_as_string(hash1)
46  << std::endl;
47  std::size_t hash2 = codec.load<Msg2>();
48  codec.unload<Msg2>();
49  std::cout << Msg2::descriptor()->full_name() << ": " << dccl::hash_as_string(hash2)
50  << std::endl;
51  return std::make_pair(hash1, hash2);
52 }
53 
54 template <typename Msg1, typename Msg2> void expect_same()
55 {
56  auto hashes = compute_hashes<Msg1, Msg2>();
57  assert(hashes.first == hashes.second);
58 }
59 
60 template <typename Msg1, typename Msg2> void expect_different()
61 {
62  auto hashes = compute_hashes<Msg1, Msg2>();
63  assert(hashes.first != hashes.second);
64 }
65 
66 int main(int /*argc*/, char* /*argv*/[])
67 {
68  dccl::dlog.connect(dccl::logger::ALL, &std::cerr);
69 
70  expect_same<TestMsg, TestMsgNoHashableChanges>();
71  expect_different<TestMsg, TestMsgNewID>();
72  expect_different<TestMsg, TestMsgNewEnum>();
73  expect_different<TestMsg, TestMsgNewBounds>();
74  expect_different<TestMsgV2, TestMsgV3>();
75  expect_different<TestMsgV3, TestMsgV4>();
76 
77  {
78  std::cout << "TestMsg desc: " << TestMsg::descriptor() << std::endl;
79 
80  auto hash = codec.load<TestMsg>();
81  codec.info<TestMsg>();
82 
83  TestMsg msg_in, msg_out;
84  msg_in.set_e(TestMsg::VALUE1);
85  msg_in.set_hash_req(0x1234); // dummy value - overwritten by dccl.hash codec
86 
87  std::cout << "Message in:\n" << msg_in.DebugString() << std::endl;
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  std::cout << codec.max_size(msg_in.GetDescriptor()) << std::endl;
95 
96  codec.decode(bytes, &msg_out);
97 
98  std::cout << "... got Message out:\n" << msg_out.DebugString() << std::endl;
99 
100  msg_in.set_hash_opt(hash & 0xFFFF);
101  msg_in.set_hash_req(hash & 0xFFFFFFFF);
102  std::cout << hash << std::endl;
103  std::cout << "Message in (with hash):\n" << msg_in.DebugString() << std::endl;
104 
105  assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
106  codec.unload<TestMsg>();
107  }
108 
109  {
110  std::cout << "TestMsg desc: " << TestMsg::descriptor() << std::endl;
111 
112  codec.load<TestMsg>();
113 
114  TestMsg msg_in;
115  TestMsgNewEnum msg_out;
116 
117  msg_in.set_e(TestMsg::VALUE1);
118  msg_in.set_hash_req(0x1234); // dummy value - overwritten by dccl.hash codec
119 
120  std::cout << "Message in:\n" << msg_in.DebugString() << std::endl;
121  std::cout << "Try encode..." << std::endl;
122  std::string bytes;
123  codec.encode(&bytes, msg_in);
124  std::cout << "... got bytes (hex): " << dccl::hex_encode(bytes) << std::endl;
125 
126  std::cout << "Try decode..." << std::endl;
127  std::cout << codec.max_size(msg_in.GetDescriptor()) << std::endl;
128 
129  codec.unload<TestMsg>();
130  codec.load<TestMsgNewEnum>();
131 
132  try
133  {
134  codec.decode(bytes, &msg_out);
135  assert(false);
136  }
137  catch (const std::exception& e)
138  {
139  // expecting exception
140  std::cout << "Caught expected exception: " << e.what() << std::endl;
141  }
142  }
143 
144  {
145  std::cout << "TestMsgMultiHash desc: " << TestMsgMultiHash::descriptor() << std::endl;
146 
147  auto hash = codec.load<TestMsgMultiHash>();
148  codec.info<TestMsgMultiHash>();
149 
150  TestMsgMultiHash msg_in, msg_out;
151 
152  std::cout << "Message in:\n" << msg_in.DebugString() << std::endl;
153  std::cout << "Try encode..." << std::endl;
154  std::string bytes;
155  codec.encode(&bytes, msg_in);
156  std::cout << "... got bytes (hex): " << dccl::hex_encode(bytes) << std::endl;
157 
158  std::cout << "Try decode..." << std::endl;
159  std::cout << codec.max_size(msg_in.GetDescriptor()) << std::endl;
160 
161  codec.decode(bytes, &msg_out);
162 
163  std::cout << "... got Message out:\n" << msg_out.DebugString() << std::endl;
164 
165  msg_in.set_hash4(hash & ((1 << 4) - 1));
166  msg_in.set_hash6(hash & ((1 << 6) - 1));
167  msg_in.set_hash8(hash & ((1 << 8) - 1));
168  msg_in.set_hash13(hash & ((1 << 13) - 1));
169  msg_in.set_hash26(hash & ((1 << 26) - 1));
170  std::cout << hash << std::endl;
171  std::cout << "Message in (with hash):\n" << msg_in.DebugString() << std::endl;
172 
173  assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
174  codec.unload<TestMsgMultiHash>();
175  }
176 
177  try
178  {
179  codec.load<TestMsgHashMaxTooLarge>();
180  assert(false);
181  }
182  catch (const std::exception& e)
183  {
184  // expecting exception
185  std::cout << "Caught expected exception: " << e.what() << std::endl;
186  }
187 
188  std::cout << "All tests passed" << std::endl;
189 }
dccl::test
Unit test namespace.
Definition: test.cpp:45
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::Logger::connect
void connect(int verbosity_mask, Slot slot)
Connect the output of one or more given verbosities to a slot (function pointer or similar)
Definition: logger.h:214
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