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
header-serialization-test.h
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2022 Universita' degli Studi di Napoli Federico II
3
*
4
* SPDX-License-Identifier: GPL-2.0-only
5
*
6
* Authors: Davide Magrin <magrin.davide@gmail.com>
7
* Stefano Avallone <stavallo@unina.it>
8
*/
9
10
#ifndef NS3_TEST_HDR_SERIALIZE_H
11
#define NS3_TEST_HDR_SERIALIZE_H
12
13
#include "ns3/buffer.h"
14
#include "ns3/test.h"
15
16
namespace
ns3
17
{
18
19
/**
20
* Subclass of TestCase class adding the ability to test the serialization and
21
* deserialization of a Header object.
22
*/
23
class
HeaderSerializationTestCase
:
public
TestCase
24
{
25
protected
:
26
/**
27
* @brief Constructor.
28
*
29
* @param [in] name The name of the new TestCase created
30
*/
31
HeaderSerializationTestCase
(std::string name)
32
:
TestCase
(name)
33
{
34
}
35
36
public
:
37
/**
38
* Serialize the given header in a buffer, then create a new header by
39
* deserializing from the buffer and serialize the new header into a new buffer.
40
* Verify that the two buffers have the same size and the same content.
41
*
42
* @tparam T \deduced Type of the given header
43
* @tparam Args \deduced Type of arguments to pass to the constructor of the header
44
* @param [in] hdr the header to test
45
* @param [in] args the arguments to construct the new header
46
*/
47
template
<
typename
T,
typename
... Args>
48
void
TestHeaderSerialization
(
const
T& hdr, Args&&... args);
49
};
50
51
}
// namespace ns3
52
53
/***************************************************************
54
* Implementation of the templates declared above.
55
***************************************************************/
56
57
namespace
ns3
58
{
59
60
template
<
typename
T,
typename
... Args>
61
void
62
HeaderSerializationTestCase::TestHeaderSerialization
(
const
T& hdr, Args&&... args)
63
{
64
Buffer
buffer;
65
buffer.
AddAtStart
(hdr.GetSerializedSize());
66
hdr.Serialize(buffer.
Begin
());
67
68
T otherHdr(std::forward<Args>(args)...);
69
otherHdr.Deserialize(buffer.
Begin
());
70
Buffer
otherBuffer;
71
otherBuffer.
AddAtStart
(otherHdr.GetSerializedSize());
72
otherHdr.Serialize(otherBuffer.
Begin
());
73
74
NS_TEST_ASSERT_MSG_EQ
(buffer.
GetSize
(), otherBuffer.
GetSize
(),
"Size of buffers differs"
);
75
76
Buffer::Iterator
bufferIterator = buffer.
Begin
();
77
Buffer::Iterator
otherBufferIterator = otherBuffer.
Begin
();
78
for
(
uint32_t
j = 0; j < buffer.
GetSize
(); j++)
79
{
80
uint8_t bufferVal = bufferIterator.
ReadU8
();
81
uint8_t otherBufferVal = otherBufferIterator.
ReadU8
();
82
NS_TEST_EXPECT_MSG_EQ
(+bufferVal,
83
+otherBufferVal,
84
"Serialization -> Deserialization -> Serialization "
85
<<
"is different than Serialization at byte "
<< j);
86
}
87
}
88
89
}
// namespace ns3
90
91
#endif
/* NS3_TEST_HDR_SERIALIZE_H */
ns3::Buffer::Iterator
iterator in a Buffer instance
Definition
buffer.h:89
ns3::Buffer::Iterator::ReadU8
uint8_t ReadU8()
Definition
buffer.h:1016
ns3::Buffer
automatically resized byte buffer
Definition
buffer.h:83
ns3::Buffer::GetSize
uint32_t GetSize() const
Definition
buffer.h:1057
ns3::Buffer::AddAtStart
void AddAtStart(uint32_t start)
Definition
buffer.cc:303
ns3::Buffer::Begin
Buffer::Iterator Begin() const
Definition
buffer.h:1063
ns3::HeaderSerializationTestCase
Subclass of TestCase class adding the ability to test the serialization and deserialization of a Head...
Definition
header-serialization-test.h:24
ns3::HeaderSerializationTestCase::TestHeaderSerialization
void TestHeaderSerialization(const T &hdr, Args &&... args)
Serialize the given header in a buffer, then create a new header by deserializing from the buffer and...
Definition
header-serialization-test.h:62
ns3::HeaderSerializationTestCase::HeaderSerializationTestCase
HeaderSerializationTestCase(std::string name)
Constructor.
Definition
header-serialization-test.h:31
ns3::TestCase
encapsulates test code
Definition
test.h:1050
uint32_t
NS_TEST_ASSERT_MSG_EQ
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition
test.h:134
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
header-serialization-test.h
Generated on Fri Dec 13 2024 09:20:47 for ns-3 by
1.11.0