DCCL v5
Loading...
Searching...
No Matches
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 "../../native_protobuf/dccl_native_protobuf.h"
29#include <google/protobuf/descriptor.pb.h>
30
31#include "../../binary.h"
32#include "../../codec.h"
33#include "test.pb.h"
34using namespace dccl::test;
35
36dccl::Codec codec;
37
38int main(int argc, char* argv[])
39{
40 bool verbose = false;
41 for (int i = 1; i < argc; ++i)
42 {
43 if (argv[i] && argv[i][0] == '-' && argv[i][1] == 'v' && argv[i][2] == '\0')
44 verbose = true;
45 }
46
47 dccl::dlog.connect(verbose ? dccl::logger::ALL : dccl::logger::WARN_PLUS, &std::cerr);
48 codec.load<TestMsg>();
49 codec.info<TestMsg>();
50 {
51 TestMsg msg_in;
52 msg_in.add_a(0);
53 msg_in.add_a(1);
54 msg_in.add_a(2);
55
56 msg_in.add_b(10);
57 msg_in.add_b(11);
58
59 msg_in.add_c(20);
60 msg_in.add_c(21);
61 msg_in.add_c(22);
62
63 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Message in:\n" << msg_in.DebugString() << std::endl;
64 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try encode..." << std::endl;
65 std::string bytes;
66 codec.encode(&bytes, msg_in);
67 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got bytes (hex): " << dccl::hex_encode(bytes) << std::endl;
68
69 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try decode..." << std::endl;
70 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << codec.max_size(msg_in.GetDescriptor()) << std::endl;
71
72 TestMsg msg_out;
73 codec.decode(bytes, &msg_out);
74
75 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got Message out:\n" << msg_out.DebugString() << std::endl;
76 assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
77 }
78
79 {
80 TestMsg msg_in;
81 msg_in.add_a(0);
82 msg_in.add_a(1);
83 msg_in.add_a(2);
84 msg_in.add_a(3);
85 msg_in.add_a(4);
86 msg_in.add_a(5);
87
88 msg_in.add_b(10);
89 msg_in.add_b(11);
90
91 msg_in.add_c(20);
92 msg_in.add_c(21);
93 msg_in.add_c(22);
94
95 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Message in:\n" << msg_in.DebugString() << std::endl;
96 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try encode..." << std::endl;
97 std::string bytes;
98 codec.encode(&bytes, msg_in);
99 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got bytes (hex): " << dccl::hex_encode(bytes) << std::endl;
100
101 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try decode..." << std::endl;
102 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << codec.max_size(msg_in.GetDescriptor()) << std::endl;
103
104 TestMsg msg_out;
105 codec.decode(bytes, &msg_out);
106
107 msg_in.mutable_a()->RemoveLast();
108
109 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got Message out:\n" << msg_out.DebugString() << std::endl;
110 assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
111 }
112
113 {
114 TestMsg msg_in;
115 msg_in.add_a(0);
116 msg_in.add_a(1);
117 msg_in.add_a(2);
118 msg_in.add_a(3);
119 msg_in.add_a(4);
120
121 msg_in.add_c(20);
122 msg_in.add_c(21);
123 msg_in.add_c(22);
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 TestMsg msg_out;
135 codec.decode(bytes, &msg_out);
136
137 auto bmin = dccl::test::TestMsg::descriptor()
138 ->FindFieldByName("b")
139 ->options()
140 .GetExtension(dccl::field)
141 .min();
142 msg_in.add_b(bmin);
143 msg_in.add_b(bmin);
144
145 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got Message out:\n" << msg_out.DebugString() << std::endl;
146 assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
147 }
148
149 try
150 {
151 // should throw exception on missing max repeat
152 codec.load<InvalidTestMsgMissingMaxRepeat>();
153 assert(false);
154 }
155 catch (const dccl::Exception& e)
156 {
157 // expected
158 }
159
160 try
161 {
162 // should throw exception
163 codec.load<InvalidTestMsgMaxRepeatLessThanOne>();
164 assert(false);
165 }
166 catch (const dccl::Exception& e)
167 {
168 // expected
169 }
170
171 try
172 {
173 // should throw exception
174 codec.load<InvalidTestMsgMaxRepeatLessThanMinRepeat>();
175 assert(false);
176 }
177 catch (const dccl::Exception& e)
178 {
179 // expected
180 }
181
182 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "All tests passed." << std::endl;
183}
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
Definition codec.h:61
Exception class for DCCL.
Definition exception.h:47
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