DCCL v5
Loading...
Searching...
No Matches
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
31#include "../../binary.h"
32#include "../../codec.h"
33#include "test.pb.h"
34using namespace dccl::test;
35
36dccl::Codec codec;
37
38// ensure we link in dccl_native_protobuf.so
40
41int main(int argc, char* argv[])
42{
43 bool verbose = false;
44 for (int i = 1; i < argc; ++i)
45 {
46 if (argv[i] && argv[i][0] == '-' && argv[i][1] == 'v' && argv[i][2] == '\0')
47 verbose = true;
48 }
49
50 dccl::dlog.connect(verbose ? dccl::logger::ALL : dccl::logger::WARN_PLUS, &std::cerr);
51
52 codec.load_library(DCCL_NATIVE_PROTOBUF_NAME);
53
54 {
55 TestMsg msg_in;
56 msg_in.set_double_oneof1(10.56);
57 msg_in.mutable_msg_oneof2()->set_val(100.123);
58
59 codec.load(msg_in.GetDescriptor());
60 codec.info(msg_in.GetDescriptor());
61
62 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Message in:\n" << msg_in.DebugString() << std::endl;
63 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try encode..." << std::endl;
64 std::string bytes;
65 codec.encode(&bytes, msg_in);
66 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got bytes (hex): " << dccl::hex_encode(bytes) << std::endl;
67
68 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try decode..." << std::endl;
69 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << codec.max_size(msg_in.GetDescriptor()) << std::endl;
70
71 TestMsg msg_out;
72 codec.decode(bytes, &msg_out);
73
74 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got Message out:\n" << msg_out.DebugString() << std::endl;
75 assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
76 }
77
78 // test non standard codec
79 {
80 TestMsg msg_in;
81 msg_in.set_non_default_double(1200.56);
82 msg_in.mutable_msg_oneof2()->set_val(100.123);
83
84 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Message in:\n" << msg_in.DebugString() << std::endl;
85 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try encode..." << std::endl;
86 std::string bytes;
87 codec.encode(&bytes, msg_in);
88 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got bytes (hex): " << dccl::hex_encode(bytes) << std::endl;
89
90 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try decode..." << std::endl;
91 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << codec.max_size(msg_in.GetDescriptor()) << std::endl;
92
93 TestMsg msg_out;
94 codec.decode(bytes, &msg_out);
95
96 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got Message out:\n" << msg_out.DebugString() << std::endl;
97 assert(msg_in.SerializeAsString() == msg_out.SerializeAsString());
98 }
99
100 // Test exception thrown for in_head oneof fields
101 try
102 {
103 InvalidTestMsg msg_in;
104 msg_in.set_double_oneof1(10.56);
105 msg_in.mutable_msg_oneof2()->set_val(100.123);
106
107 codec.load(msg_in.GetDescriptor());
108 codec.info(msg_in.GetDescriptor());
109 }
110 catch (dccl::Exception& e)
111 {
112 // expect throw dccl::Exception with in_head == true
113 assert(
114 strcmp(e.what(),
115 "Oneof field used in header - oneof fields cannot be encoded in the header.") ==
116 0);
117 }
118}
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
Definition codec.h:61
Exception class for DCCL.
Definition exception.h:47
bool is(logger::Verbosity verbosity, logger::Group group=logger::GENERAL)
Indicates the verbosity of the Logger until the next std::flush or std::endl. The boolean return is u...
Definition logger.h:191
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:213
Unit test namespace.
Definition test.cpp:45
Dynamic Compact Control Language namespace.
Definition any.h:28
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:95