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 
30 {
32 
33 
34 public:
35 
37  {
38  m_testTracedSequenceNumber = SequenceNumber32 (0);
39  }
40 
41  static TypeId GetTypeId (void)
42  {
43  static TypeId tid = TypeId ("ns3::SequenceNumberTestObj")
44  .SetParent<Object> ()
45  .AddTraceSource ("TestTracedSequenceNumber",
46  "A traceable sequence number",
48  "ns3::SequenceNumber32TracedValueCallback")
49  .AddConstructor<SequenceNumberTestObj> ()
50  ;
51  return tid;
52  }
53 
55  {
56  return GetTypeId ();
57  }
58 
60  {
61  m_testTracedSequenceNumber += 1;
62  }
63 
64 
65 };
66 
68 {
71 
72  void SequenceNumberTracer (SequenceNumber32 oldval, SequenceNumber32 newval);
73 
74 public:
75 
77  virtual ~SequenceNumberTestCase ();
78  virtual void DoRun (void);
79 };
80 
82  : TestCase ("Sequence number test case")
83 {
84  m_oldval = 0;
85  m_newval = 0;
86 }
87 
89 {
90 }
91 
92 void
94 {
95  m_oldval = oldval;
96  m_newval = newval;
97 }
98 
100 {
101 #define SEQ_TEST_ASSERT_EQUAL(a,b) NS_TEST_ASSERT_MSG_EQ (a,b, "foo")
102 #define SEQ_TEST_ASSERT(a) NS_TEST_ASSERT_MSG_EQ (bool(a), true, "foo")
103 
104  {
105  SequenceNumber32 num1 (3), num2 (5);
106  uint32_t value;
107 
108  value = (num1 + num2).GetValue ();
109  SEQ_TEST_ASSERT_EQUAL (value, 8);
110 
111  num1 += num2.GetValue ();
113 
114  ++num1;
116 
117  --num1;
119 
120  num1++;
122 
123  num1--;
125 
126  }
127 
128  {
129  SequenceNumber16 num1 (60900), num2 (5), num3 (10000);
130 
131  SEQ_TEST_ASSERT (num1 == num1);
132 
133  SEQ_TEST_ASSERT (num2 != num1);
134 
135  SEQ_TEST_ASSERT (num3 > num2);
136  SEQ_TEST_ASSERT (num3 >= num2);
137  SEQ_TEST_ASSERT (num1 < num3);
138  SEQ_TEST_ASSERT (num1 <= num3);
139 
140  SEQ_TEST_ASSERT (num1 < num2);
141  SEQ_TEST_ASSERT (num1 <= num2);
142  SEQ_TEST_ASSERT (num2 > num1);
143  SEQ_TEST_ASSERT (num2 >= num1);
144 
145  SEQ_TEST_ASSERT (num1+num2 > num1);
146  SEQ_TEST_ASSERT (num1+num2 >= num1);
147  SEQ_TEST_ASSERT (num1 < num1+num2);
148  SEQ_TEST_ASSERT (num1 <= num1+num2);
149 
150  SEQ_TEST_ASSERT (num1 < num1+num3);
151  SEQ_TEST_ASSERT (num1 <= num1+num3);
152  SEQ_TEST_ASSERT (num1+num3 > num1);
153  SEQ_TEST_ASSERT (num1+num3 >= num1);
154  }
155 
156  {
157  SEQ_TEST_ASSERT_EQUAL ((SequenceNumber16 (1000) + SequenceNumber16 (6000)) - SequenceNumber16 (1000), 6000);
158  SEQ_TEST_ASSERT_EQUAL ((SequenceNumber16 (60000) + SequenceNumber16 (6000)) - SequenceNumber16 (60000), 6000);
160  SEQ_TEST_ASSERT_EQUAL ((SequenceNumber16 (60000) + SequenceNumber16 (1000)) - SequenceNumber16 (65000), -4000);
161  }
162 
163  {
164  SequenceNumber32 num1 (3);
165 
166  SEQ_TEST_ASSERT_EQUAL (num1 + 10, SequenceNumber32 (13));
167  num1 += -1;
169 
170  SEQ_TEST_ASSERT_EQUAL (num1 - (num1 - 100), 100);
171  }
172 
173  {
174  Ptr<SequenceNumberTestObj> obj = CreateObject<SequenceNumberTestObj> ();
176  obj->IncSequenceNumber ();
179  obj->Dispose ();
180  }
181 
182 }
183 
184 static class SequenceNumberTestSuite : public TestSuite
185 {
186 public:
188  : TestSuite ("sequence-number", UNIT)
189  {
190  AddTestCase (new SequenceNumberTestCase (), TestCase::QUICK);
191  }
192 } g_seqNumTests;
void Dispose(void)
Dispose of this Object.
Definition: object.cc:214
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define SEQ_TEST_ASSERT(a)
NUMERIC_TYPE GetValue() const
Extracts the numeric value of the sequence number.
#define SEQ_TEST_ASSERT_EQUAL(a, b)
A suite of tests to run.
Definition: test.h:1333
TypeId GetInstanceTypeId(void) const
Implement the GetInstanceTypeId method defined in ObjectBase.
encapsulates test code
Definition: test.h:1147
This test suite implements a Unit Test.
Definition: test.h:1343
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
void AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
Definition: test.cc:298
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
virtual void DoRun(void)
Implementation to actually run this TestCase.
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Connect a TraceSource to a Callback without a context.
Definition: object-base.cc:299
Every class exported by the ns3 library is enclosed in the ns3 namespace.
TracedValue< SequenceNumber32 > m_testTracedSequenceNumber
void SequenceNumberTracer(SequenceNumber32 oldval, SequenceNumber32 newval)
SequenceNumberTestSuite g_seqNumTests
A base class which provides memory management and object aggregation.
Definition: object.h:87
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:904
SequenceNumber< uint32_t, int32_t > SequenceNumber32
32 bit Sequence number.