DCCL v4
test.cpp
1 // Copyright 2011-2023:
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 // File authors:
6 // Toby Schneider <toby@gobysoft.org>
7 // Davide Fenucci <davfen@noc.ac.uk>
8 //
9 //
10 // This file is part of the Dynamic Compact Control Language Library
11 // ("DCCL").
12 //
13 // DCCL is free software: you can redistribute it and/or modify
14 // it under the terms of the GNU Lesser General Public License as published by
15 // the Free Software Foundation, either version 2.1 of the License, or
16 // (at your option) any later version.
17 //
18 // DCCL is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU Lesser General Public License for more details.
22 //
23 // You should have received a copy of the GNU Lesser General Public License
24 // along with DCCL. If not, see <http://www.gnu.org/licenses/>.
25 // tests all protobuf types with _default codecs, repeat and non repeat
26 
27 #include <fstream>
28 
29 #include "../../native_protobuf/dccl_native_protobuf.h"
30 #include <google/protobuf/descriptor.pb.h>
31 
32 #include "../../binary.h"
33 #include "../../codec.h"
34 #include "test.pb.h"
35 using namespace dccl::test;
36 
37 dccl::Codec codec;
38 
39 // ensure we link in dccl_native_protobuf.so
41 
42 int main(int /*argc*/, char* /*argv*/ [])
43 {
44  dccl::dlog.connect(dccl::logger::ALL, &std::cerr);
45 
46  codec.load_library(DCCL_NATIVE_PROTOBUF_NAME);
47 
48  {
49  TestMsg msg_in;
50  msg_in.set_double_oneof1(10.56);
51  msg_in.mutable_msg_oneof2()->set_val(100.123);
52 
53  codec.load(msg_in.GetDescriptor());
54  codec.info(msg_in.GetDescriptor());
55 
56  std::cout << "Message in:\n" << msg_in.DebugString() << std::endl;
57  std::cout << "Try encode..." << std::endl;
58  std::string bytes;
59  codec.encode(&bytes, msg_in);
60  std::cout << "... got bytes (hex): " << dccl::hex_encode(bytes) << std::endl;
61 
62  std::cout << "Try decode..." << std::endl;
63  std::cout << codec.max_size(msg_in.GetDescriptor()) << std::endl;
64 
65  TestMsg msg_out;
66  codec.decode(bytes, &msg_out);
67 
68  std::cout << "... got Message out:\n" << msg_out.DebugString() << std::endl;
69  assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
70  }
71 
72  // test non standard codec
73  {
74  TestMsg msg_in;
75  msg_in.set_non_default_double(1200.56);
76  msg_in.mutable_msg_oneof2()->set_val(100.123);
77 
78  std::cout << "Message in:\n" << msg_in.DebugString() << std::endl;
79  std::cout << "Try encode..." << std::endl;
80  std::string bytes;
81  codec.encode(&bytes, msg_in);
82  std::cout << "... got bytes (hex): " << dccl::hex_encode(bytes) << std::endl;
83 
84  std::cout << "Try decode..." << std::endl;
85  std::cout << codec.max_size(msg_in.GetDescriptor()) << std::endl;
86 
87  TestMsg msg_out;
88  codec.decode(bytes, &msg_out);
89 
90  std::cout << "... got Message out:\n" << msg_out.DebugString() << std::endl;
91  assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
92  }
93 
94  // Test exception thrown for in_head oneof fields
95  try
96  {
97  InvalidTestMsg msg_in;
98  msg_in.set_double_oneof1(10.56);
99  msg_in.mutable_msg_oneof2()->set_val(100.123);
100 
101  codec.load(msg_in.GetDescriptor());
102  codec.info(msg_in.GetDescriptor());
103  }
104  catch (dccl::Exception& e)
105  {
106  // expect throw dccl::Exception with in_head == true
107  assert(
108  strcmp(e.what(),
109  "Oneof field used in header - oneof fields cannot be encoded in the header.") ==
110  0);
111  }
112 }
dccl::test
Unit test namespace.
Definition: test.cpp:44
dccl::Exception
Exception class for DCCL.
Definition: exception.h:46
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
dccl::native_protobuf::EnumFieldCodec
Definition: dccl_native_protobuf.h:393