A Discrete-Event Network Simulator
API
sequence-number-test-suite.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 //
3 // Copyright (c) 2008-2010 INESC Porto
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License version 2 as
7 // published by the Free Software Foundation;
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 //
18 // Author: Gustavo J. A. M. Carneiro <gjc@inescporto.pt> <gjcarneiro@gmail.com>
19 //
20 
21 #include "ns3/sequence-number.h"
22 #include "ns3/test.h"
23 #include "ns3/object.h"
24 #include "ns3/traced-value.h"
25 #include "ns3/trace-source-accessor.h"
26 
27 using namespace ns3;
28 
29 namespace {
30 
31 
41 {
44 
45 public:
46 
48  {
49  m_testTracedSequenceNumber = SequenceNumber32 (0);
50  }
51 
56  static TypeId GetTypeId (void)
57  {
58  static TypeId tid = TypeId ("ns3::SequenceNumberTestObj")
59  .SetParent<Object> ()
60  .AddTraceSource ("TestTracedSequenceNumber",
61  "A traceable sequence number",
62  MakeTraceSourceAccessor (&SequenceNumberTestObj::m_testTracedSequenceNumber),
63  "ns3::SequenceNumber32TracedValueCallback")
64  .AddConstructor<SequenceNumberTestObj> ()
65  ;
66  return tid;
67  }
68 
70  {
71  return GetTypeId ();
72  }
73 
76  {
77  m_testTracedSequenceNumber += 1;
78  }
79 };
80 
81 }
82 
90 {
93 
99  void SequenceNumberTracer (SequenceNumber32 oldval, SequenceNumber32 newval);
100 
101 public:
102 
104  virtual ~SequenceNumberTestCase ();
105  virtual void DoRun (void);
106 };
107 
109  : TestCase ("Sequence number test case")
110 {
111  m_oldval = 0;
112  m_newval = 0;
113 }
114 
116 {
117 }
118 
119 void
121 {
122  m_oldval = oldval;
123  m_newval = newval;
124 }
125 
127 {
128 #define SEQ_TEST_ASSERT_EQUAL(a,b) NS_TEST_ASSERT_MSG_EQ (a,b, "foo")
129 #define SEQ_TEST_ASSERT(a) NS_TEST_ASSERT_MSG_EQ (bool(a), true, "foo")
130 
131  {
132  SequenceNumber32 num1 (3), num2 (5);
133  uint32_t value;
134 
135  value = (num1 + num2).GetValue ();
136  SEQ_TEST_ASSERT_EQUAL (value, 8);
137 
138  num1 += num2.GetValue ();
140 
141  ++num1;
143 
144  --num1;
146 
147  num1++;
149 
150  num1--;
152 
153  }
154 
155  {
156  SequenceNumber16 num1 (60900), num2 (5), num3 (10000);
157 
158  SEQ_TEST_ASSERT (num1 == num1);
159 
160  SEQ_TEST_ASSERT (num2 != num1);
161 
162  SEQ_TEST_ASSERT (num3 > num2);
163  SEQ_TEST_ASSERT (num3 >= num2);
164  SEQ_TEST_ASSERT (num1 < num3);
165  SEQ_TEST_ASSERT (num1 <= num3);
166 
167  SEQ_TEST_ASSERT (num1 < num2);
168  SEQ_TEST_ASSERT (num1 <= num2);
169  SEQ_TEST_ASSERT (num2 > num1);
170  SEQ_TEST_ASSERT (num2 >= num1);
171 
172  SEQ_TEST_ASSERT (num1+num2 > num1);
173  SEQ_TEST_ASSERT (num1+num2 >= num1);
174  SEQ_TEST_ASSERT (num1 < num1+num2);
175  SEQ_TEST_ASSERT (num1 <= num1+num2);
176 
177  SEQ_TEST_ASSERT (num1 < num1+num3);
178  SEQ_TEST_ASSERT (num1 <= num1+num3);
179  SEQ_TEST_ASSERT (num1+num3 > num1);
180  SEQ_TEST_ASSERT (num1+num3 >= num1);
181  }
182 
183  {
184  SEQ_TEST_ASSERT_EQUAL ((SequenceNumber16 (1000) + SequenceNumber16 (6000)) - SequenceNumber16 (1000), 6000);
185  SEQ_TEST_ASSERT_EQUAL ((SequenceNumber16 (60000) + SequenceNumber16 (6000)) - SequenceNumber16 (60000), 6000);
187  SEQ_TEST_ASSERT_EQUAL ((SequenceNumber16 (60000) + SequenceNumber16 (1000)) - SequenceNumber16 (65000), -4000);
188  }
189 
190  {
191  SequenceNumber32 num1 (3);
192 
193  SEQ_TEST_ASSERT_EQUAL (num1 + 10, SequenceNumber32 (13));
194  num1 += -1;
196 
197  SEQ_TEST_ASSERT_EQUAL (num1 - (num1 - 100), 100);
198  }
199 
200  {
201  Ptr<SequenceNumberTestObj> obj = CreateObject<SequenceNumberTestObj> ();
202  obj->TraceConnectWithoutContext ("TestTracedSequenceNumber", MakeCallback (&SequenceNumberTestCase::SequenceNumberTracer, this));
203  obj->IncSequenceNumber ();
206  obj->Dispose ();
207  }
208 
209 }
210 
218 {
219 public:
221  : TestSuite ("sequence-number", UNIT)
222  {
223  AddTestCase (new SequenceNumberTestCase (), TestCase::QUICK);
224  }
225 };
226 
Sequence Number TestSuite.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define SEQ_TEST_ASSERT(a)
#define SEQ_TEST_ASSERT_EQUAL(a, b)
A suite of tests to run.
Definition: test.h:1342
TracedValue< SequenceNumber32 > m_testTracedSequenceNumber
Test traced sequence number.
SequenceNumber32 m_oldval
Old value.
encapsulates test code
Definition: test.h:1155
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
virtual void DoRun(void)
Implementation to actually run this TestCase.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void SequenceNumberTracer(SequenceNumber32 oldval, SequenceNumber32 newval)
Sequence number tracker.
static SequenceNumberTestSuite g_seqNumTests
Static variable for test initialization.
A base class which provides memory management and object aggregation.
Definition: object.h:87
This test suite implements a Unit Test.
Definition: test.h:1351
NUMERIC_TYPE GetValue() const
Extracts the numeric value of the sequence number.
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
SequenceNumber< uint32_t, int32_t > SequenceNumber32
32 bit Sequence number.
SequenceNumber32 m_newval
New value.
Sequence Number Unit Test.