DCCL v4
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"
32 using namespace dccl::test;
33 
34 void decode_check(const std::string& encoded);
35 
36 dccl::Codec codec;
37 TestMsgPack msg_pack;
38 TestMsgUnpack msg_unpack;
39 
40 int main(int /*argc*/, char* /*argv*/ [])
41 {
42  dccl::dlog.connect(dccl::logger::ALL, &std::cerr);
43 
44  msg_pack.set_five_bit_padding(0);
45  msg_pack.set_value(ENUM2_H);
46  msg_unpack.set_value(ENUM2_H);
47 
48  codec.info(msg_pack.GetDescriptor());
49  codec.info(msg_unpack.GetDescriptor());
50 
51  codec.load(msg_pack.GetDescriptor());
52  codec.load(msg_unpack.GetDescriptor());
53 
54  std::cout << "Try encode..." << std::endl;
55  std::string bytes_pack;
56  codec.encode(&bytes_pack, msg_pack);
57  std::cout << "... got packed bytes (hex): " << dccl::hex_encode(bytes_pack) << std::endl;
58 
59  std::string bytes_unpack;
60  codec.encode(&bytes_unpack, msg_unpack);
61  std::cout << "... got unpacked bytes (hex): " << dccl::hex_encode(bytes_unpack) << std::endl;
62 
63  std::cout << "Try decode..." << std::endl;
64  TestMsgPack msg_pack_out;
65  TestMsgUnpack msg_unpack_out;
66 
67  codec.decode(bytes_pack, &msg_pack_out);
68  codec.decode(bytes_unpack, &msg_unpack_out);
69  assert(msg_pack_out.SerializeAsString() == msg_pack.SerializeAsString());
70  assert(msg_unpack_out.SerializeAsString() == msg_unpack.SerializeAsString());
71  std::cout << "all tests passed" << std::endl;
72 }
dccl::test
Unit test namespace.
Definition: test.cpp:44
dccl::Codec
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
Definition: codec.h:62
dccl::Logger::connect
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:214
dccl::hex_encode
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:100