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//
8//
9// This file is part of the Dynamic Compact Control Language Library
10// ("DCCL").
11//
12// DCCL is free software: you can redistribute it and/or modify
13// it under the terms of the GNU Lesser General Public License as published by
14// the Free Software Foundation, either version 2.1 of the License, or
15// (at your option) any later version.
16//
17// DCCL is distributed in the hope that it will be useful,
18// but WITHOUT ANY WARRANTY; without even the implied warranty of
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20// GNU Lesser General Public License for more details.
21//
22// You should have received a copy of the GNU Lesser General Public License
23// along with DCCL. If not, see <http://www.gnu.org/licenses/>.
24// tests functionality of std::list<const google::protobuf::Message*> calls
25
26#include "../../binary.h"
27#include "../../codec.h"
28
29#include <list>
30
31#include "test.pb.h"
32using namespace dccl::test;
33
34int main(int argc, char* argv[])
35{
36 bool verbose = false;
37 for (int i = 1; i < argc; ++i)
38 {
39 if (argv[i] && argv[i][0] == '-' && argv[i][1] == 'v' && argv[i][2] == '\0')
40 verbose = true;
41 }
42
43 dccl::dlog.connect(verbose ? dccl::logger::ALL : dccl::logger::WARN_PLUS, &std::cerr);
44
45 dccl::Codec codec;
46
47 GobyMessage1 msg_in1;
48 GobyMessage2 msg_in2;
49 GobyMessage3 msg_in3;
50 GobyMessage3 msg_in4;
51
52 msg_in1.set_int32_val(1);
53 msg_in2.set_bool_val(false);
54 msg_in3.set_string_val("string1");
55 msg_in4.set_string_val("string2");
56
57 std::list<const google::protobuf::Message*> msgs;
58 msgs.push_back(&msg_in1);
59 msgs.push_back(&msg_in2);
60 msgs.push_back(&msg_in3);
61 msgs.push_back(&msg_in4);
62
63 std::list<const google::protobuf::Descriptor*> descs;
64 descs.push_back(msg_in1.GetDescriptor());
65 descs.push_back(msg_in2.GetDescriptor());
66 descs.push_back(msg_in3.GetDescriptor());
67 descs.push_back(msg_in4.GetDescriptor());
68
69 for (auto desc : descs)
70 {
71 if (dccl::dlog.is(dccl::logger::INFO))
72 {
73 codec.info(desc, &dccl::dlog);
74 }
75 codec.load(desc);
76 }
77
78 std::string bytes1;
79 for (auto msg : msgs)
80 {
81 static int i = 0;
82 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Message " << ++i << " in:\n" << msg->DebugString() << std::endl;
83 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try encode..." << std::endl;
84 codec.encode(&bytes1, *msg);
85 }
86 bytes1 += std::string(4, '\0');
87
88 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got bytes (hex): " << dccl::hex_encode(bytes1) << std::endl;
89 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Try decode..." << std::endl;
90
91 // non-destructive
92 {
93 std::list<std::shared_ptr<google::protobuf::Message>> msgs_out;
94 try
95 {
96 std::string::iterator begin = bytes1.begin(), end = bytes1.end();
97 while (begin != end)
98 {
99 auto it = codec.loaded().find(codec.id(begin, end));
100 if (it == codec.loaded().end())
101 break;
102
103 std::shared_ptr<google::protobuf::Message> msg =
105 begin = codec.decode(begin, end, msg.get());
106 msgs_out.push_back(msg);
107 }
108 }
109 catch (dccl::Exception& e)
110 {
111 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << e.what() << std::endl;
112 }
113
114 std::list<const google::protobuf::Message*>::const_iterator in_it = msgs.begin();
115
116 assert(msgs.size() == msgs_out.size());
117
118 for (const auto& it : msgs_out)
119 {
120 static int i = 0;
121 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got Message " << ++i << " out:\n" << it->DebugString() << std::endl;
122 assert((*in_it)->SerializeAsString() == it->SerializeAsString());
123 ++in_it;
124 }
125 }
126
127 // destructive
128 {
129 std::list<std::shared_ptr<google::protobuf::Message>> msgs_out;
130 try
131 {
132 while (!bytes1.empty())
133 msgs_out.push_back(
134 codec.decode<std::shared_ptr<google::protobuf::Message>>(&bytes1));
135 }
136 catch (dccl::Exception& e)
137 {
138 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << e.what() << std::endl;
139 }
140
141 std::list<const google::protobuf::Message*>::const_iterator in_it = msgs.begin();
142
143 assert(msgs.size() == msgs_out.size());
144
145 for (const auto& it : msgs_out)
146 {
147 static int i = 0;
148 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "... got Message " << ++i << " out:\n" << it->DebugString() << std::endl;
149 assert((*in_it)->SerializeAsString() == it->SerializeAsString());
150 ++in_it;
151 }
152 }
153
154 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "all tests passed" << std::endl;
155}
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
Definition codec.h:61
static GoogleProtobufMessagePointer new_protobuf_message(const std::string &protobuf_type_name, bool user_pool_first=false)
Create a new (empty) Google Protobuf message of a given type by name.
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