A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
Loading...
Searching...
No Matches
bit-serializer-test.cc
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2020 Universita' di Firenze, Italy
3
*
4
* SPDX-License-Identifier: GPL-2.0-only
5
*
6
* Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
7
*/
8
#include "ns3/bit-deserializer.h"
9
#include "ns3/bit-serializer.h"
10
#include "ns3/test.h"
11
12
#include <cstdarg>
13
#include <iostream>
14
#include <sstream>
15
16
using namespace
ns3
;
17
18
/**
19
* @ingroup network-test
20
* @ingroup tests
21
*
22
* @brief Bit serialization test
23
*/
24
class
BitSerializerTest
:
public
TestCase
25
{
26
public
:
27
void
DoRun
()
override
;
28
BitSerializerTest
();
29
};
30
31
BitSerializerTest::BitSerializerTest
()
32
:
TestCase
(
"BitSerializer"
)
33
{
34
}
35
36
void
37
BitSerializerTest::DoRun
()
38
{
39
BitSerializer
testBitSerializer1;
40
41
testBitSerializer1.
PushBits
(0x55, 7);
42
testBitSerializer1.
PushBits
(0x7, 3);
43
testBitSerializer1.
PushBits
(0x0, 2);
44
45
std::vector<uint8_t> result = testBitSerializer1.
GetBytes
();
46
NS_TEST_EXPECT_MSG_EQ
((result[0] == 0xab) && (result[1] == 0xc0),
47
true
,
48
"Incorrect serialization "
<< std::hex << +result[0] << +result[1]
49
<<
" instead of "
<< 0xab <<
" "
<< 0xc0
50
<< std::dec);
51
52
BitSerializer
testBitSerializer2;
53
54
testBitSerializer2.
PushBits
(0x55, 7);
55
testBitSerializer2.
PushBits
(0x7, 3);
56
testBitSerializer2.
PushBits
(0x0, 2);
57
58
testBitSerializer2.
InsertPaddingAtEnd
(
false
);
59
60
result = testBitSerializer2.
GetBytes
();
61
NS_TEST_EXPECT_MSG_EQ
((result[0] == 0x0a) && (result[1] == 0xbc),
62
true
,
63
"Incorrect serialization "
<< std::hex << +result[0] << +result[1]
64
<<
" instead of "
<< 0x0a <<
" "
<< 0xbc
65
<< std::dec);
66
}
67
68
/**
69
* @ingroup network-test
70
* @ingroup tests
71
*
72
* @brief Bit deserialization test
73
*/
74
class
BitDeserializerTest
:
public
TestCase
75
{
76
public
:
77
void
DoRun
()
override
;
78
BitDeserializerTest
();
79
};
80
81
BitDeserializerTest::BitDeserializerTest
()
82
:
TestCase
(
"BitDeserializer"
)
83
{
84
}
85
86
void
87
BitDeserializerTest::DoRun
()
88
{
89
uint16_t nibble1;
90
uint16_t nibble2;
91
uint16_t nibble3;
92
bool
result;
93
94
BitDeserializer
testBitDeserializer1;
95
uint8_t test1[2];
96
test1[0] = 0xab;
97
test1[1] = 0xc0;
98
99
testBitDeserializer1.
PushBytes
(test1, 2);
100
nibble1 = testBitDeserializer1.
GetBits
(7);
101
nibble2 = testBitDeserializer1.
GetBits
(3);
102
nibble3 = testBitDeserializer1.
GetBits
(2);
103
result = (nibble1 == 0x55) && (nibble2 == 0x7) && (nibble3 == 0x0);
104
105
NS_TEST_EXPECT_MSG_EQ
(result,
106
true
,
107
"Incorrect deserialization "
<< std::hex << nibble1 <<
" "
<< nibble2
108
<<
" "
<< nibble3 <<
" << instead of "
109
<<
" "
<< 0x55 <<
" "
<< 0x7 <<
" "
<< 0x0
110
<< std::dec);
111
112
BitDeserializer
testBitDeserializer2;
113
std::vector<uint8_t> test2;
114
test2.push_back(0xab);
115
test2.push_back(0xc0);
116
117
testBitDeserializer2.
PushBytes
(test2);
118
nibble1 = testBitDeserializer2.
GetBits
(7);
119
nibble2 = testBitDeserializer2.
GetBits
(3);
120
nibble3 = testBitDeserializer2.
GetBits
(2);
121
122
result = (nibble1 == 0x55) && (nibble2 == 0x7) && (nibble3 == 0x0);
123
124
NS_TEST_EXPECT_MSG_EQ
(result,
125
true
,
126
"Incorrect deserialization "
<< std::hex << nibble1 <<
" "
<< nibble2
127
<<
" "
<< nibble3 <<
" << instead of "
128
<<
" "
<< 0x55 <<
" "
<< 0x7 <<
" "
<< 0x0
129
<< std::dec);
130
}
131
132
/**
133
* @ingroup network-test
134
* @ingroup tests
135
*
136
* @brief Packet Metadata TestSuite
137
*/
138
class
BitSerializerTestSuite
:
public
TestSuite
139
{
140
public
:
141
BitSerializerTestSuite
();
142
};
143
144
BitSerializerTestSuite::BitSerializerTestSuite
()
145
:
TestSuite
(
"bit-serializer"
,
Type
::UNIT)
146
{
147
AddTestCase
(
new
BitSerializerTest
, TestCase::Duration::QUICK);
148
AddTestCase
(
new
BitDeserializerTest
, TestCase::Duration::QUICK);
149
}
150
151
static
BitSerializerTestSuite
g_bitSerializerTest
;
//!< Static variable for test initialization
g_bitSerializerTest
static BitSerializerTestSuite g_bitSerializerTest
Static variable for test initialization.
Definition
bit-serializer-test.cc:151
BitDeserializerTest
Bit deserialization test.
Definition
bit-serializer-test.cc:75
BitDeserializerTest::BitDeserializerTest
BitDeserializerTest()
Definition
bit-serializer-test.cc:81
BitDeserializerTest::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition
bit-serializer-test.cc:87
BitSerializerTest
Bit serialization test.
Definition
bit-serializer-test.cc:25
BitSerializerTest::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition
bit-serializer-test.cc:37
BitSerializerTest::BitSerializerTest
BitSerializerTest()
Definition
bit-serializer-test.cc:31
BitSerializerTestSuite
Packet Metadata TestSuite.
Definition
bit-serializer-test.cc:139
BitSerializerTestSuite::BitSerializerTestSuite
BitSerializerTestSuite()
Definition
bit-serializer-test.cc:144
ns3::BitDeserializer
Bit deserializer.
Definition
bit-deserializer.h:33
ns3::BitDeserializer::PushBytes
void PushBytes(std::vector< uint8_t > bytes)
Pushes some bytes into the blob to be deserialized.
Definition
bit-deserializer.cc:28
ns3::BitDeserializer::GetBits
uint64_t GetBits(uint8_t size)
Pops a given number of bits from the blob front.
Definition
bit-deserializer.cc:55
ns3::BitSerializer
Bit serializer.
Definition
bit-serializer.h:49
ns3::BitSerializer::PushBits
void PushBits(uint64_t value, uint8_t significantBits)
Pushes a number of bits in the blob.
Definition
bit-serializer.cc:54
ns3::BitSerializer::GetBytes
std::vector< uint8_t > GetBytes()
Get the bytes representation of the blob.
Definition
bit-serializer.cc:76
ns3::BitSerializer::InsertPaddingAtEnd
void InsertPaddingAtEnd(bool padAtEnd)
Toggles the padding insertion policy.
Definition
bit-serializer.cc:29
ns3::TestCase
encapsulates test code
Definition
test.h:1050
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition
test.cc:292
ns3::TestSuite
A suite of tests to run.
Definition
test.h:1267
ns3::TestSuite::Type
Type
Type of test.
Definition
test.h:1274
NS_TEST_EXPECT_MSG_EQ
#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:241
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
src
network
test
bit-serializer-test.cc
Generated on Thu Dec 12 2024 18:40:45 for ns-3 by
1.11.0