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 required versus optional encoding of fields using a presence bit
23 
24 #include "dccl/codec.h"
25 #include "test.pb.h"
26 using namespace dccl::test;
27 
28 using dccl::operator<<;
29 
30 int main(int argc, char* argv[])
31 {
32  dccl::dlog.connect(dccl::logger::ALL, &std::cerr);
33 
34  dccl::Codec codec;
35 
36  codec.load<BytesMsg>();
37  codec.info<BytesMsg>(&dccl::dlog);
38 
39  BytesMsg msg_in;
40 
41  msg_in.set_req_bytes(dccl::hex_decode("88abcd1122338754"));
42  msg_in.set_opt_bytes(dccl::hex_decode("102030adef2cb79d"));
43 
44  std::string encoded;
45  codec.encode(&encoded, msg_in);
46 
47  BytesMsg msg_out;
48  codec.decode(encoded, &msg_out);
49 
50  assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
51 
52 
53  std::cout << "all tests passed" << std::endl;
54 }
55 
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
Definition: codec.h:56
void hex_decode(const std::string &in, std::string *out)
Decodes a (little-endian) hexadecimal string to a byte string. Index 0 and 1 (first byte) of in are w...
Definition: binary.h:47
void load()
All messages must be explicited loaded and validated (size checks, option extensions checks...
Definition: codec.h:96
void info(std::ostream *os=0) const
Writes a human readable summary (including field sizes) of the provided DCCL type to the stream provi...
Definition: codec.h:139
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
Unit test namespace.
Definition: test.pb.h:36
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:405
void encode(std::string *bytes, const google::protobuf::Message &msg, bool header_only=false)
Encodes a DCCL message.
Definition: codec.cpp:266