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
29#include "../../codec.h"
30#include "../../codecs2/field_codec_default.h"
31
32#include "../../binary.h"
33#include "test.pb.h"
34
35using namespace dccl::test;
36
37int main(int argc, char* argv[])
38{
39 bool verbose = false;
40 for (int i = 1; i < argc; ++i)
41 {
42 if (argv[i] && argv[i][0] == '-' && argv[i][1] == 'v' && argv[i][2] == '\0')
43 verbose = true;
44 }
45
46 dccl::dlog.connect(verbose ? dccl::logger::ALL : dccl::logger::WARN_PLUS, &std::cerr);
47 // check the empty messages
48 dccl::Codec codec;
49
50 if (dccl::dlog.is(dccl::logger::INFO))
51 {
52 codec.info<TestMsg>(&dccl::dlog);
53 }
54 codec.load<TestMsg>();
55
56 {
57 TestMsg msg_in, msg_out;
58
59 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Message in:\n" << msg_in.DebugString() << std::endl;
60
61 codec.load(msg_in.GetDescriptor());
62
63 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try encode..." << std::endl;
64 std::string bytes;
65 codec.encode(&bytes, msg_in);
66 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got bytes (hex): " << dccl::hex_encode(bytes) << std::endl;
67
68 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try decode..." << std::endl;
69
70 codec.decode(bytes, &msg_out);
71
72 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got Message out:\n" << msg_out.DebugString() << std::endl;
73
74 assert(!msg_out.has_msg1());
75 assert(!msg_out.msg1_repeat_size());
76 assert(!msg_out.has_msg2());
77 assert(!msg_out.msg2_repeat_size());
78 assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
79 }
80
81 // check partially full messages
82 {
83 TestMsg msg_in, msg_out;
84
85 msg_in.mutable_msg1()->set_val(0.1);
86 msg_in.add_msg1_repeat()->set_val(0.11);
87 msg_in.add_msg1_repeat()->set_val(0.12);
88 msg_in.add_msg1_repeat()->set_val(0.13);
89 msg_in.mutable_msg2()->set_val(0.2);
90 msg_in.add_msg2_repeat()->set_val(0.21);
91 msg_in.add_msg2_repeat()->set_val(0.22);
92 msg_in.add_msg2_repeat()->set_val(0.23);
93
94 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Message in:\n" << msg_in.DebugString() << std::endl;
95
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
103 codec.decode(bytes, &msg_out);
104
105 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got Message out:\n" << msg_out.DebugString() << std::endl;
106
107 assert(msg_out.has_msg1());
108 assert(msg_out.msg1_repeat_size() == 3);
109 assert(msg_out.has_msg2());
110 assert(msg_out.msg2_repeat_size() == 3);
111 assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
112 }
113
114 // check message with set submessage but not children
115 {
116 TestMsg msg_in, msg_out;
117
118 msg_in.mutable_msg1();
119 msg_in.mutable_msg2()->set_val(1);
120 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Message in:\n" << msg_in.DebugString() << std::endl;
121
122 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try encode..." << std::endl;
123 std::string bytes;
124 codec.encode(&bytes, msg_in);
125 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got bytes (hex): " << dccl::hex_encode(bytes) << std::endl;
126
127 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try decode..." << std::endl;
128
129 codec.decode(bytes, &msg_out);
130
131 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got Message out:\n" << msg_out.DebugString() << std::endl;
132
133 assert(msg_out.has_msg1());
134 assert(msg_out.has_msg2());
135 assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
136 }
137
138 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "all tests passed" << std::endl;
139}
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