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// Chris Murphy <cmurphy@aphysci.com>
8//
9//
10// This file is part of the Dynamic Compact Control Language Library
11// ("DCCL").
12//
13// DCCL is free software: you can redistribute it and/or modify
14// it under the terms of the GNU Lesser General Public License as published by
15// the Free Software Foundation, either version 2.1 of the License, or
16// (at your option) any later version.
17//
18// DCCL is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU Lesser General Public License for more details.
22//
23// You should have received a copy of the GNU Lesser General Public License
24// along with DCCL. If not, see <http://www.gnu.org/licenses/>.
25// tests receiving one of several static types
26
27#include "../../codec.h"
28#include "test.pb.h"
29
30using namespace dccl::test;
31
32using dccl::operator<<;
33
34GobyMessage1 msg_in1;
35GobyMessage2 msg_in2;
36GobyMessage3 msg_in3;
37dccl::Codec codec;
38
39void decode(const std::string& bytes);
40
41int main(int argc, char* argv[])
42{
43 bool verbose = false;
44 for (int i = 1; i < argc; ++i)
45 {
46 if (argv[i] && argv[i][0] == '-' && argv[i][1] == 'v' && argv[i][2] == '\0')
47 verbose = true;
48 }
49
50 dccl::dlog.connect(verbose ? dccl::logger::ALL : dccl::logger::WARN_PLUS, &std::cerr);
51
52 codec.load<GobyMessage1>();
53 codec.load<GobyMessage2>();
54 codec.load<GobyMessage3>();
55
56 codec.info<GobyMessage1>(&dccl::dlog);
57 codec.info<GobyMessage2>(&dccl::dlog);
58 codec.info<GobyMessage3>(&dccl::dlog);
59
60 msg_in1.set_int32_val(1);
61 msg_in2.set_bool_val(false);
62 msg_in3.set_string_val("string1");
63
64 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try encode..." << std::endl;
65 std::string bytes1, bytes2, bytes3;
66 codec.encode(&bytes1, msg_in1);
67 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got bytes for GobyMessage1 (hex): " << dccl::hex_encode(bytes1) << std::endl;
68 codec.encode(&bytes2, msg_in2);
69 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got bytes for GobyMessage2 (hex): " << dccl::hex_encode(bytes2) << std::endl;
70 codec.encode(&bytes3, msg_in3);
71 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got bytes for GobyMessage3 (hex): " << dccl::hex_encode(bytes3) << std::endl;
72
73 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try decode..." << std::endl;
74
75 // mix up the order
76 decode(bytes2);
77 decode(bytes1);
78 decode(bytes3);
79
80 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "all tests passed" << std::endl;
81}
82
83void decode(const std::string& bytes)
84{
85 unsigned dccl_id = codec.id(bytes);
86
87 if (dccl_id == codec.id<GobyMessage1>())
88 {
89 GobyMessage1 msg_out1;
90 codec.decode(bytes, &msg_out1);
91
92 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Got..." << msg_out1 << std::endl;
93 assert(msg_out1.SerializeAsString() == msg_in1.SerializeAsString());
94 }
95 else if (dccl_id == codec.id<GobyMessage2>())
96 {
97 GobyMessage2 msg_out2;
98 codec.decode(bytes, &msg_out2);
99
100 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Got..." << msg_out2 << std::endl;
101 assert(msg_out2.SerializeAsString() == msg_in2.SerializeAsString());
102 }
103 else if (dccl_id == codec.id<GobyMessage3>())
104 {
105 GobyMessage3 msg_out3;
106 codec.decode(bytes, &msg_out3);
107
108 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Got..." << msg_out3 << std::endl;
109 assert(msg_out3.SerializeAsString() == msg_in3.SerializeAsString());
110 }
111}
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