DCCL v4
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#include <google/protobuf/descriptor.pb.h>
29
30#include "../../codec.h"
31#include "../../codecs3/field_codec_default.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 // dccl::dlog.connect(dccl::logger::ALL, &std::cerr);
60
61 codec.manager().add<dccl::test::TestCodec>("test.grouptest");
62 codec.manager()
63 .add<dccl::v3::DefaultMessageCodec, google::protobuf::FieldDescriptor::TYPE_MESSAGE>(
64 "test.grouptest");
65
66 check<TestMsg>(50, true);
67 check<TestMsg>(-50, false);
68 check<TestMsgGroup>(50, true);
69 check<TestMsgGroup>(-50, true);
70 check<TestMsgVersion>(50, true);
71
72 std::cout << "all tests passed" << std::endl;
73}
74
75template <typename Msg> void check(double val, bool should_pass)
76{
77 Msg msg_in;
78 int i = 0;
79 msg_in.set_d(++i + 0.1);
80 msg_in.add_d_repeat(12.1);
81 msg_in.add_d_repeat(12.2);
82 msg_in.add_d_repeat(12.3);
83
84 msg_in.mutable_msg()->set_val(val);
85 msg_in.mutable_msg()->mutable_msg()->set_val(val);
86 codec.info(msg_in.GetDescriptor(), &std::cout);
87
88 std::cout << "Message in:\n" << msg_in.DebugString() << std::endl;
89
90 codec.load(msg_in.GetDescriptor());
91
92 std::cout << "Try encode..." << std::endl;
93 std::string bytes;
94 codec.encode(&bytes, msg_in);
95 std::cout << "... got bytes (hex): " << dccl::hex_encode(bytes) << std::endl;
96
97 std::cout << "Try decode..." << std::endl;
98
99 Msg msg_out;
100 codec.decode(bytes, &msg_out);
101
102 std::cout << "... got Message out:\n" << msg_out.DebugString() << std::endl;
103
104 if (should_pass)
105 assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
106 else
107 assert(msg_in.SerializeAsString() != msg_out.SerializeAsString());
108}
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
Definition codec.h:63
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:47
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:100