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 /// Increment the sequence number.
59 {
60 m_testTracedSequenceNumber += 1;
61 }
62};
63
64} // namespace
65
66/**
67 * @ingroup network-test
68 * @ingroup tests
69 *
70 * @brief Sequence Number Unit Test
71 */
73{
74 SequenceNumber32 m_oldval; //!< Old value
75 SequenceNumber32 m_newval; //!< New value
76
77 /**
78 * Sequence number tracker
79 * @param oldval Old value
80 * @param newval New value
81 */
83
84 public:
86 ~SequenceNumberTestCase() override;
87 void DoRun() override;
88};
89
91 : TestCase("Sequence number test case")
92{
93 m_oldval = 0;
94 m_newval = 0;
95}
96
100
101void
107
108void
110{
111#define SEQ_TEST_ASSERT_EQUAL(a, b) NS_TEST_ASSERT_MSG_EQ(a, b, "foo")
112#define SEQ_TEST_ASSERT(a) NS_TEST_ASSERT_MSG_EQ(bool(a), true, "foo")
113
114 {
115 SequenceNumber32 num1(3);
116 SequenceNumber32 num2(5);
117 uint32_t value;
118
119 value = (num1 + num2).GetValue();
120 SEQ_TEST_ASSERT_EQUAL(value, 8);
121
122 num1 += num2.GetValue();
124
125 ++num1;
127
128 --num1;
130
131 num1++;
133
134 num1--;
136 }
137
138 {
139 SequenceNumber16 num1(60900);
140 SequenceNumber16 num2(5);
141 SequenceNumber16 num3(10000);
142
143 SEQ_TEST_ASSERT(num1 == num1);
144
145 SEQ_TEST_ASSERT(num2 != num1);
146
147 SEQ_TEST_ASSERT(num3 > num2);
148 SEQ_TEST_ASSERT(num3 >= num2);
149 SEQ_TEST_ASSERT(num1 < num3);
150 SEQ_TEST_ASSERT(num1 <= num3);
151
152 SEQ_TEST_ASSERT(num1 < num2);
153 SEQ_TEST_ASSERT(num1 <= num2);
154 SEQ_TEST_ASSERT(num2 > num1);
155 SEQ_TEST_ASSERT(num2 >= num1);
156
157 SEQ_TEST_ASSERT(num1 + num2 > num1);
158 SEQ_TEST_ASSERT(num1 + num2 >= num1);
159 SEQ_TEST_ASSERT(num1 < num1 + num2);
160 SEQ_TEST_ASSERT(num1 <= num1 + num2);
161
162 SEQ_TEST_ASSERT(num1 < num1 + num3);
163 SEQ_TEST_ASSERT(num1 <= num1 + num3);
164 SEQ_TEST_ASSERT(num1 + num3 > num1);
165 SEQ_TEST_ASSERT(num1 + num3 >= num1);
166 }
167
168 {
170 SequenceNumber16(1000),
171 6000);
173 SequenceNumber16(60000),
174 6000);
177 SequenceNumber16(65000),
178 -4000);
179 }
180
181 {
182 SequenceNumber32 num1(3);
183
185 num1 += -1;
187
188 SEQ_TEST_ASSERT_EQUAL(num1 - (num1 - 100), 100);
189 }
190
191 {
193 obj->TraceConnectWithoutContext(
194 "TestTracedSequenceNumber",
196 obj->IncSequenceNumber();
199 obj->Dispose();
200 }
201}
202
203/**
204 * @ingroup network-test
205 * @ingroup tests
206 *
207 * @brief Sequence Number TestSuite
208 */
210{
211 public:
213 : TestSuite("sequence-number", Type::UNIT)
214 {
215 AddTestCase(new SequenceNumberTestCase(), TestCase::Duration::QUICK);
216 }
217};
218
219static 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.
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:49
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)