DCCL v5
Loading...
Searching...
No Matches
test.cpp
1// Copyright 2023:
2// GobySoft, LLC (2013-)
3// Community contributors (see AUTHORS file)
4// File authors:
5// Toby Schneider <toby@gobysoft.org>
6//
7//
8// This file is part of the Dynamic Compact Control Language Library
9// ("DCCL").
10//
11// DCCL is free software: you can redistribute it and/or modify
12// it under the terms of the GNU Lesser General Public License as published by
13// the Free Software Foundation, either version 2.1 of the License, or
14// (at your option) any later version.
15//
16// DCCL is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU Lesser General Public License for more details.
20//
21// You should have received a copy of the GNU Lesser General Public License
22// along with DCCL. If not, see <http://www.gnu.org/licenses/>.
23// tests all protobuf types with _default codecs, repeat and non repeat
24
25#include <fstream>
26
27#include "../../native_protobuf/dccl_native_protobuf.h"
28
29#include "../../binary.h"
30#include "../../codec.h"
31#include "test.pb.h"
32using namespace dccl::test;
33
34dccl::Codec codec;
35
36template <typename Msg1, typename Msg2>
37
38std::pair<std::size_t, std::size_t> compute_hashes()
39{
40 std::size_t hash1 = codec.load<Msg1>();
41 codec.unload<Msg1>();
42 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << Msg1::descriptor()->full_name() << ": " << dccl::hash_as_string(hash1)
43 << std::endl;
44 std::size_t hash2 = codec.load<Msg2>();
45 codec.unload<Msg2>();
46 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << Msg2::descriptor()->full_name() << ": " << dccl::hash_as_string(hash2)
47 << std::endl;
48 return std::make_pair(hash1, hash2);
49}
50
51template <typename Msg1, typename Msg2> void expect_same()
52{
53 auto hashes = compute_hashes<Msg1, Msg2>();
54 assert(hashes.first == hashes.second);
55}
56
57template <typename Msg1, typename Msg2> void expect_different()
58{
59 auto hashes = compute_hashes<Msg1, Msg2>();
60 assert(hashes.first != hashes.second);
61}
62
63int main(int argc, char* argv[])
64{
65 bool verbose = false;
66 for (int i = 1; i < argc; ++i)
67 {
68 if (argv[i] && argv[i][0] == '-' && argv[i][1] == 'v' && argv[i][2] == '\0')
69 verbose = true;
70 }
71
72 dccl::dlog.connect(verbose ? dccl::logger::ALL : dccl::logger::WARN_PLUS, &std::cerr);
73
74 expect_same<TestMsg, TestMsgNoHashableChanges>();
75 expect_different<TestMsg, TestMsgNewID>();
76 expect_different<TestMsg, TestMsgNewEnum>();
77 expect_different<TestMsg, TestMsgNewBounds>();
78 expect_different<TestMsgV2, TestMsgV3>();
79 expect_different<TestMsgV3, TestMsgV4>();
80 expect_different<TestMsgV4, TestMsgV5>();
81
82 {
83 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "TestMsg desc: " << TestMsg::descriptor() << std::endl;
84
85 auto hash = codec.load<TestMsg>();
86 codec.info<TestMsg>();
87
88 TestMsg msg_in, msg_out;
89 msg_in.set_e(TestMsg::VALUE1);
90 msg_in.set_hash_req(0x1234); // dummy value - overwritten by dccl.hash codec
91
92 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Message in:\n" << msg_in.DebugString() << std::endl;
93 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try encode..." << std::endl;
94 std::string bytes;
95 codec.encode(&bytes, msg_in);
96 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got bytes (hex): " << dccl::hex_encode(bytes) << std::endl;
97
98 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try decode..." << std::endl;
99 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << codec.max_size(msg_in.GetDescriptor()) << std::endl;
100
101 codec.decode(bytes, &msg_out);
102
103 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got Message out:\n" << msg_out.DebugString() << std::endl;
104
105 msg_in.set_hash_opt(hash & 0xFFFF);
106 msg_in.set_hash_req(hash & 0xFFFFFFFF);
107 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << hash << std::endl;
108 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Message in (with hash):\n" << msg_in.DebugString() << std::endl;
109
110 assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
111 codec.unload<TestMsg>();
112 }
113
114 {
115 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "TestMsg desc: " << TestMsg::descriptor() << std::endl;
116
117 codec.load<TestMsg>();
118
119 TestMsg msg_in;
120 TestMsgNewEnum msg_out;
121
122 msg_in.set_e(TestMsg::VALUE1);
123 msg_in.set_hash_req(0x1234); // dummy value - overwritten by dccl.hash codec
124
125 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Message in:\n" << msg_in.DebugString() << std::endl;
126 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try encode..." << std::endl;
127 std::string bytes;
128 codec.encode(&bytes, msg_in);
129 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got bytes (hex): " << dccl::hex_encode(bytes) << std::endl;
130
131 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try decode..." << std::endl;
132 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << codec.max_size(msg_in.GetDescriptor()) << std::endl;
133
134 codec.unload<TestMsg>();
135 codec.load<TestMsgNewEnum>();
136
137 try
138 {
139 codec.decode(bytes, &msg_out);
140 assert(false);
141 }
142 catch (const std::exception& e)
143 {
144 // expecting exception
145 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Caught expected exception: " << e.what() << std::endl;
146 }
147 }
148
149 {
150 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "TestMsgMultiHash desc: " << TestMsgMultiHash::descriptor() << std::endl;
151
152 auto hash = codec.load<TestMsgMultiHash>();
153 codec.info<TestMsgMultiHash>();
154
155 TestMsgMultiHash msg_in, msg_out;
156
157 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Message in:\n" << msg_in.DebugString() << std::endl;
158 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try encode..." << std::endl;
159 std::string bytes;
160 codec.encode(&bytes, msg_in);
161 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got bytes (hex): " << dccl::hex_encode(bytes) << std::endl;
162
163 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try decode..." << std::endl;
164 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << codec.max_size(msg_in.GetDescriptor()) << std::endl;
165
166 codec.decode(bytes, &msg_out);
167
168 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got Message out:\n" << msg_out.DebugString() << std::endl;
169
170 msg_in.set_hash4(hash & ((1 << 4) - 1));
171 msg_in.set_hash6(hash & ((1 << 6) - 1));
172 msg_in.set_hash8(hash & ((1 << 8) - 1));
173 msg_in.set_hash13(hash & ((1 << 13) - 1));
174 msg_in.set_hash26(hash & ((1 << 26) - 1));
175 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << hash << std::endl;
176 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Message in (with hash):\n" << msg_in.DebugString() << std::endl;
177
178 assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
179 codec.unload<TestMsgMultiHash>();
180 }
181
182 try
183 {
184 codec.load<TestMsgHashMaxTooLarge>();
185 assert(false);
186 }
187 catch (const std::exception& e)
188 {
189 // expecting exception
190 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Caught expected exception: " << e.what() << std::endl;
191 }
192
193 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "All tests passed" << std::endl;
194}
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
Definition codec.h:61
bool is(logger::Verbosity verbosity, logger::Group group=logger::GENERAL)
Indicates the verbosity of the Logger until the next std::flush or std::endl. The boolean return is u...
Definition logger.h:191
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:213
Unit test namespace.
Definition test.cpp:45
Dynamic Compact Control Language namespace.
Definition any.h:28
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:95