DCCL v5
Loading...
Searching...
No Matches
test.cpp
1// Copyright 2012-2023:
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"
29using namespace dccl::test;
30
31
32namespace dccl
33{
34namespace 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
63bool 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
74dccl::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
81int main(int argc, char* argv[])
82{
83 bool verbose = false;
84 for (int i = 1; i < argc; ++i)
85 {
86 if (argv[i] && argv[i][0] == '-' && argv[i][1] == 'v' && argv[i][2] == '\0')
87 verbose = true;
88 }
89
90 dccl::dlog.connect(verbose ? dccl::logger::ALL : dccl::logger::WARN_PLUS, &std::cerr);
91
92 {
94 codec.set_crypto_passphrase("309ldskjfla39");
95
96 codec.load<MiniUser>();
97 codec.info<MiniUser>(&dccl::dlog);
98
99 MiniUser mini_user_msg_in, mini_user_msg_out;
100 mini_user_msg_in.set_user(876);
101 std::string encoded;
102 codec.encode(&encoded, mini_user_msg_in);
103 codec.decode(encoded, &mini_user_msg_out);
104 assert(mini_user_msg_out.SerializeAsString() == mini_user_msg_in.SerializeAsString());
105
106 codec.load<MiniOWTT>();
107 codec.info<MiniOWTT>(&dccl::dlog);
108
109 MiniOWTT mini_owtt_in, mini_owtt_out;
110 mini_owtt_in.set_clock_mode(3);
111 mini_owtt_in.set_tod(12);
112 mini_owtt_in.set_user(13);
113
114 encoded.clear();
115 codec.encode(&encoded, mini_owtt_in);
116 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "OWTT as hex: " << dccl::hex_encode(encoded) << std::endl;
117
118 codec.decode(encoded, &mini_owtt_out);
119 assert(mini_owtt_out.SerializeAsString() == mini_owtt_in.SerializeAsString());
120
121 codec.load<MiniAbort>();
122 codec.info<MiniAbort>(&dccl::dlog);
123
124 MiniAbort mini_abort_in, mini_abort_out;
125 mini_abort_in.set_user(130);
126 encoded.clear();
127 codec.encode(&encoded, mini_abort_in);
128 codec.decode(encoded, &mini_abort_out);
129 assert(mini_abort_out.SerializeAsString() == mini_abort_in.SerializeAsString());
130 }
131
132 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "all tests passed" << std::endl;
133}
A variable size container of bits (subclassed from std::deque<bool>) with an optional hierarchy....
Definition bitset.h:43
unsigned long to_ulong() const
Returns the value of the Bitset as an unsigned long integer. Equivalent to to<unsigned long>().
Definition bitset.h:275
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
Definition codec.h:61
bool is(logger::Verbosity verbosity, logger::Group group=logger::GENERAL)
Indicates the verbosity of the Logger until the next std::flush or std::endl. The boolean return is u...
Definition logger.h:191
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:213
Base class for static-typed field encoders/decoders that use a fixed number of bits on the wire regar...
Unit test namespace.
Definition test.cpp:45
Dynamic Compact Control Language namespace.
Definition any.h:28
google::protobuf::uint32 uint32
an unsigned 32 bit integer
Definition common.h:56
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:95