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 //
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"
32 using namespace dccl::test;
33 
34 int main(int /*argc*/, char* /*argv*/ [])
35 {
36  dccl::dlog.connect(dccl::logger::ALL, &std::cerr);
37 
38  dccl::Codec codec;
39 
40  GobyMessage1 msg_in1;
41  GobyMessage2 msg_in2;
42  GobyMessage3 msg_in3;
43  GobyMessage3 msg_in4;
44 
45  msg_in1.set_int32_val(1);
46  msg_in2.set_bool_val(false);
47  msg_in3.set_string_val("string1");
48  msg_in4.set_string_val("string2");
49 
50  std::list<const google::protobuf::Message*> msgs;
51  msgs.push_back(&msg_in1);
52  msgs.push_back(&msg_in2);
53  msgs.push_back(&msg_in3);
54  msgs.push_back(&msg_in4);
55 
56  std::list<const google::protobuf::Descriptor*> descs;
57  descs.push_back(msg_in1.GetDescriptor());
58  descs.push_back(msg_in2.GetDescriptor());
59  descs.push_back(msg_in3.GetDescriptor());
60  descs.push_back(msg_in4.GetDescriptor());
61 
62  for (auto desc : descs)
63  {
64  codec.info(desc, &std::cout);
65  codec.load(desc);
66  }
67 
68  std::string bytes1;
69  for (auto msg : msgs)
70  {
71  static int i = 0;
72  std::cout << "Message " << ++i << " in:\n" << msg->DebugString() << std::endl;
73  std::cout << "Try encode..." << std::endl;
74  codec.encode(&bytes1, *msg);
75  }
76  bytes1 += std::string(4, '\0');
77 
78  std::cout << "... got bytes (hex): " << dccl::hex_encode(bytes1) << std::endl;
79  std::cout << "Try decode..." << std::endl;
80 
81  // non-destructive
82  {
83  std::list<std::shared_ptr<google::protobuf::Message>> msgs_out;
84  try
85  {
86  std::string::iterator begin = bytes1.begin(), end = bytes1.end();
87  while (begin != end)
88  {
89  auto it = codec.loaded().find(codec.id(begin, end));
90  if (it == codec.loaded().end())
91  break;
92 
93  std::shared_ptr<google::protobuf::Message> msg =
95  begin = codec.decode(begin, end, msg.get());
96  msgs_out.push_back(msg);
97  }
98  }
99  catch (dccl::Exception& e)
100  {
101  std::cout << e.what() << std::endl;
102  }
103 
104  std::list<const google::protobuf::Message*>::const_iterator in_it = msgs.begin();
105 
106  assert(msgs.size() == msgs_out.size());
107 
108  for (const auto& it : msgs_out)
109  {
110  static int i = 0;
111  std::cout << "... got Message " << ++i << " out:\n" << it->DebugString() << std::endl;
112  assert((*in_it)->SerializeAsString() == it->SerializeAsString());
113  ++in_it;
114  }
115  }
116 
117  // destructive
118  {
119  std::list<std::shared_ptr<google::protobuf::Message>> msgs_out;
120  try
121  {
122  while (!bytes1.empty())
123  msgs_out.push_back(
124  codec.decode<std::shared_ptr<google::protobuf::Message>>(&bytes1));
125  }
126  catch (dccl::Exception& e)
127  {
128  std::cout << e.what() << std::endl;
129  }
130 
131  std::list<const google::protobuf::Message*>::const_iterator in_it = msgs.begin();
132 
133  assert(msgs.size() == msgs_out.size());
134 
135  for (const auto& it : msgs_out)
136  {
137  static int i = 0;
138  std::cout << "... got Message " << ++i << " out:\n" << it->DebugString() << std::endl;
139  assert((*in_it)->SerializeAsString() == it->SerializeAsString());
140  ++in_it;
141  }
142  }
143 
144  std::cout << "all tests passed" << std::endl;
145 }
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::DynamicProtobufManager::new_protobuf_message
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.
Definition: dynamic_protobuf_manager.h:76