DCCL v5
Loading...
Searching...
No Matches
test.cpp
1// Copyright 2011-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 "../../codecs3/field_codec_default.h"
31#include "../../codecs3/field_codec_default_message.h"
32
33#include "../../binary.h"
34#include "test.pb.h"
35using namespace dccl::test;
36
37template <typename Msg> void check(double val, bool should_pass);
38dccl::Codec codec;
39TestMsg msg_in;
40TestMsgGroup msg_group_in;
41
42namespace dccl
43{
44namespace test
45{
47{
48 double max() override { return 100; }
49 double min() override { return -100; }
50 double precision() override { return 1; } // Deprecated
51 double resolution() override { return 0.1; }
52 void validate() override {}
53};
54} // namespace test
55} // namespace dccl
56
57int main(int argc, char* argv[])
58{
59 bool verbose = false;
60 for (int i = 1; i < argc; ++i)
61 {
62 if (argv[i] && argv[i][0] == '-' && argv[i][1] == 'v' && argv[i][2] == '\0')
63 verbose = true;
64 }
65
66 dccl::dlog.connect(verbose ? dccl::logger::ALL : dccl::logger::WARN_PLUS, &std::cerr);
67
68 codec.manager().add<dccl::test::TestCodec>("test.grouptest");
69 codec.manager()
70 .add<dccl::v3::DefaultMessageCodec, google::protobuf::FieldDescriptor::TYPE_MESSAGE>(
71 "test.grouptest");
72
73 check<TestMsg>(50, true);
74 check<TestMsg>(-50, false);
75 check<TestMsgGroup>(50, true);
76 check<TestMsgGroup>(-50, true);
77 check<TestMsgV4>(50, true);
78
79 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "all tests passed" << std::endl;
80}
81
82template <typename Msg> void check(double val, bool should_pass)
83{
84 Msg msg_in;
85 int i = 0;
86 msg_in.set_d(++i + 0.1);
87 msg_in.add_d_repeat(12.1);
88 msg_in.add_d_repeat(12.2);
89 msg_in.add_d_repeat(12.3);
90
91 msg_in.mutable_msg()->set_val(val);
92 msg_in.mutable_msg()->mutable_msg()->set_val(val);
93 if (dccl::dlog.is(dccl::logger::INFO))
94 {
95 codec.info(msg_in.GetDescriptor(), &dccl::dlog);
96 }
97 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Message in:\n" << msg_in.DebugString() << std::endl;
98
99 codec.load(msg_in.GetDescriptor());
100
101 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try encode..." << std::endl;
102 std::string bytes;
103 codec.encode(&bytes, msg_in);
104 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got bytes (hex): " << dccl::hex_encode(bytes) << std::endl;
105
106 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try decode..." << std::endl;
107
108 Msg msg_out;
109 codec.decode(bytes, &msg_out);
110
111 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got Message out:\n" << msg_out.DebugString() << std::endl;
112
113 if (should_pass)
114 assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
115 else
116 assert(msg_in.SerializeAsString() != msg_out.SerializeAsString());
117}
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
Provides a basic bounded arbitrary length numeric (double, float, uint32, uint64, int32,...
Provides the default codec for encoding a base Google Protobuf message or an embedded message by call...
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