DCCL v5
Loading...
Searching...
No Matches
test.cpp
1// Copyright 2024:
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
24// Tests that the codec recursively checks for in_head fields inside embedded
25// messages. In the issue scenario, Bar.foo does not have in_head=true, but
26// Foo.header does. Before the fix, Bar:foo::header was never encoded/decoded
27// because the codec skipped foo (in_head=false) when processing the HEAD part.
28
29#include <cassert>
30#include <iostream>
31
32#include "../../binary.h"
33#include "../../codec.h"
34#include "test.pb.h"
35
36using namespace dccl::test;
37
38template <typename BarMsg> void run_test(const std::string& label)
39{
40 dccl::Codec codec;
41 BarMsg msg_in;
42
43 msg_in.mutable_foo()->mutable_header()->set_field(42);
44 msg_in.mutable_foo()->set_body_field(99);
45 msg_in.set_bar_body(77);
46
47 codec.load(msg_in.GetDescriptor());
48 if (dccl::dlog.is(dccl::logger::INFO))
49 {
50 codec.info(msg_in.GetDescriptor(), &dccl::dlog);
51 }
52 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << label << " message in:\n" << msg_in.DebugString() << std::endl;
53
54 std::string bytes;
55 codec.encode(&bytes, msg_in);
56 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << label << " encoded (hex): " << dccl::hex_encode(bytes) << std::endl;
57
58 BarMsg msg_out;
59 codec.decode(bytes, &msg_out);
60 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << label << " message out:\n" << msg_out.DebugString() << std::endl;
61
62 // Verify that the header field (in_head) was correctly encoded and decoded
63 assert(msg_out.foo().header().field() == msg_in.foo().header().field());
64 // Verify that the body fields were also correctly handled
65 assert(msg_out.foo().body_field() == msg_in.foo().body_field());
66 assert(msg_out.bar_body() == msg_in.bar_body());
67 // Full round-trip check
68 assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
69
70 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << label << " passed.\n" << std::endl;
71}
72
73int main(int argc, char* argv[])
74{
75 bool verbose = false;
76 for (int i = 1; i < argc; ++i)
77 {
78 if (argv[i] && argv[i][0] == '-' && argv[i][1] == 'v' && argv[i][2] == '\0')
79 verbose = true;
80 }
81
82 dccl::dlog.connect(verbose ? dccl::logger::ALL : dccl::logger::WARN_PLUS, &std::cerr);
83
84 run_test<BarV4>("BarV4 (codec_version=4)");
85 run_test<BarV5>("BarV5 (codec_version=5)");
86
87 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "all tests passed" << std::endl;
88 return 0;
89}
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