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// Community contributors (see AUTHORS file)
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 dynamic max_bytes: messages can be sent over different channels
24// with different byte-size constraints enforced at runtime.
25
26#include <cassert>
27#include <iostream>
28#include <stdexcept>
29#include <string>
30
31#include "../../codec.h"
32#include "test.pb.h"
33
34using namespace dccl::test;
35
36dccl::Codec codec;
37
38// Test that a message with channel=WIFI (limit 200 bytes) encodes/decodes OK
39// when data fits within 200 bytes.
40void test_wifi_ok()
41{
42 codec.load<TestMaxBytes>();
43
44 TestMaxBytes msg;
45 msg.set_channel(TestMaxBytes::WIFI);
46 // Add a few data values - well within 200 bytes
47 for (int i = 0; i < 5; ++i)
48 msg.add_data(i * 1000);
49
50 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "test_wifi_ok: encoding..." << std::endl;
51 std::string bytes;
52 codec.encode(&bytes, msg);
53 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << " encoded size: " << bytes.size() << " bytes" << std::endl;
54 assert(bytes.size() <= 200);
55
56 TestMaxBytes msg_out;
57 codec.decode(bytes, &msg_out);
58
59 assert(msg.SerializeAsString() == msg_out.SerializeAsString());
60 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "test_wifi_ok: passed" << std::endl;
61}
62
63// Test that a message with channel=ACOUSTIC (limit 10 bytes) encodes OK
64// when data is small enough.
65void test_acoustic_ok()
66{
67 codec.load<TestMaxBytes>();
68
69 TestMaxBytes msg;
70 msg.set_channel(TestMaxBytes::ACOUSTIC);
71 // No extra data - just the channel field - should be tiny
72
73 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "test_acoustic_ok: encoding..." << std::endl;
74 std::string bytes;
75 codec.encode(&bytes, msg);
76 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << " encoded size: " << bytes.size() << " bytes" << std::endl;
77 assert(bytes.size() <= 10);
78
79 TestMaxBytes msg_out;
80 codec.decode(bytes, &msg_out);
81
82 assert(msg.SerializeAsString() == msg_out.SerializeAsString());
83 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "test_acoustic_ok: passed" << std::endl;
84}
85
86// Test that a message with channel=ACOUSTIC (limit 10 bytes) throws when
87// the encoded data exceeds 10 bytes.
88void test_acoustic_too_large()
89{
90 codec.load<TestMaxBytes>();
91
92 TestMaxBytes msg;
93 msg.set_channel(TestMaxBytes::ACOUSTIC);
94 // Add many data values to exceed 10 bytes
95 for (int i = 0; i < 15; ++i)
96 msg.add_data(i * 1000);
97
98 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "test_acoustic_too_large: encoding (should throw)..." << std::endl;
99 std::string bytes;
100 bool threw = false;
101 try
102 {
103 codec.encode(&bytes, msg);
104 }
105 catch (const dccl::Exception& e)
106 {
107 threw = true;
108 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << " caught expected exception: " << e.what() << std::endl;
109 }
110 assert(threw && "Expected exception when encoding message exceeding dynamic max_bytes");
111 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "test_acoustic_too_large: passed" << std::endl;
112}
113
114int main(int argc, char* argv[])
115{
116 bool verbose = false;
117 for (int i = 1; i < argc; ++i)
118 {
119 if (argv[i] && argv[i][0] == '-' && argv[i][1] == 'v' && argv[i][2] == '\0')
120 verbose = true;
121 }
122
123 dccl::dlog.connect(verbose ? dccl::logger::ALL : dccl::logger::WARN_PLUS, &std::cerr);
124
125 test_wifi_ok();
126 test_acoustic_ok();
127 test_acoustic_too_large();
128
129 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "all tests passed" << std::endl;
130 return 0;
131}
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