A Discrete-Event Network Simulator
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 * 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 * Authors: Davide Magrin <magrin.davide@gmail.com>
18 * Stefano Avallone <stavallo@unina.it>
19 */
20
21#ifndef NS3_TEST_HDR_SERIALIZE_H
22#define NS3_TEST_HDR_SERIALIZE_H
23
24#include "ns3/buffer.h"
25#include "ns3/test.h"
26
27namespace ns3
28{
29
30/**
31 * Subclass of TestCase class adding the ability to test the serialization and
32 * deserialization of a Header object.
33 */
35{
36 protected:
37 /**
38 * \brief Constructor.
39 *
40 * \param [in] name The name of the new TestCase created
41 */
43 : TestCase(name)
44 {
45 }
46
47 public:
48 /**
49 * Serialize the given header in a buffer, then create a new header by
50 * deserializing from the buffer and serialize the new header into a new buffer.
51 * Verify that the two buffers have the same size and the same content.
52 *
53 * \tparam T \deduced Type of the given header
54 * \tparam Args \deduced Type of arguments to pass to the constructor of the header
55 * \param [in] hdr the header to test
56 * \param [in] args the arguments to construct the new header
57 */
58 template <typename T, typename... Args>
59 void TestHeaderSerialization(const T& hdr, Args&&... args);
60};
61
62} // namespace ns3
63
64/***************************************************************
65 * Implementation of the templates declared above.
66 ***************************************************************/
67
68namespace ns3
69{
70
71template <typename T, typename... Args>
72void
74{
75 Buffer buffer;
76 buffer.AddAtStart(hdr.GetSerializedSize());
77 hdr.Serialize(buffer.Begin());
78
79 T otherHdr(std::forward<Args>(args)...);
80 otherHdr.Deserialize(buffer.Begin());
81 Buffer otherBuffer;
82 otherBuffer.AddAtStart(otherHdr.GetSerializedSize());
83 otherHdr.Serialize(otherBuffer.Begin());
84
85 NS_TEST_ASSERT_MSG_EQ(buffer.GetSize(), otherBuffer.GetSize(), "Size of buffers differs");
86
87 Buffer::Iterator bufferIterator = buffer.Begin();
88 Buffer::Iterator otherBufferIterator = otherBuffer.Begin();
89 for (uint32_t j = 0; j < buffer.GetSize(); j++)
90 {
91 uint8_t bufferVal = bufferIterator.ReadU8();
92 uint8_t otherBufferVal = otherBufferIterator.ReadU8();
93 NS_TEST_EXPECT_MSG_EQ(+bufferVal,
94 +otherBufferVal,
95 "Serialization -> Deserialization -> Serialization "
96 << "is different than Serialization at byte " << j);
97 }
98}
99
100} // namespace ns3
101
102#endif /* NS3_TEST_HDR_SERIALIZE_H */
iterator in a Buffer instance
Definition: buffer.h:100
uint8_t ReadU8()
Definition: buffer.h:1027
automatically resized byte buffer
Definition: buffer.h:94
uint32_t GetSize() const
Definition: buffer.h:1068
void AddAtStart(uint32_t start)
Definition: buffer.cc:314
Buffer::Iterator Begin() const
Definition: buffer.h:1074
Subclass of TestCase class adding the ability to test the serialization and deserialization of a Head...
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...
HeaderSerializationTestCase(std::string name)
Constructor.
encapsulates test code
Definition: test.h:1061
#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:145
#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:252
Every class exported by the ns3 library is enclosed in the ns3 namespace.