DCCL v4
test.cpp
1 // Copyright 2019:
2 // GobySoft, LLC (2013-)
3 // Community contributors (see AUTHORS file)
4 // File authors:
5 // Chris Murphy <cmurphy@aphysci.com>
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 all protobuf types with _default codecs, repeat and non repeat
24 
25 #include <google/protobuf/descriptor.pb.h>
26 
27 #include "../../binary.h"
28 #include "../../codec.h"
29 #include "../../codecs3/field_codec_default.h"
30 #include "test.pb.h"
31 using namespace dccl::test;
32 
33 void decode_check(const std::string& encoded);
34 
35 dccl::Codec codec;
36 TestMsgPack msg_pack;
37 TestMsgUnpack msg_unpack;
38 
39 int main(int /*argc*/, char* /*argv*/ [])
40 {
41  dccl::dlog.connect(dccl::logger::ALL, &std::cerr);
42 
43  msg_pack.set_five_bit_padding(0);
44  msg_pack.set_value(ENUM2_H);
45  msg_unpack.set_value(ENUM2_H);
46 
47  codec.info(msg_pack.GetDescriptor());
48  codec.info(msg_unpack.GetDescriptor());
49 
50  codec.load(msg_pack.GetDescriptor());
51  codec.load(msg_unpack.GetDescriptor());
52 
53  std::cout << "Try encode..." << std::endl;
54  std::string bytes_pack;
55  codec.encode(&bytes_pack, msg_pack);
56  std::cout << "... got packed bytes (hex): " << dccl::hex_encode(bytes_pack) << std::endl;
57 
58  std::string bytes_unpack;
59  codec.encode(&bytes_unpack, msg_unpack);
60  std::cout << "... got unpacked bytes (hex): " << dccl::hex_encode(bytes_unpack) << std::endl;
61 
62  std::cout << "Try decode..." << std::endl;
63  TestMsgPack msg_pack_out;
64  TestMsgUnpack msg_unpack_out;
65 
66  codec.decode(bytes_pack, &msg_pack_out);
67  codec.decode(bytes_unpack, &msg_unpack_out);
68  assert(msg_pack_out.SerializeAsString() == msg_pack.SerializeAsString());
69  assert(msg_unpack_out.SerializeAsString() == msg_unpack.SerializeAsString());
70  std::cout << "all tests passed" << std::endl;
71 }
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
Definition: codec.h:63
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
Unit test namespace.
Definition: test.cpp:46
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