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
24// Tests proto3 syntax support via (dccl.field).required = true
25
26#include <cassert>
27#include <iostream>
28
29#include "../../codec.h"
30#include "test2.pb.h"
31#include "test3.pb.h"
32
33using namespace dccl::test;
34
35// Test 1: encode/decode with DCCL-required and DCCL-optional fields populated
36void test1(dccl::Codec& codec, dccl::Codec& codec2)
37{
38 Proto3Msg msg_in;
39 msg_in.set_req_i32(-50);
40 msg_in.set_req_ui32(512);
41 msg_in.set_opt_i32(200);
42 msg_in.set_opt_ui32(100);
43
44 std::string encoded;
45 codec.encode(&encoded, msg_in);
46
47 Proto3Msg msg_out;
48 codec.decode(encoded, &msg_out);
49
50 assert(msg_in.req_i32() == msg_out.req_i32());
51 assert(msg_in.req_ui32() == msg_out.req_ui32());
52 assert(msg_out.has_opt_i32());
53 assert(msg_in.opt_i32() == msg_out.opt_i32());
54 assert(msg_out.has_opt_ui32());
55 assert(msg_in.opt_ui32() == msg_out.opt_ui32());
56 assert(!msg_out.has_child());
57
58 Proto2Msg msg2_out;
59 codec2.decode(encoded, &msg2_out);
60
61 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Msg in: " << msg_in.ShortDebugString() << std::endl;
62 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Msg out: " << msg_out.ShortDebugString() << std::endl;
63 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Msg2 out: " << msg2_out.ShortDebugString() << std::endl;
64
65 assert(msg_in.req_i32() == msg2_out.req_i32());
66 assert(msg_in.req_ui32() == msg2_out.req_ui32());
67 assert(msg2_out.has_opt_i32());
68 assert(msg_in.opt_i32() == msg2_out.opt_i32());
69 assert(msg2_out.has_opt_ui32());
70 assert(msg_in.opt_ui32() == msg2_out.opt_ui32());
71 assert(!msg2_out.has_child());
72}
73
74// Test 2: encode/decode with optional fields left empty
75void test2(dccl::Codec& codec)
76{
77 Proto3Msg msg_in;
78 msg_in.set_req_i32(0);
79 msg_in.set_req_ui32(10);
80 // leave optional fields unset
81
82 std::string encoded;
83 codec.encode(&encoded, msg_in);
84
85 Proto3Msg msg_out;
86 codec.decode(encoded, &msg_out);
87
88 assert(msg_in.req_i32() == msg_out.req_i32());
89 assert(msg_in.req_ui32() == msg_out.req_ui32());
90 assert(!msg_out.has_opt_i32());
91 assert(!msg_out.has_opt_ui32());
92}
93
94// Test 3: encode/decode with required fields left empty
95void test3(dccl::Codec& codec)
96{
97 Proto3Msg msg_in;
98
99 std::string encoded;
100 codec.encode(&encoded, msg_in);
101
102 Proto3Msg msg_out;
103 codec.decode(encoded, &msg_out);
104
105 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Msg in: " << msg_in.ShortDebugString() << std::endl;
106 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Msg out: " << msg_out.ShortDebugString() << std::endl;
107
108 assert(msg_in.req_i32() == msg_out.req_i32());
109
110 // built-in default of 0 is not encodable with this range of DCCL values
111 assert(msg_out.req_ui32() == 10);
112}
113
114// Test 4: encode child message
115void test4(dccl::Codec& codec)
116{
117 Proto3Msg msg_in;
118 msg_in.mutable_child()->set_req_dbl(0.4);
119
120 std::string encoded;
121 codec.encode(&encoded, msg_in);
122
123 Proto3Msg msg_out;
124 codec.decode(encoded, &msg_out);
125
126 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Msg in: " << msg_in.ShortDebugString() << std::endl;
127 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Msg out: " << msg_out.ShortDebugString() << std::endl;
128
129 assert(msg_in.req_i32() == msg_out.req_i32());
130}
131
132int main(int argc, char* argv[])
133{
134 bool verbose = false;
135 for (int i = 1; i < argc; ++i)
136 {
137 if (argv[i] && argv[i][0] == '-' && argv[i][1] == 'v' && argv[i][2] == '\0')
138 verbose = true;
139 }
140
141 dccl::dlog.connect(verbose ? dccl::logger::ALL : dccl::logger::WARN_PLUS, &std::cerr);
142
143 dccl::Codec codec3;
144 dccl::Codec codec2;
145 codec3.load<Proto3Msg>();
146 codec2.load<Proto2Msg>();
147 codec3.info<Proto3Msg>(&dccl::dlog);
148 codec2.info<Proto2Msg>(&dccl::dlog);
149
150 test1(codec3, codec2);
151 test2(codec3);
152 test3(codec3);
153 test4(codec3);
154
155 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "all tests passed" << std::endl;
156 return 0;
157}
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
Definition codec.h:61
void info(std::ostream *os=nullptr, int user_id=-1) const
Writes a human readable summary (including field sizes) of the provided DCCL type to the stream provi...
Definition codec.h:195
CharIterator decode(CharIterator begin, CharIterator end, ProtobufMessage *msg, bool header_only=false)
Decode a DCCL message when the type is known at compile time.
Definition codec.h:535
std::size_t load()
All messages must be explicited loaded and validated (size checks, option extensions checks,...
Definition codec.h:119
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