Goby v2
test.cpp
1 // Copyright 2009-2018 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 //
5 //
6 // This file is part of the Goby Underwater Autonomy Project Binaries
7 // ("The Goby Binaries").
8 //
9 // The Goby Binaries are free software: you can redistribute them and/or modify
10 // them under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // The Goby Binaries are distributed in the hope that they will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
21 
22 // tests functionality of std::list<const google::protobuf::Message*> calls
23 
24 #include "goby/acomms/dccl.h"
25 #include "goby/common/time.h"
26 #include "goby/util/as.h"
27 #include "goby/util/binary.h"
28 #include "test.pb.h"
29 
30 using goby::acomms::operator<<;
31 
32 int main(int argc, char* argv[])
33 {
34  goby::glog.add_stream(goby::common::logger::DEBUG3, &std::cerr);
35  goby::glog.set_name(argv[0]);
36 
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  codec->info_repeated(descs, &std::cout);
62 
63  BOOST_FOREACH (const google::protobuf::Message* p, msgs)
64  {
65  static int i = 0;
66  std::cout << "Message " << ++i << " in:\n" << p->DebugString() << std::endl;
67  }
68 
69  codec->validate_repeated(descs);
70 
71  std::cout << "Try encode..." << std::endl;
72  std::string bytes1 = codec->encode_repeated(msgs) + std::string(4, '\0');
73  std::cout << "... got bytes (hex): " << goby::util::hex_encode(bytes1) << std::endl;
74  std::cout << "Try decode..." << std::endl;
75 
76  std::list<boost::shared_ptr<google::protobuf::Message> > msgs_out =
77  codec->decode_repeated<boost::shared_ptr<google::protobuf::Message> >(bytes1);
78 
79  std::list<const google::protobuf::Message*>::const_iterator in_it = msgs.begin();
80 
81  assert(msgs.size() == msgs_out.size());
82 
83  BOOST_FOREACH (boost::shared_ptr<google::protobuf::Message> p, msgs_out)
84  {
85  static int i = 0;
86  std::cout << "... got Message " << ++i << " out:\n" << p->DebugString() << std::endl;
87  assert((*in_it)->SerializeAsString() == p->SerializeAsString());
88  ++in_it;
89  }
90 
91  std::cout << "all tests passed" << std::endl;
92 }
void set_name(const std::string &s)
Set the name of the application that the logger is serving.
Definition: flex_ostream.h:67
static DCCLCodec * get()
DCCLCodec is a singleton class; use this to get a pointer to the class.
Definition: dccl.h:124
common::FlexOstream glog
Access the Goby logger through this object.
void add_stream(logger::Verbosity verbosity=logger::VERBOSE, std::ostream *os=0)
Attach a stream object (e.g. std::cout, std::ofstream, ...) to the logger with desired verbosity...
Definition: flex_ostream.h:96