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 usage of a custom DCCL ID codec
23 
24 #include "dccl/codec.h"
25 #include "dccl/field_codec.h"
26 #include "test.pb.h"
27 using namespace dccl::test;
28 
29 using dccl::operator<<;
30 
31 namespace dccl
32 {
33  namespace test
34  {
36  {
37  private:
38  dccl::Bitset encode(const dccl::uint32& wire_value);
39 
40  dccl::Bitset encode()
41  { return encode(MINI_ID_OFFSET); }
42 
43  dccl::uint32 decode(dccl::Bitset* bits)
44  { return bits->to_ulong() + MINI_ID_OFFSET; }
45 
46  unsigned size()
47  { return MINI_ID_SIZE; }
48 
49  void validate()
50  { }
51 
52 
53  // Add this value when decoding to put us safely in our own namespace
54  // from the normal default DCCL Codec
55  enum { MINI_ID_OFFSET = 1000000 };
56  enum { MINI_ID_SIZE = 6 };
57  };
58  }
59 }
60 
61 
62 bool double_cmp(double a, double b, int precision)
63 {
64  int a_whole = a;
65  int b_whole = b;
66 
67  int a_part = (a-a_whole)*pow(10.0, precision);
68  int b_part = (b-b_whole)*pow(10.0, precision);
69 
70  return (a_whole == b_whole) && (a_part == b_part);
71 }
72 
73 dccl::Bitset dccl::test::MicroModemMiniPacketDCCLIDCodec::encode(const dccl::uint32& wire_value)
74 {
75  // 16 bits, only 13 are useable, so
76  // 3 "blank bits" + 3 bits for us
77  return dccl::Bitset(MINI_ID_SIZE, wire_value - MINI_ID_OFFSET);
78 }
79 
80 
81 int main(int argc, char* argv[])
82 {
83  dccl::dlog.connect(dccl::logger::ALL, &std::cerr);
84 
85  {
86  dccl::FieldCodecManager::add<dccl::test::MicroModemMiniPacketDCCLIDCodec>("mini_id_codec");
87  dccl::Codec codec("mini_id_codec");
88  codec.set_crypto_passphrase("309ldskjfla39");
89 
90  codec.load<MiniUser>();
91  codec.info<MiniUser>(&dccl::dlog);
92 
93  MiniUser mini_user_msg_in, mini_user_msg_out;
94  mini_user_msg_in.set_user(876);
95  std::string encoded;
96  codec.encode(&encoded, mini_user_msg_in);
97  codec.decode(encoded, &mini_user_msg_out);
98  assert(mini_user_msg_out.SerializeAsString() == mini_user_msg_in.SerializeAsString());
99 
100  codec.load<MiniOWTT>();
101  codec.info<MiniOWTT>(&dccl::dlog);
102 
103  MiniOWTT mini_owtt_in, mini_owtt_out;
104  mini_owtt_in.set_clock_mode(3);
105  mini_owtt_in.set_tod(12);
106  mini_owtt_in.set_user(13);
107 
108  encoded.clear();
109  codec.encode(&encoded, mini_owtt_in);
110  std::cout << "OWTT as hex: " << dccl::hex_encode(encoded) << std::endl;
111 
112  codec.decode(encoded, &mini_owtt_out);
113  assert(mini_owtt_out.SerializeAsString() == mini_owtt_in.SerializeAsString());
114 
115  codec.load<MiniAbort>();
116  codec.info<MiniAbort>(&dccl::dlog);
117 
118  MiniAbort mini_abort_in, mini_abort_out;
119  mini_abort_in.set_user(130);
120  encoded.clear();
121  codec.encode(&encoded, mini_abort_in);
122  codec.decode(encoded, &mini_abort_out);
123  assert(mini_abort_out.SerializeAsString() == mini_abort_in.SerializeAsString());
124  }
125 
126  std::cout << "all tests passed" << std::endl;
127 
128 }
129 
dccl::test
Unit test namespace.
Definition: test.cpp:44
dccl::Bitset::to_ulong
unsigned long to_ulong() const
Returns the value of the Bitset as an unsigned long integer. Equivalent to to<unsigned long>().
Definition: bitset.h:284
dccl
Dynamic Compact Control Language namespace.
Definition: gen_units_class_plugin.h:49
dccl::TypedFixedFieldCodec
Base class for static-typed field encoders/decoders that use a fixed number of bits on the wire regar...
Definition: field_codec_fixed.h:34
dccl::uint32
google::protobuf::uint32 uint32
an unsigned 32 bit integer
Definition: common.h:55
dccl::Codec
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
Definition: codec.h:56
dccl::test::MicroModemMiniPacketDCCLIDCodec
Definition: test.cpp:35
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:161
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::Bitset
A variable size container of bits (subclassed from std::deque<bool>) with an optional hierarchy....
Definition: bitset.h:38