A Discrete-Event Network Simulator
API
sequence-number-test-suite.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 2008-2010 INESC Porto
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 // Author: Gustavo J. A. M. Carneiro <gjc@inescporto.pt> <gjcarneiro@gmail.com>
18 //
19 
20 #include "ns3/object.h"
21 #include "ns3/sequence-number.h"
22 #include "ns3/test.h"
23 #include "ns3/trace-source-accessor.h"
24 #include "ns3/traced-value.h"
25 
26 using namespace ns3;
27 
28 namespace
29 {
30 
40 {
43 
44  public:
46  {
47  m_testTracedSequenceNumber = SequenceNumber32(0);
48  }
49 
54  static TypeId GetTypeId()
55  {
56  static TypeId tid =
57  TypeId("ns3::SequenceNumberTestObj")
58  .SetParent<Object>()
59  .AddTraceSource(
60  "TestTracedSequenceNumber",
61  "A traceable sequence number",
62  MakeTraceSourceAccessor(&SequenceNumberTestObj::m_testTracedSequenceNumber),
63  "ns3::SequenceNumber32TracedValueCallback")
64  .AddConstructor<SequenceNumberTestObj>();
65  return tid;
66  }
67 
68  TypeId GetInstanceTypeId() const override
69  {
70  return GetTypeId();
71  }
72 
75  {
76  m_testTracedSequenceNumber += 1;
77  }
78 };
79 
80 } // namespace
81 
89 {
92 
98  void SequenceNumberTracer(SequenceNumber32 oldval, SequenceNumber32 newval);
99 
100  public:
102  ~SequenceNumberTestCase() override;
103  void DoRun() override;
104 };
105 
107  : TestCase("Sequence number test case")
108 {
109  m_oldval = 0;
110  m_newval = 0;
111 }
112 
114 {
115 }
116 
117 void
119 {
120  m_oldval = oldval;
121  m_newval = newval;
122 }
123 
124 void
126 {
127 #define SEQ_TEST_ASSERT_EQUAL(a, b) NS_TEST_ASSERT_MSG_EQ(a, b, "foo")
128 #define SEQ_TEST_ASSERT(a) NS_TEST_ASSERT_MSG_EQ(bool(a), true, "foo")
129 
130  {
131  SequenceNumber32 num1(3);
132  SequenceNumber32 num2(5);
133  uint32_t value;
134 
135  value = (num1 + num2).GetValue();
137 
138  num1 += num2.GetValue();
140 
141  ++num1;
143 
144  --num1;
146 
147  num1++;
149 
150  num1--;
152  }
153 
154  {
155  SequenceNumber16 num1(60900);
156  SequenceNumber16 num2(5);
157  SequenceNumber16 num3(10000);
158 
159  SEQ_TEST_ASSERT(num1 == num1);
160 
161  SEQ_TEST_ASSERT(num2 != num1);
162 
163  SEQ_TEST_ASSERT(num3 > num2);
164  SEQ_TEST_ASSERT(num3 >= num2);
165  SEQ_TEST_ASSERT(num1 < num3);
166  SEQ_TEST_ASSERT(num1 <= num3);
167 
168  SEQ_TEST_ASSERT(num1 < num2);
169  SEQ_TEST_ASSERT(num1 <= num2);
170  SEQ_TEST_ASSERT(num2 > num1);
171  SEQ_TEST_ASSERT(num2 >= num1);
172 
173  SEQ_TEST_ASSERT(num1 + num2 > num1);
174  SEQ_TEST_ASSERT(num1 + num2 >= num1);
175  SEQ_TEST_ASSERT(num1 < num1 + num2);
176  SEQ_TEST_ASSERT(num1 <= num1 + num2);
177 
178  SEQ_TEST_ASSERT(num1 < num1 + num3);
179  SEQ_TEST_ASSERT(num1 <= num1 + num3);
180  SEQ_TEST_ASSERT(num1 + num3 > num1);
181  SEQ_TEST_ASSERT(num1 + num3 >= num1);
182  }
183 
184  {
186  SequenceNumber16(1000),
187  6000);
189  SequenceNumber16(60000),
190  6000);
193  SequenceNumber16(65000),
194  -4000);
195  }
196 
197  {
198  SequenceNumber32 num1(3);
199 
200  SEQ_TEST_ASSERT_EQUAL(num1 + 10, SequenceNumber32(13));
201  num1 += -1;
203 
204  SEQ_TEST_ASSERT_EQUAL(num1 - (num1 - 100), 100);
205  }
206 
207  {
208  Ptr<SequenceNumberTestObj> obj = CreateObject<SequenceNumberTestObj>();
209  obj->TraceConnectWithoutContext(
210  "TestTracedSequenceNumber",
212  obj->IncSequenceNumber();
215  obj->Dispose();
216  }
217 }
218 
226 {
227  public:
229  : TestSuite("sequence-number", UNIT)
230  {
231  AddTestCase(new SequenceNumberTestCase(), TestCase::QUICK);
232  }
233 };
234 
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.
Sequence Number TestSuite.
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:89
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
NUMERIC_TYPE GetValue() const
Extracts the numeric value of the sequence number.
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1265
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
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:704
value
Definition: second.py:48
#define SEQ_TEST_ASSERT_EQUAL(a, b)
static SequenceNumberTestSuite g_seqNumTests
Static variable for test initialization.
#define SEQ_TEST_ASSERT(a)