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// tests all protobuf types with _default codecs, repeat and non repeat
24
25#include <fstream>
26
27
28#include "../../binary.h"
29#include "../../codec.h"
30#include "test.pb.h"
31using namespace dccl::test;
32
33dccl::Codec codec;
34
35template <typename TestMsg> void decode_check(const TestMsg& msg_in)
36{
37 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Message in:\n" << msg_in.DebugString() << std::endl;
38
39 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try encode (in bounds)..." << std::endl;
40 std::string bytes;
41 codec.encode(&bytes, msg_in);
42 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got bytes (hex): " << dccl::hex_encode(bytes) << std::endl;
43
44 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try decode..." << std::endl;
45
46 TestMsg msg_out;
47 codec.decode(bytes, &msg_out);
48
49 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got Message out:\n" << msg_out.DebugString() << std::endl;
50}
51
52int main(int argc, char* argv[])
53{
54 bool verbose = false;
55 for (int i = 1; i < argc; ++i)
56 {
57 if (argv[i] && argv[i][0] == '-' && argv[i][1] == 'v' && argv[i][2] == '\0')
58 verbose = true;
59 }
60
61 dccl::dlog.connect(verbose ? dccl::logger::ALL : dccl::logger::WARN_PLUS, &std::cerr);
62
63 codec.load<TestMsgOmit1>();
64 codec.info<TestMsgOmit1>();
65
66 codec.load<TestMsgOmit2>();
67 codec.info<TestMsgOmit2>();
68
69 codec.load<TestMsgNormal>();
70 codec.info<TestMsgNormal>();
71
72 codec.info_all();
73
74 {
75 TestMsgOmit1 msg_in;
76 msg_in.set_d(10.0);
77 msg_in.set_i(1000);
78
79 decode_check(msg_in);
80 }
81
82 {
83 TestMsgOmit2 msg_in;
84 msg_in.set_u(15);
85
86 decode_check(msg_in);
87 }
88
89 {
90 TestMsgNormal msg_in;
91 msg_in.set_f(2.3);
92
93 decode_check(msg_in);
94 }
95
96 {
97 TestMsgNormal msg_in;
98 msg_in.set_f(2.3);
99
100 std::string bytes;
101 codec.encode(&bytes, msg_in);
102 TestMsgNormalId3 msg_out;
103
104 // mismatch in ID used to encode vs decode
105 try
106 {
107 codec.decode(bytes, &msg_out);
108 }
109 catch (const dccl::Exception& e)
110 {
111 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Caught expected exception: " << e.what() << std::endl;
112 }
113 }
114
115 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "all tests passed" << std::endl;
116}
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