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 fixed id header
23 
24 #include "goby/acomms/dccl.h"
25 #include "test.pb.h"
26 
27 using goby::acomms::operator<<;
28 
29 int main(int argc, char* argv[])
30 {
31  goby::glog.add_stream(goby::common::logger::DEBUG3, &std::cerr);
32  goby::glog.set_name(argv[0]);
33 
35 
36  ShortIDMsg short_id_msg;
37  codec->validate(short_id_msg.GetDescriptor());
38  codec->info(short_id_msg.GetDescriptor(), &goby::glog);
39 
40  std::string encoded;
41  assert(codec->size(short_id_msg) == 1);
42  codec->encode(&encoded, short_id_msg);
43  codec->decode(encoded, &short_id_msg);
44 
45  LongIDMsg long_id_msg;
46  codec->validate(long_id_msg.GetDescriptor());
47  codec->info(long_id_msg.GetDescriptor(), &goby::glog);
48  assert(codec->size(long_id_msg) == 2);
49  codec->encode(&encoded, long_id_msg);
50  codec->decode(encoded, &long_id_msg);
51 
52  ShortIDEdgeMsg short_id_edge_msg;
53  codec->validate(short_id_edge_msg.GetDescriptor());
54  codec->info(short_id_edge_msg.GetDescriptor(), &goby::glog);
55  assert(codec->size(short_id_edge_msg) == 1);
56  codec->encode(&encoded, short_id_edge_msg);
57  codec->decode(encoded, &short_id_edge_msg);
58 
59  LongIDEdgeMsg long_id_edge_msg;
60  codec->validate(long_id_edge_msg.GetDescriptor());
61  codec->info(long_id_edge_msg.GetDescriptor(), &goby::glog);
62  codec->encode(&encoded, long_id_edge_msg);
63  codec->decode(encoded, &long_id_edge_msg);
64  assert(codec->size(long_id_edge_msg) == 2);
65 
66  TooLongIDMsg too_long_id_msg;
67  // should fail validation
68  try
69  {
70  codec->validate(too_long_id_msg.GetDescriptor());
71  assert(false);
72  }
73  catch (goby::acomms::DCCLException& e)
74  {
75  }
76 
77  ShortIDMsgWithData short_id_msg_with_data;
78  codec->validate(short_id_msg_with_data.GetDescriptor());
79  codec->info(short_id_msg_with_data.GetDescriptor(), &goby::glog);
80 
81  short_id_msg_with_data.set_in_head(42);
82  short_id_msg_with_data.set_in_body(37);
83  codec->encode(&encoded, short_id_msg_with_data);
84  codec->decode(encoded, &short_id_msg_with_data);
85 
86  std::cout << "all tests passed" << std::endl;
87 }
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