A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
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
27
using namespace
ns3
;
28
35
class
BitSerializerTest
:
public
TestCase
36
{
37
public
:
38
virtual
void
DoRun (
void
);
39
BitSerializerTest
();
40
};
41
42
BitSerializerTest::BitSerializerTest
()
43
:
TestCase
(
"BitSerializer"
)
44
{}
45
46
void
BitSerializerTest::DoRun
()
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
77
class
BitDeserializerTest
:
public
TestCase
78
{
79
public
:
80
virtual
void
DoRun
(
void
);
81
BitDeserializerTest
();
82
};
83
84
BitDeserializerTest::BitDeserializerTest
()
85
:
TestCase
(
"BitDeserializer"
)
86
{}
87
88
void
BitDeserializerTest::DoRun
()
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
134
class
BitSerializerTestSuite
:
public
TestSuite
135
{
136
public
:
137
BitSerializerTestSuite
();
138
};
139
140
141
BitSerializerTestSuite::BitSerializerTestSuite
()
142
:
TestSuite
(
"bit-serializer"
, UNIT)
143
{
144
AddTestCase
(
new
BitSerializerTest
, TestCase::QUICK);
145
AddTestCase
(
new
BitDeserializerTest
, TestCase::QUICK);
146
}
147
148
static
BitSerializerTestSuite
g_bitSerializerTest
;
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition:
test.cc:299
ns3::BitDeserializer::GetBits
uint64_t GetBits(uint8_t size)
Pops a given number of bits from the blob front.
Definition:
bit-deserializer.cc:60
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::BitDeserializer::PushBytes
void PushBytes(std::vector< uint8_t > bytes)
Pushes some bytes into the blob to be deserialized.
Definition:
bit-deserializer.cc:36
BitSerializerTest
Bit serialization test.
Definition:
bit-serializer-test.cc:36
ns3::BitSerializer::InsertPaddingAtEnd
void InsertPaddingAtEnd(bool padAtEnd)
Toggles the padding insertion policy.
Definition:
bit-serializer.cc:37
BitDeserializerTest::DoRun
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition:
bit-serializer-test.cc:88
BitDeserializerTest
Bit deserialization test.
Definition:
bit-serializer-test.cc:78
ns3::TestCase
encapsulates test code
Definition:
test.h:1154
ns3::BitSerializer::PushBits
void PushBits(uint64_t value, uint8_t significantBits)
Pushes a number of bits in the blob.
Definition:
bit-serializer.cc:59
BitDeserializerTest::BitDeserializerTest
BitDeserializerTest()
Definition:
bit-serializer-test.cc:84
ns3::BitSerializer::GetBytes
std::vector< uint8_t > GetBytes()
Get the bytes representation of the blob.
Definition:
bit-serializer.cc:80
BitSerializerTestSuite::BitSerializerTestSuite
BitSerializerTestSuite()
Definition:
bit-serializer-test.cc:141
g_bitSerializerTest
static BitSerializerTestSuite g_bitSerializerTest
Static variable for test initialization.
Definition:
bit-serializer-test.cc:148
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:283
BitSerializerTest::BitSerializerTest
BitSerializerTest()
Definition:
bit-serializer-test.cc:42
BitSerializerTest::DoRun
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition:
bit-serializer-test.cc:46
ns3::TestSuite
A suite of tests to run.
Definition:
test.h:1344
ns3::BitDeserializer
Bit deserializer.
Definition:
bit-deserializer.h:43
ns3::BitSerializer
Bit serializer.
Definition:
bit-serializer.h:59
BitSerializerTestSuite
Packet Metadata TestSuite.
Definition:
bit-serializer-test.cc:135
src
network
test
bit-serializer-test.cc
Generated on Fri Oct 1 2021 17:03:29 for ns-3 by
1.8.20