DCCL v5
Loading...
Searching...
No Matches
test.cpp
1// Copyright 2019-2023:
2// GobySoft, LLC (2013-)
3// Community contributors (see AUTHORS file)
4// File authors:
5// Chris Murphy <cmurphy@aphysci.com>
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 all protobuf types with _default codecs, repeat and non repeat
25
26#include <google/protobuf/descriptor.pb.h>
27
28#include "../../binary.h"
29#include "../../codec.h"
30#include "../../codecs3/field_codec_default.h"
31#include "test.pb.h"
32using namespace dccl::test;
33
34void decode_check(const std::string& encoded);
35
36dccl::Codec codec;
37TestMsgPack msg_pack;
38TestMsgUnpack msg_unpack;
39
40int main(int argc, char* argv[])
41{
42 bool verbose = false;
43 for (int i = 1; i < argc; ++i)
44 {
45 if (argv[i] && argv[i][0] == '-' && argv[i][1] == 'v' && argv[i][2] == '\0')
46 verbose = true;
47 }
48
49 dccl::dlog.connect(verbose ? dccl::logger::ALL : dccl::logger::WARN_PLUS, &std::cerr);
50
51 msg_pack.set_five_bit_padding(0);
52 msg_pack.set_value(ENUM2_H);
53 msg_unpack.set_value(ENUM2_H);
54
55 codec.info(msg_pack.GetDescriptor());
56 codec.info(msg_unpack.GetDescriptor());
57
58 codec.load(msg_pack.GetDescriptor());
59 codec.load(msg_unpack.GetDescriptor());
60
61 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try encode..." << std::endl;
62 std::string bytes_pack;
63 codec.encode(&bytes_pack, msg_pack);
64 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got packed bytes (hex): " << dccl::hex_encode(bytes_pack) << std::endl;
65
66 std::string bytes_unpack;
67 codec.encode(&bytes_unpack, msg_unpack);
68 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got unpacked bytes (hex): " << dccl::hex_encode(bytes_unpack) << std::endl;
69
70 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try decode..." << std::endl;
71 TestMsgPack msg_pack_out;
72 TestMsgUnpack msg_unpack_out;
73
74 codec.decode(bytes_pack, &msg_pack_out);
75 codec.decode(bytes_unpack, &msg_unpack_out);
76 assert(msg_pack_out.SerializeAsString() == msg_pack.SerializeAsString());
77 assert(msg_unpack_out.SerializeAsString() == msg_unpack.SerializeAsString());
78 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "all tests passed" << std::endl;
79}
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