A Discrete-Event Network Simulator
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
16using namespace ns3;
17
18/**
19 * @ingroup network-test
20 * @ingroup tests
21 *
22 * @brief Bit serialization test
23 */
25{
26 public:
27 void DoRun() override;
29};
30
32 : TestCase("BitSerializer")
33{
34}
35
36void
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 */
75{
76 public:
77 void DoRun() override;
79};
80
82 : TestCase("BitDeserializer")
83{
84}
85
86void
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
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
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 */
139{
140 public:
142};
143
145 : TestSuite("bit-serializer", Type::UNIT)
146{
147 AddTestCase(new BitSerializerTest, TestCase::Duration::QUICK);
148 AddTestCase(new BitDeserializerTest, TestCase::Duration::QUICK);
149}
150
151static BitSerializerTestSuite g_bitSerializerTest; //!< Static variable for test initialization
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:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
#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
Every class exported by the ns3 library is enclosed in the ns3 namespace.