A Discrete-Event Network Simulator
API
bit-serializer-test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Universita' di Firenze, Italy
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
18  */
19 #include "ns3/bit-deserializer.h"
20 #include "ns3/bit-serializer.h"
21 #include "ns3/test.h"
22 
23 #include <cstdarg>
24 #include <iostream>
25 #include <sstream>
26 
27 using namespace ns3;
28 
36 {
37  public:
38  void DoRun() override;
40 };
41 
43  : TestCase("BitSerializer")
44 {
45 }
46 
47 void
49 {
50  BitSerializer testBitSerializer1;
51 
52  testBitSerializer1.PushBits(0x55, 7);
53  testBitSerializer1.PushBits(0x7, 3);
54  testBitSerializer1.PushBits(0x0, 2);
55 
56  std::vector<uint8_t> result = testBitSerializer1.GetBytes();
57  NS_TEST_EXPECT_MSG_EQ((result[0] == 0xab) && (result[1] == 0xc0),
58  true,
59  "Incorrect serialization " << std::hex << +result[0] << +result[1]
60  << " instead of " << 0xab << " " << 0xc0
61  << std::dec);
62 
63  BitSerializer testBitSerializer2;
64 
65  testBitSerializer2.PushBits(0x55, 7);
66  testBitSerializer2.PushBits(0x7, 3);
67  testBitSerializer2.PushBits(0x0, 2);
68 
69  testBitSerializer2.InsertPaddingAtEnd(false);
70 
71  result = testBitSerializer2.GetBytes();
72  NS_TEST_EXPECT_MSG_EQ((result[0] == 0x0a) && (result[1] == 0xbc),
73  true,
74  "Incorrect serialization " << std::hex << +result[0] << +result[1]
75  << " instead of " << 0x0a << " " << 0xbc
76  << std::dec);
77 }
78 
86 {
87  public:
88  void DoRun() override;
90 };
91 
93  : TestCase("BitDeserializer")
94 {
95 }
96 
97 void
99 {
100  uint16_t nibble1;
101  uint16_t nibble2;
102  uint16_t nibble3;
103  bool result;
104 
105  BitDeserializer testBitDeserializer1;
106  uint8_t test1[2];
107  test1[0] = 0xab;
108  test1[1] = 0xc0;
109 
110  testBitDeserializer1.PushBytes(test1, 2);
111  nibble1 = testBitDeserializer1.GetBits(7);
112  nibble2 = testBitDeserializer1.GetBits(3);
113  nibble3 = testBitDeserializer1.GetBits(2);
114  result = (nibble1 == 0x55) && (nibble2 == 0x7) && (nibble3 == 0x0);
115 
116  NS_TEST_EXPECT_MSG_EQ(result,
117  true,
118  "Incorrect deserialization " << std::hex << nibble1 << " " << nibble2
119  << " " << nibble3 << " << instead of "
120  << " " << 0x55 << " " << 0x7 << " " << 0x0
121  << std::dec);
122 
123  BitDeserializer testBitDeserializer2;
124  std::vector<uint8_t> test2;
125  test2.push_back(0xab);
126  test2.push_back(0xc0);
127 
128  testBitDeserializer2.PushBytes(test2);
129  nibble1 = testBitDeserializer2.GetBits(7);
130  nibble2 = testBitDeserializer2.GetBits(3);
131  nibble3 = testBitDeserializer2.GetBits(2);
132 
133  result = (nibble1 == 0x55) && (nibble2 == 0x7) && (nibble3 == 0x0);
134 
135  NS_TEST_EXPECT_MSG_EQ(result,
136  true,
137  "Incorrect deserialization " << std::hex << nibble1 << " " << nibble2
138  << " " << nibble3 << " << instead of "
139  << " " << 0x55 << " " << 0x7 << " " << 0x0
140  << std::dec);
141 }
142 
150 {
151  public:
153 };
154 
156  : TestSuite("bit-serializer", UNIT)
157 {
158  AddTestCase(new BitSerializerTest, TestCase::QUICK);
159  AddTestCase(new BitDeserializerTest, TestCase::QUICK);
160 }
161 
static BitSerializerTestSuite g_bitSerializerTest
Static variable for test initialization.
Bit deserialization test.
void DoRun() override
Implementation to actually run this TestCase.
Bit serialization test.
void DoRun() override
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:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
#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:251
Every class exported by the ns3 library is enclosed in the ns3 namespace.