DCCL v4
test.cpp
1 // Copyright 2023:
2 // GobySoft, LLC (2013-)
3 // Community contributors (see AUTHORS file)
4 // File authors:
5 // Toby Schneider <toby@gobysoft.org>
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 <fstream>
26 
27 #include <google/protobuf/descriptor.pb.h>
28 
29 #include "../../binary.h"
30 #include "../../codec.h"
31 #include "test.pb.h"
32 using namespace dccl::test;
33 
34 dccl::Codec codec;
35 
36 template <typename TestMsg> void decode_check(const TestMsg& msg_in)
37 {
38  std::cout << "Message in:\n" << msg_in.DebugString() << std::endl;
39 
40  std::cout << "Try encode (in bounds)..." << std::endl;
41  std::string bytes;
42  codec.encode(&bytes, msg_in);
43  std::cout << "... got bytes (hex): " << dccl::hex_encode(bytes) << std::endl;
44 
45  std::cout << "Try decode..." << std::endl;
46 
47  TestMsg msg_out;
48  codec.decode(bytes, &msg_out);
49 
50  std::cout << "... got Message out:\n" << msg_out.DebugString() << std::endl;
51 }
52 
53 int main(int /*argc*/, char* /*argv*/[])
54 {
55  dccl::dlog.connect(dccl::logger::ALL, &std::cerr);
56 
57  codec.load<TestMsgOmit1>();
58  codec.info<TestMsgOmit1>();
59 
60  codec.load<TestMsgOmit2>();
61  codec.info<TestMsgOmit2>();
62 
63  codec.load<TestMsgNormal>();
64  codec.info<TestMsgNormal>();
65 
66  codec.info_all();
67 
68  {
69  TestMsgOmit1 msg_in;
70  msg_in.set_d(10.0);
71  msg_in.set_i(1000);
72 
73  decode_check(msg_in);
74  }
75 
76  {
77  TestMsgOmit2 msg_in;
78  msg_in.set_u(15);
79 
80  decode_check(msg_in);
81  }
82 
83  {
84  TestMsgNormal msg_in;
85  msg_in.set_f(2.3);
86 
87  decode_check(msg_in);
88  }
89 
90  {
91  TestMsgNormal msg_in;
92  msg_in.set_f(2.3);
93 
94  std::string bytes;
95  codec.encode(&bytes, msg_in);
96  TestMsgNormalId3 msg_out;
97 
98  // mismatch in ID used to encode vs decode
99  try
100  {
101  codec.decode(bytes, &msg_out);
102  }
103  catch (const dccl::Exception& e)
104  {
105  std::cout << "Caught expected exception: " << e.what() << std::endl;
106  }
107  }
108 
109  std::cout << "all tests passed" << std::endl;
110 }
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