DCCL v5
Loading...
Searching...
No Matches
test.cpp
1// Copyright 2019-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 usage of a custom DCCL ID codec
24
25#include "../../codec.h"
26#include "../../field_codec_id.h"
27#include "test.pb.h"
28using namespace dccl::test;
29
30
31namespace dccl
32{
33namespace test
34{
35// If "user_id" is set via UserCustomIdRAII, use that, fall back to using the
36// default ID Codec
38{
39 private:
40 dccl::Bitset encode(const dccl::uint32& wire_value) override
41 {
42 return user_id_set ? encode() : DefaultIdentifierCodec::encode(wire_value);
43 }
44
45 dccl::Bitset encode() override
46 {
47 return user_id_set ? dccl::Bitset() : DefaultIdentifierCodec::encode();
48 }
49
50 unsigned size() override { return user_id_set ? 0 : DefaultIdentifierCodec::size(); }
51
52 unsigned size(const dccl::uint32& wire_value) override
53 {
54 return user_id_set ? 0 : DefaultIdentifierCodec::size(wire_value);
55 }
56
57 unsigned min_size() override { return user_id_set ? 0 : DefaultIdentifierCodec::min_size(); }
58
59 unsigned max_size() override { return user_id_set ? 0 : DefaultIdentifierCodec::max_size(); }
60
61 // pass the current ID back
62 dccl::uint32 decode(dccl::Bitset* bits) override
63 {
64 return user_id_set ? user_id : DefaultIdentifierCodec::decode(bits);
65 }
66
67 void validate() override
68 {
69 if (!user_id_set)
71 }
72
73 friend struct UserCustomIdRAII;
74 static dccl::uint32 user_id;
75 static bool user_id_set;
76};
77
78// RAII-based tool for setting the current DCCL ID within the scope of this
79// object's lifetime. Any actions (load, info, encode, decode, etc.) will
80// use this DCCL ID for the duration of this lifetime.
82{
84 {
85 UserCustomIdCodec::user_id = id;
86 UserCustomIdCodec::user_id_set = true;
87 }
89 {
90 UserCustomIdCodec::user_id = 0;
91 UserCustomIdCodec::user_id_set = false;
92 }
93};
94} // namespace test
95} // namespace dccl
96
97bool dccl::test::UserCustomIdCodec::user_id_set = false;
98dccl::uint32 dccl::test::UserCustomIdCodec::user_id = 0;
99
100int main(int argc, char* argv[])
101{
102 bool verbose = false;
103 for (int i = 1; i < argc; ++i)
104 {
105 if (argv[i] && argv[i][0] == '-' && argv[i][1] == 'v' && argv[i][2] == '\0')
106 verbose = true;
107 }
108
109 dccl::dlog.connect(verbose ? dccl::logger::ALL : dccl::logger::WARN_PLUS, &std::cerr);
110
111 {
112 dccl::Codec codec("user_id_codec", dccl::test::UserCustomIdCodec());
113
114 // load TestMessageA as DCCL ID 1
115 {
116 dccl::test::UserCustomIdRAII scoped_user_id(1);
117 codec.load<TestMessageA>();
118 codec.info<TestMessageA>(&dccl::dlog);
119 }
120 // load TestMessageA as DCCL ID 2
121 {
122 dccl::test::UserCustomIdRAII scoped_user_id(2);
123 codec.load<TestMessageA>();
124 codec.info<TestMessageA>(&dccl::dlog);
125 }
126
127 // load TestMessageB as DCCL ID 3
128 {
129 dccl::test::UserCustomIdRAII scoped_user_id(3);
130 codec.load<TestMessageB>();
131 codec.info<TestMessageB>(&dccl::dlog);
132 }
133
134 // load TestMessageA using default ID codec
135 {
136 codec.load<TestMessageA>();
137 codec.info<TestMessageA>(&dccl::dlog);
138 }
139
140 // one byte as the UserCustomIdCodec doesn't use any bytes on the wire
141 unsigned byte_size_user_specified = 1;
142 TestMessageA a1;
143 a1.set_a(10);
144
145 TestMessageA a2;
146 a2.set_a(20);
147
148 TestMessageB b;
149 b.set_b(30);
150
151 // one byte for the data, one byte for the ID
152 unsigned byte_size_default = 2;
153 TestMessageA a_default;
154 a_default.set_a(40);
155
156 // encode/decode using user-specified DCCL ID
157 {
158 dccl::test::UserCustomIdRAII scoped_user_id(1);
159 std::string bytes;
160
161 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "A1: " << a1.ShortDebugString() << std::endl;
162 assert(codec.size(a1) == byte_size_user_specified);
163 codec.encode(&bytes, a1);
164 assert(bytes.size() == byte_size_user_specified);
165 TestMessageA a1_out;
166 codec.decode(bytes, &a1_out);
167 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "A1 decoded: " << a1_out.ShortDebugString() << std::endl;
168 assert(a1_out.SerializeAsString() == a1.SerializeAsString());
169 }
170
171 {
172 dccl::test::UserCustomIdRAII scoped_user_id(2);
173 std::string bytes;
174 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "A2: " << a2.ShortDebugString() << std::endl;
175 assert(codec.size(a2) == byte_size_user_specified);
176 codec.encode(&bytes, a2);
177 assert(bytes.size() == byte_size_user_specified);
178 TestMessageA a2_out;
179 codec.decode(bytes, &a2_out);
180 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "A2 decoded: " << a2_out.ShortDebugString() << std::endl;
181 assert(a2_out.SerializeAsString() == a2.SerializeAsString());
182 }
183
184 {
185 dccl::test::UserCustomIdRAII scoped_user_id(3);
186 std::string bytes;
187 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "B: " << b.ShortDebugString() << std::endl;
188 assert(codec.size(b) == byte_size_user_specified);
189 codec.encode(&bytes, b);
190 assert(bytes.size() == byte_size_user_specified);
191 TestMessageA b_out;
192 codec.decode(bytes, &b_out);
193 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "B decoded: " << b_out.ShortDebugString() << std::endl;
194 assert(b_out.SerializeAsString() == b.SerializeAsString());
195 }
196
197 // encode/decode using default DCCL ID
198 {
199 std::string bytes;
200
201 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "A Default: " << a_default.ShortDebugString() << std::endl;
202 assert(codec.size(a_default) == byte_size_default);
203 codec.encode(&bytes, a_default);
204 assert(bytes.size() == byte_size_default);
205 TestMessageA a_default_out;
206 codec.decode(bytes, &a_default_out);
207 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "A Default decoded: " << a_default_out.ShortDebugString() << std::endl;
208 assert(a_default_out.SerializeAsString() == a_default.SerializeAsString());
209 }
210 }
211
212 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "all tests passed" << std::endl;
213}
A variable size container of bits (subclassed from std::deque<bool>) with an optional hierarchy....
Definition bitset.h:43
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
Definition codec.h:61
Provides the default 1 byte or 2 byte DCCL ID codec.
Bitset encode() override
Encode an empty field.
unsigned size() override
Calculate the size (in bits) of an empty field.
void validate() override
Validate a field. Use require() inside your overloaded validate() to assert requirements or throw Exc...
uint32 decode(Bitset *bits) override
Decode a field. If the field is empty (i.e. was encoded using the zero-argument encode()),...
unsigned min_size() override
Calculate minimum size of the field in bits.
unsigned max_size() override
Calculate maximum size of the field in bits.
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
google::protobuf::uint32 uint32
an unsigned 32 bit integer
Definition common.h:56