A Discrete-Event Network Simulator
API
bit-serializer-test.cc
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2020 Universita' di Firenze, Italy
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
19 */
20#include <cstdarg>
21#include <iostream>
22#include <sstream>
23#include "ns3/test.h"
24#include "ns3/bit-serializer.h"
25#include "ns3/bit-deserializer.h"
26
27using namespace ns3;
28
36{
37public:
38 virtual void DoRun (void);
40};
41
43 : TestCase ("BitSerializer")
44{}
45
47{
48 BitSerializer testBitSerializer1;
49
50 testBitSerializer1.PushBits (0x55, 7);
51 testBitSerializer1.PushBits (0x7, 3);
52 testBitSerializer1.PushBits (0x0, 2);
53
54 std::vector<uint8_t> result = testBitSerializer1.GetBytes ();
55 NS_TEST_EXPECT_MSG_EQ ((result[0] == 0xab) && (result[1] == 0xc0), true,
56 "Incorrect serialization " << std::hex << +result[0] << +result[1] << " instead of " << 0xab << " " << 0xc0 << std::dec);
57
58 BitSerializer testBitSerializer2;
59
60 testBitSerializer2.PushBits (0x55, 7);
61 testBitSerializer2.PushBits (0x7, 3);
62 testBitSerializer2.PushBits (0x0, 2);
63
64 testBitSerializer2.InsertPaddingAtEnd (false);
65
66 result = testBitSerializer2.GetBytes ();
67 NS_TEST_EXPECT_MSG_EQ ((result[0] == 0x0a) && (result[1] == 0xbc), true,
68 "Incorrect serialization " << std::hex << +result[0] << +result[1] << " instead of " << 0x0a << " " << 0xbc << std::dec);
69}
70
78{
79public:
80 virtual void DoRun (void);
82};
83
85 : TestCase ("BitDeserializer")
86{}
87
89{
90 uint16_t nibble1;
91 uint16_t nibble2;
92 uint16_t nibble3;
93 bool result;
94
95 BitDeserializer testBitDeserializer1;
96 uint8_t test1[2];
97 test1[0] = 0xab;
98 test1[1] = 0xc0;
99
100 testBitDeserializer1.PushBytes (test1, 2);
101 nibble1 = testBitDeserializer1.GetBits (7);
102 nibble2 = testBitDeserializer1.GetBits (3);
103 nibble3 = testBitDeserializer1.GetBits (2);
104 result = (nibble1 == 0x55) && (nibble2 == 0x7) && (nibble3 == 0x0);
105
106 NS_TEST_EXPECT_MSG_EQ (result, true,
107 "Incorrect deserialization " << std::hex << nibble1 << " " << nibble2 << " " << nibble3 <<
108 " << instead of " << " " << 0x55 << " " << 0x7 << " " << 0x0 << std::dec);
109
110 BitDeserializer testBitDeserializer2;
111 std::vector<uint8_t> test2;
112 test2.push_back (0xab);
113 test2.push_back (0xc0);
114
115 testBitDeserializer2.PushBytes (test2);
116 nibble1 = testBitDeserializer2.GetBits (7);
117 nibble2 = testBitDeserializer2.GetBits (3);
118 nibble3 = testBitDeserializer2.GetBits (2);
119
120 result = (nibble1 == 0x55) && (nibble2 == 0x7) && (nibble3 == 0x0);
121
122 NS_TEST_EXPECT_MSG_EQ (result, true,
123 "Incorrect deserialization " << std::hex << nibble1 << " " << nibble2 << " " << nibble3 <<
124 " << instead of " << " " << 0x55 << " " << 0x7 << " " << 0x0 << std::dec);
125
126}
127
135{
136public:
138};
139
140
142 : TestSuite ("bit-serializer", UNIT)
143{
144 AddTestCase (new BitSerializerTest, TestCase::QUICK);
145 AddTestCase (new BitDeserializerTest, TestCase::QUICK);
146}
147
static BitSerializerTestSuite g_bitSerializerTest
Static variable for test initialization.
Bit deserialization test.
virtual void DoRun(void)
Implementation to actually run this TestCase.
Bit serialization test.
virtual void DoRun(void)
Implementation to actually run this TestCase.
Packet Metadata TestSuite.
Bit deserializer.
void PushBytes(std::vector< uint8_t > bytes)
Pushes some bytes into the blob to be deserialized.
uint64_t GetBits(uint8_t size)
Pops a given number of bits from the blob front.
Bit serializer.
void PushBits(uint64_t value, uint8_t significantBits)
Pushes a number of bits in the blob.
std::vector< uint8_t > GetBytes()
Get the bytes representation of the blob.
void InsertPaddingAtEnd(bool padAtEnd)
Toggles the padding insertion policy.
encapsulates test code
Definition: test.h:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
A suite of tests to run.
Definition: test.h:1188
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition: test.h:240
Every class exported by the ns3 library is enclosed in the ns3 namespace.