A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
sequence-number-test-suite.cc
Go to the documentation of this file.
1//
2// Copyright (c) 2008-2010 INESC Porto
3//
4// SPDX-License-Identifier: GPL-2.0-only
5//
6// Author: Gustavo J. A. M. Carneiro <gjc@inescporto.pt> <gjcarneiro@gmail.com>
7//
8
9#include "ns3/object.h"
10#include "ns3/sequence-number.h"
11#include "ns3/test.h"
12#include "ns3/trace-source-accessor.h"
13#include "ns3/traced-value.h"
14
15using namespace ns3;
16
17namespace
18{
19
20/**
21 * \ingroup network-test
22 * \ingroup tests
23 *
24 * \brief Test object using sequence numbers
25 *
26 * \note Class internal to sequence-number-test-suite.cc
27 */
29{
30 /// Test traced sequence number.
32
33 public:
35 {
36 m_testTracedSequenceNumber = SequenceNumber32(0);
37 }
38
39 /**
40 * \brief Get the type ID.
41 * \return The object TypeId.
42 */
44 {
45 static TypeId tid =
46 TypeId("ns3::SequenceNumberTestObj")
48 .AddTraceSource(
49 "TestTracedSequenceNumber",
50 "A traceable sequence number",
52 "ns3::SequenceNumber32TracedValueCallback")
53 .AddConstructor<SequenceNumberTestObj>();
54 return tid;
55 }
56
57 TypeId GetInstanceTypeId() const override
58 {
59 return GetTypeId();
60 }
61
62 /// Increment the sequence number.
64 {
65 m_testTracedSequenceNumber += 1;
66 }
67};
68
69} // namespace
70
71/**
72 * \ingroup network-test
73 * \ingroup tests
74 *
75 * \brief Sequence Number Unit Test
76 */
78{
79 SequenceNumber32 m_oldval; //!< Old value
80 SequenceNumber32 m_newval; //!< New value
81
82 /**
83 * Sequence number tracker
84 * \param oldval Old value
85 * \param newval New value
86 */
88
89 public:
91 ~SequenceNumberTestCase() override;
92 void DoRun() override;
93};
94
96 : TestCase("Sequence number test case")
97{
98 m_oldval = 0;
99 m_newval = 0;
100}
101
105
106void
112
113void
115{
116#define SEQ_TEST_ASSERT_EQUAL(a, b) NS_TEST_ASSERT_MSG_EQ(a, b, "foo")
117#define SEQ_TEST_ASSERT(a) NS_TEST_ASSERT_MSG_EQ(bool(a), true, "foo")
118
119 {
120 SequenceNumber32 num1(3);
121 SequenceNumber32 num2(5);
122 uint32_t value;
123
124 value = (num1 + num2).GetValue();
125 SEQ_TEST_ASSERT_EQUAL(value, 8);
126
127 num1 += num2.GetValue();
129
130 ++num1;
132
133 --num1;
135
136 num1++;
138
139 num1--;
141 }
142
143 {
144 SequenceNumber16 num1(60900);
145 SequenceNumber16 num2(5);
146 SequenceNumber16 num3(10000);
147
148 SEQ_TEST_ASSERT(num1 == num1);
149
150 SEQ_TEST_ASSERT(num2 != num1);
151
152 SEQ_TEST_ASSERT(num3 > num2);
153 SEQ_TEST_ASSERT(num3 >= num2);
154 SEQ_TEST_ASSERT(num1 < num3);
155 SEQ_TEST_ASSERT(num1 <= num3);
156
157 SEQ_TEST_ASSERT(num1 < num2);
158 SEQ_TEST_ASSERT(num1 <= num2);
159 SEQ_TEST_ASSERT(num2 > num1);
160 SEQ_TEST_ASSERT(num2 >= num1);
161
162 SEQ_TEST_ASSERT(num1 + num2 > num1);
163 SEQ_TEST_ASSERT(num1 + num2 >= num1);
164 SEQ_TEST_ASSERT(num1 < num1 + num2);
165 SEQ_TEST_ASSERT(num1 <= num1 + num2);
166
167 SEQ_TEST_ASSERT(num1 < num1 + num3);
168 SEQ_TEST_ASSERT(num1 <= num1 + num3);
169 SEQ_TEST_ASSERT(num1 + num3 > num1);
170 SEQ_TEST_ASSERT(num1 + num3 >= num1);
171 }
172
173 {
175 SequenceNumber16(1000),
176 6000);
178 SequenceNumber16(60000),
179 6000);
182 SequenceNumber16(65000),
183 -4000);
184 }
185
186 {
187 SequenceNumber32 num1(3);
188
190 num1 += -1;
192
193 SEQ_TEST_ASSERT_EQUAL(num1 - (num1 - 100), 100);
194 }
195
196 {
198 obj->TraceConnectWithoutContext(
199 "TestTracedSequenceNumber",
201 obj->IncSequenceNumber();
204 obj->Dispose();
205 }
206}
207
208/**
209 * \ingroup network-test
210 * \ingroup tests
211 *
212 * \brief Sequence Number TestSuite
213 */
215{
216 public:
218 : TestSuite("sequence-number", Type::UNIT)
219 {
220 AddTestCase(new SequenceNumberTestCase(), TestCase::Duration::QUICK);
221 }
222};
223
224static SequenceNumberTestSuite g_seqNumTests; //!< Static variable for test initialization
Sequence Number Unit Test.
void DoRun() override
Implementation to actually run this TestCase.
SequenceNumber32 m_oldval
Old value.
void SequenceNumberTracer(SequenceNumber32 oldval, SequenceNumber32 newval)
Sequence number tracker.
SequenceNumber32 m_newval
New value.
TracedValue< SequenceNumber32 > m_testTracedSequenceNumber
Test traced sequence number.
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
A base class which provides memory management and object aggregation.
Definition object.h:78
Smart pointer class similar to boost::intrusive_ptr.
NUMERIC_TYPE GetValue() const
Extracts the numeric value of the sequence number.
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
static constexpr auto UNIT
Definition test.h:1291
Trace classes with value semantics.
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
SequenceNumber< uint32_t, int32_t > SequenceNumber32
32 bit Sequence number.
SequenceNumber< uint16_t, int16_t > SequenceNumber16
16 bit Sequence number.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition callback.h:684
#define SEQ_TEST_ASSERT_EQUAL(a, b)
static SequenceNumberTestSuite g_seqNumTests
Static variable for test initialization.
#define SEQ_TEST_ASSERT(a)