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 fixed id header
25
26#include "../../codec.h"
27#include "test.pb.h"
28using namespace dccl::test;
29
30int main(int argc, char* argv[])
31{
32 bool verbose = false;
33 for (int i = 1; i < argc; ++i)
34 {
35 if (argv[i] && argv[i][0] == '-' && argv[i][1] == 'v' && argv[i][2] == '\0')
36 verbose = true;
37 }
38
39 dccl::dlog.connect(verbose ? dccl::logger::ALL : dccl::logger::WARN_PLUS, &std::cerr);
40
41 dccl::Codec codec;
42
43 {
44 ShortIDMsg short_id_msg;
45 codec.load(short_id_msg.GetDescriptor());
46 codec.info(short_id_msg.GetDescriptor(), &dccl::dlog);
47
48 std::string encoded;
49 assert(codec.size(short_id_msg) == 1);
50 codec.encode(&encoded, short_id_msg);
51 assert(codec.id(encoded) == 2);
52 codec.decode(encoded, &short_id_msg);
53 }
54
55 {
56 LongIDMsg long_id_msg;
57 std::string encoded;
58 codec.load(long_id_msg.GetDescriptor());
59 codec.info(long_id_msg.GetDescriptor(), &dccl::dlog);
60 assert(codec.size(long_id_msg) == 2);
61 codec.encode(&encoded, long_id_msg);
62 assert(codec.id(encoded) == 10000);
63 assert(encoded.size() == 2);
64 codec.decode(encoded, &long_id_msg);
65
66 // try decoding zero and single byte (should fail)
67 try
68 {
69 codec.id(std::string());
70 assert(false);
71 }
72 catch (const dccl::Exception& e)
73 {
74 // expected
75 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Expected exception: " << e.what() << std::endl;
76 }
77 try
78 {
79 codec.id(encoded.substr(0, 1));
80 assert(false);
81 }
82 catch (const dccl::Exception& e)
83 {
84 // expected
85 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Expected exception: " << e.what() << std::endl;
86 }
87 }
88
89 {
90 ShortIDEdgeMsg short_id_edge_msg;
91 std::string encoded;
92 codec.load(short_id_edge_msg.GetDescriptor());
93 codec.info(short_id_edge_msg.GetDescriptor(), &dccl::dlog);
94 assert(codec.size(short_id_edge_msg) == 1);
95 codec.encode(&encoded, short_id_edge_msg);
96 assert(codec.id(encoded) == 127);
97 codec.decode(encoded, &short_id_edge_msg);
98 }
99
100 {
101 LongIDEdgeMsg long_id_edge_msg;
102 std::string encoded;
103 codec.load(long_id_edge_msg.GetDescriptor());
104 codec.info(long_id_edge_msg.GetDescriptor(), &dccl::dlog);
105 codec.encode(&encoded, long_id_edge_msg);
106 assert(codec.id(encoded) == 128);
107 codec.decode(encoded, &long_id_edge_msg);
108 assert(codec.size(long_id_edge_msg) == 2);
109 }
110
111 {
112 TooLongIDMsg too_long_id_msg;
113 // should fail validation
114 try
115 {
116 codec.load(too_long_id_msg.GetDescriptor());
117 assert(false);
118 }
119 catch (dccl::Exception& e)
120 {
121 }
122 }
123
124 {
125 ShortIDMsgWithData short_id_msg_with_data;
126 std::string encoded;
127 codec.load(short_id_msg_with_data.GetDescriptor());
128 codec.info(short_id_msg_with_data.GetDescriptor(), &dccl::dlog);
129
130 short_id_msg_with_data.set_in_head(42);
131 short_id_msg_with_data.set_in_body(37);
132 codec.encode(&encoded, short_id_msg_with_data);
133 assert(codec.id(encoded) == 3);
134 codec.decode(encoded, &short_id_msg_with_data);
135 }
136
137 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "all tests passed" << std::endl;
138}
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