A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  .AddConstructor<SequenceNumberTestObj> ()
49  ;
50  return tid;
51  }
52 
54  {
55  return GetTypeId ();
56  }
57 
59  {
60  m_testTracedSequenceNumber += 1;
61  }
62 
63 
64 };
65 
67 {
70 
71  void SequenceNumberTracer (SequenceNumber32 oldval, SequenceNumber32 newval);
72 
73 public:
74 
76  virtual ~SequenceNumberTestCase ();
77  virtual void DoRun (void);
78 };
79 
81  : TestCase ("Sequence number test case")
82 {
83  m_oldval = 0;
84  m_newval = 0;
85 }
86 
88 {
89 }
90 
91 void
93 {
94  m_oldval = oldval;
95  m_newval = newval;
96 }
97 
99 {
100 #define NS_TEST_ASSERT_EQUAL(a,b) NS_TEST_ASSERT_MSG_EQ (a,b, "foo")
101 #define NS_TEST_ASSERT(a) NS_TEST_ASSERT_MSG_EQ (bool(a), true, "foo")
102 
103  {
104  SequenceNumber32 num1 (3), num2 (5);
105  uint32_t value;
106 
107  value = (num1 + num2).GetValue ();
108  NS_TEST_ASSERT_EQUAL (value, 8);
109 
110  num1 += num2.GetValue ();
112 
113  ++num1;
115 
116  --num1;
118 
119  num1++;
121 
122  num1--;
124 
125  }
126 
127  {
128  SequenceNumber16 num1 (60900), num2 (5), num3 (10000);
129 
130  NS_TEST_ASSERT (num1 == num1);
131 
132  NS_TEST_ASSERT (num2 != num1);
133 
134  NS_TEST_ASSERT (num3 > num2);
135  NS_TEST_ASSERT (num3 >= num2);
136  NS_TEST_ASSERT (num1 < num3);
137  NS_TEST_ASSERT (num1 <= num3);
138 
139  NS_TEST_ASSERT (num1 < num2);
140  NS_TEST_ASSERT (num1 <= num2);
141  NS_TEST_ASSERT (num2 > num1);
142  NS_TEST_ASSERT (num2 >= num1);
143 
144  NS_TEST_ASSERT (num1+num2 > num1);
145  NS_TEST_ASSERT (num1+num2 >= num1);
146  NS_TEST_ASSERT (num1 < num1+num2);
147  NS_TEST_ASSERT (num1 <= num1+num2);
148 
149  NS_TEST_ASSERT (num1 < num1+num3);
150  NS_TEST_ASSERT (num1 <= num1+num3);
151  NS_TEST_ASSERT (num1+num3 > num1);
152  NS_TEST_ASSERT (num1+num3 >= num1);
153  }
154 
155  {
156  NS_TEST_ASSERT_EQUAL ((SequenceNumber16 (1000) + SequenceNumber16 (6000)) - SequenceNumber16 (1000), 6000);
157  NS_TEST_ASSERT_EQUAL ((SequenceNumber16 (60000) + SequenceNumber16 (6000)) - SequenceNumber16 (60000), 6000);
158  NS_TEST_ASSERT_EQUAL (SequenceNumber16 (1000) - SequenceNumber16 (6000), -5000);
159  NS_TEST_ASSERT_EQUAL ((SequenceNumber16 (60000) + SequenceNumber16 (1000)) - SequenceNumber16 (65000), -4000);
160  }
161 
162  {
163  SequenceNumber32 num1 (3);
164 
165  NS_TEST_ASSERT_EQUAL (num1 + 10, SequenceNumber32 (13));
166  num1 += -1;
168 
169  NS_TEST_ASSERT_EQUAL (num1 - (num1 - 100), 100);
170  }
171 
172  {
173  Ptr<SequenceNumberTestObj> obj = CreateObject<SequenceNumberTestObj> ();
175  obj->IncSequenceNumber ();
178  obj->Dispose ();
179  }
180 
181 }
182 
183 static class SequenceNumberTestSuite : public TestSuite
184 {
185 public:
187  : TestSuite ("sequence-number", UNIT)
188  {
189  AddTestCase (new SequenceNumberTestCase (), TestCase::QUICK);
190  }
191 } g_seqNumTests;
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
NUMERIC_TYPE GetValue() const
Extracts the numeric value of the sequence number.
A suite of tests to run.
Definition: test.h:1025
TypeId GetInstanceTypeId(void) const
encapsulates test code
Definition: test.h:849
SequenceNumber< uint32_t, int32_t > SequenceNumber32
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1238
virtual void DoRun(void)
Implementation to actually run this TestCase.
#define NS_TEST_ASSERT(a)
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Definition: object-base.cc:269
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
Definition: test.cc:173
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:63
#define NS_TEST_ASSERT_EQUAL(a, b)
This test suite implements a Unit Test.
Definition: test.h:1035
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611
void Dispose(void)
Run the DoDispose methods of this object and all the objects aggregated to it.
Definition: object.cc:205