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