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 * 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
16namespace ns3
17{
18
19/**
20 * Subclass of TestCase class adding the ability to test the serialization and
21 * deserialization of a Header object.
22 */
24{
25 protected:
26 /**
27 * @brief Constructor.
28 *
29 * @param [in] name The name of the new TestCase created
30 */
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
57namespace ns3
58{
59
60template <typename T, typename... Args>
61void
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 */
iterator in a Buffer instance
Definition buffer.h:89
automatically resized byte buffer
Definition buffer.h:83
uint32_t GetSize() const
Definition buffer.h:1057
void AddAtStart(uint32_t start)
Definition buffer.cc:303
Buffer::Iterator Begin() const
Definition buffer.h:1063
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:1050
#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
#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.