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
15#include <limits>
16
17using namespace ns3;
18
19namespace
20{
21
22/**
23 * @ingroup network-test
24 * @ingroup tests
25 *
26 * @brief Test object using sequence numbers
27 *
28 * @note Class internal to sequence-number-test-suite.cc
29 */
31{
32 /// Test traced sequence number.
34
35 public:
40
41 /**
42 * @brief Get the type ID.
43 * @return The object TypeId.
44 */
46 {
47 static TypeId tid =
48 TypeId("ns3::SequenceNumberTestObj")
50 .AddTraceSource(
51 "TestTracedSequenceNumber",
52 "A traceable sequence number",
54 "ns3::SequenceNumber32TracedValueCallback")
55 .AddConstructor<SequenceNumberTestObj>();
56 return tid;
57 }
58
59 /// Increment the sequence number.
64};
65
66} // namespace
67
68/**
69 * @ingroup network-test
70 * @ingroup tests
71 *
72 * @brief Sequence Number Unit Test
73 */
75{
76 SequenceNumber32 m_oldval; //!< Old value
77 SequenceNumber32 m_newval; //!< New value
78
79 /**
80 * Sequence number tracker
81 * @param oldval Old value
82 * @param newval New value
83 */
85
86 public:
88 ~SequenceNumberTestCase() override;
89 void DoRun() override;
90};
91
93 : TestCase("Sequence number test case")
94{
95 m_oldval = 0;
96 m_newval = 0;
97}
98
102
103void
109
110void
112{
113#define SEQ_TEST_ASSERT_EQUAL(a, b) NS_TEST_ASSERT_MSG_EQ(a, b, "foo")
114#define SEQ_TEST_ASSERT(a) NS_TEST_ASSERT_MSG_EQ(bool(a), true, "foo")
115
116 {
117 SequenceNumber32 num1(3);
118 SequenceNumber32 num2(5);
119 uint32_t value;
120
121 value = (num1 + num2).GetValue();
122 SEQ_TEST_ASSERT_EQUAL(value, 8);
123
124 num1 += num2.GetValue();
126
127 ++num1;
129
130 --num1;
132
133 num1++;
135
136 num1--;
138 }
139
140 {
141 SequenceNumber16 num1(60900);
142 SequenceNumber16 num2(5);
143 SequenceNumber16 num3(10000);
144
145 SEQ_TEST_ASSERT(num1 == num1);
146
147 SEQ_TEST_ASSERT(num2 != num1);
148
149 SEQ_TEST_ASSERT(num3 > num2);
150 SEQ_TEST_ASSERT(num3 >= num2);
151 SEQ_TEST_ASSERT(num1 < num3);
152 SEQ_TEST_ASSERT(num1 <= num3);
153
154 SEQ_TEST_ASSERT(num1 < num2);
155 SEQ_TEST_ASSERT(num1 <= num2);
156 SEQ_TEST_ASSERT(num2 > num1);
157 SEQ_TEST_ASSERT(num2 >= num1);
158
159 SEQ_TEST_ASSERT(num1 + num2 > num1);
160 SEQ_TEST_ASSERT(num1 + num2 >= num1);
161 SEQ_TEST_ASSERT(num1 < num1 + num2);
162 SEQ_TEST_ASSERT(num1 <= num1 + num2);
163
164 SEQ_TEST_ASSERT(num1 < num1 + num3);
165 SEQ_TEST_ASSERT(num1 <= num1 + num3);
166 SEQ_TEST_ASSERT(num1 + num3 > num1);
167 SEQ_TEST_ASSERT(num1 + num3 >= num1);
168 }
169
170 {
172 SequenceNumber16(1000),
173 6000);
175 SequenceNumber16(60000),
176 6000);
179 SequenceNumber16(65000),
180 -4000);
181 }
182
183 {
184 SequenceNumber32 num1(3);
185
187 num1 += -1;
189
190 SEQ_TEST_ASSERT_EQUAL(num1 - (num1 - 100), 100);
191 }
192
193 {
195 obj->TraceConnectWithoutContext(
196 "TestTracedSequenceNumber",
198 obj->IncSequenceNumber();
201 obj->Dispose();
202 }
203
204 {
205 SequenceNumber16 num1(0);
206 SequenceNumber16 num2(0);
207
208 for (uint32_t index = 0; index <= std::numeric_limits<uint16_t>::max(); index++)
209 {
210 NS_TEST_ASSERT_MSG_EQ(((num1 - num2) < 0 && (num1 > num2)) ||
211 ((num1 - num2) > 0 && (num2 > num1)),
212 false,
213 "Sequence number: difference and <=> are not agreeing for "
214 << num1.GetValue() << " and " << num2.GetValue());
215 num2++;
216 }
217
218 num2 = 0;
219 for (uint32_t index = 0; index <= std::numeric_limits<uint16_t>::max(); index++)
220 {
221 NS_TEST_ASSERT_MSG_EQ(((num1 - num2) < 0 && (num1 > num2)) ||
222 ((num1 - num2) > 0 && (num2 > num1)),
223 false,
224 "Sequence number: difference and <=> are not agreeing for "
225 << num1.GetValue() << " and " << num2.GetValue());
226 num1++;
227 }
228 }
229
230 {
232 SequenceNumber<uint16_t, 5> num2 = num1;
233
234 for (uint32_t index = 0; index < (1 << 5) - 1; index++)
235 {
236 {
237 NS_TEST_ASSERT_MSG_EQ(((num1 - num2) < 0 && (num1 > num2)) ||
238 ((num1 - num2) > 0 && (num2 > num1)),
239 false,
240 "Sequence number: difference and <=> are not agreeing for "
241 << num1.GetValue() << " and " << num2.GetValue());
242 }
243 num2++;
244 }
245
246 num2 = 0;
247 for (uint32_t index = 0; index < (1 << 5) - 1; index++)
248 {
249 NS_TEST_ASSERT_MSG_EQ(((num1 - num2) < 0 && (num1 > num2)) ||
250 ((num1 - num2) > 0 && (num2 > num1)),
251 false,
252 "Sequence number: difference and <=> are not agreeing for "
253 << num1.GetValue() << " and " << num2.GetValue());
254 num1++;
255 }
256 }
257}
258
259/**
260 * @ingroup network-test
261 * @ingroup tests
262 *
263 * @brief Sequence Number TestSuite
264 */
266{
267 public:
273};
274
275static 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.
Object()
Caller graph was not generated because of its size.
Definition object.cc:93
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
Generic "sequence number" class.
constexpr NUMERIC_TYPE GetValue() const
Extracts the numeric value of the sequence number.
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:296
@ QUICK
Fast test.
Definition test.h:1057
TestCase(const TestCase &)=delete
Caller graph was not generated because of its size.
Type
Type of test.
Definition test.h:1271
@ UNIT
This test suite implements a Unit Test.
Definition test.h:1273
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition test.cc:494
Trace classes with value semantics.
a unique identifier for an interface.
Definition type-id.h:50
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:999
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:690
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:627
SequenceNumber< uint16_t > SequenceNumber16
16 bit Sequence number.
SequenceNumber< uint32_t > SequenceNumber32
32 bit Sequence number.
#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:133
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.
#define SEQ_TEST_ASSERT_EQUAL(a, b)
static SequenceNumberTestSuite g_seqNumTests
Static variable for test initialization.
#define SEQ_TEST_ASSERT(a)