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