DCCL v3
test.cpp
1 // Copyright 2009-2017 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (for 2013-)
3 // Massachusetts Institute of Technology (for 2007-2014)
4 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Dynamic Compact Control Language Library
8 // ("DCCL").
9 //
10 // DCCL is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // DCCL is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with DCCL. If not, see <http://www.gnu.org/licenses/>.
22 // tests all protobuf types with _default codecs, repeat and non repeat
23 
24 #include <google/protobuf/descriptor.pb.h>
25 
26 #include "dccl/codecs3/field_codec_default.h"
27 #include "dccl/codec.h"
28 #include "test.pb.h"
29 #include "dccl/binary.h"
30 using namespace dccl::test;
31 
32 void decode_check(const std::string& encoded);
33 
34 dccl::Codec codec;
35 TestMsgPack msg_pack;
36 TestMsgUnpack msg_unpack;
37 
38 int main(int argc, char* argv[]) {
39  dccl::dlog.connect(dccl::logger::ALL, &std::cerr);
40 
41  msg_pack.set_five_bit_padding(0);
42  msg_pack.set_value(ENUM2_H);
43  msg_unpack.set_value(ENUM2_H);
44 
45  codec.info(msg_pack.GetDescriptor());
46  codec.info(msg_unpack.GetDescriptor());
47 
48  codec.load(msg_pack.GetDescriptor());
49  codec.load(msg_unpack.GetDescriptor());
50 
51  std::cout << "Try encode..." << std::endl;
52  std::string bytes_pack;
53  codec.encode(&bytes_pack, msg_pack);
54  std::cout << "... got packed bytes (hex): " << dccl::hex_encode(bytes_pack) << std::endl;
55 
56  std::string bytes_unpack;
57  codec.encode(&bytes_unpack, msg_unpack);
58  std::cout << "... got unpacked bytes (hex): " << dccl::hex_encode(bytes_unpack) << std::endl;
59 
60  std::cout << "Try decode..." << std::endl;
61  TestMsgPack msg_pack_out;
62  TestMsgUnpack msg_unpack_out;
63 
64  codec.decode(bytes_pack, &msg_pack_out);
65  codec.decode(bytes_unpack, &msg_unpack_out);
66  assert(msg_pack_out.SerializeAsString() == msg_pack.SerializeAsString());
67  assert(msg_unpack_out.SerializeAsString() == msg_unpack.SerializeAsString());
68  std::cout << "all tests passed" << std::endl;
69 }
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
Definition: codec.h:56
void info(std::ostream *os=0, int user_id=-1) const
Writes a human readable summary (including field sizes) of the provided DCCL type to the stream provi...
Definition: codec.h:155
void load()
All messages must be explicited loaded and validated (size checks, option extensions checks...
Definition: codec.h:96
void encode(std::string *bytes, const google::protobuf::Message &msg, bool header_only=false, int user_id=-1)
Encodes a DCCL message.
Definition: codec.cpp:273
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:161
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
Unit test namespace.
Definition: test.cpp:44
CharIterator decode(CharIterator begin, CharIterator end, google::protobuf::Message *msg, bool header_only=false)
Decode a DCCL message when the type is known at compile time.
Definition: codec.h:439