|
1 |
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
2 |
/* |
3 |
* This program is free software; you can redistribute it and/or modify |
4 |
* it under the terms of the GNU General Public License version 2 as |
5 |
* published by the Free Software Foundation; |
6 |
* |
7 |
* This program is distributed in the hope that it will be useful, |
8 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
9 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
10 |
* GNU General Public License for more details. |
11 |
* |
12 |
* You should have received a copy of the GNU General Public License |
13 |
* along with this program; if not, write to the Free Software |
14 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
15 |
* |
16 |
*/ |
17 |
|
18 |
#include <stdint.h> |
19 |
#include <limits> |
20 |
|
21 |
#include "ns3/assert.h" |
22 |
#include "ns3/log.h" |
23 |
#include "ns3/fatal-error.h" |
24 |
#include "ns3/test.h" |
25 |
#include "sequence.h" |
26 |
|
27 |
namespace ns3 { |
28 |
|
29 |
template <class T> |
30 |
void |
31 |
Sequence::Set (T * sequence, T value) |
32 |
{ |
33 |
*sequence = value; |
34 |
} |
35 |
|
36 |
template <class T> |
37 |
void |
38 |
Sequence::Inc (T * sequence) |
39 |
{ |
40 |
*sequence = *sequence + 1; |
41 |
} |
42 |
|
43 |
template <class T> |
44 |
void |
45 |
Sequence::Inc (T * sequence, T count) |
46 |
{ |
47 |
*sequence = *sequence + count; |
48 |
} |
49 |
|
50 |
template <class T> |
51 |
void |
52 |
Sequence::Dec (T * sequence) |
53 |
{ |
54 |
*sequence = *sequence - 1; |
55 |
} |
56 |
|
57 |
template <class T> |
58 |
void |
59 |
Sequence::Dec (T * sequence, T count) |
60 |
{ |
61 |
*sequence = *sequence - count; |
62 |
} |
63 |
|
64 |
template <class T> |
65 |
bool |
66 |
Sequence::IsEqual (T sequence, T other) |
67 |
{ |
68 |
return sequence == other; |
69 |
} |
70 |
|
71 |
// From RFC 3626: |
72 |
// |
73 |
// The sequence number S1 is said to be "greater than" the sequence |
74 |
// number S2 if: |
75 |
// |
76 |
// S1 > S2 AND S1 - S2 <= MAXVALUE/2 OR |
77 |
// |
78 |
// S2 > S1 AND S2 - S1 > MAXVALUE/2 |
79 |
template <class T> |
80 |
bool |
81 |
Sequence::IsGreater (T sequence, T other) |
82 |
{ |
83 |
static const T halfMaxValue = std::numeric_limits<T>::max () / 2; |
84 |
|
85 |
return (((sequence > other) && (sequence - other) <= halfMaxValue) |
86 |
|| ((other > sequence) && (other - sequence) > halfMaxValue)); |
87 |
} |
88 |
|
89 |
template <class T> |
90 |
bool |
91 |
Sequence::IsGreaterOrEqual (T sequence, T other) |
92 |
{ |
93 |
return (IsGreater (sequence, other) || IsEqual (sequence, other)); |
94 |
} |
95 |
|
96 |
template <class T> |
97 |
bool |
98 |
Sequence::IsLess (T sequence, T other) |
99 |
{ |
100 |
return !(IsGreaterOrEqual (sequence, other)); |
101 |
} |
102 |
|
103 |
template <class T> |
104 |
bool |
105 |
Sequence::IsLessOrEqual (T sequence, T other) |
106 |
{ |
107 |
return !(IsGreater (sequence, other)); |
108 |
} |
109 |
|
110 |
//----------------------------------------------------------------------------- |
111 |
// Unit tests |
112 |
//----------------------------------------------------------------------------- |
113 |
class SequenceTest: public TestCase { |
114 |
public: |
115 |
virtual bool DoRun (void); |
116 |
SequenceTest (); |
117 |
}; |
118 |
|
119 |
|
120 |
SequenceTest::SequenceTest () |
121 |
: TestCase ("Sequence Number") {} |
122 |
|
123 |
bool |
124 |
SequenceTest::DoRun (void) |
125 |
{ |
126 |
/* Will actually write some tests once it is clear this is |
127 |
headed in the right direction |
128 |
*/ |
129 |
uint16_t num1 (60900); |
130 |
NS_TEST_EXPECT_MSG_EQ (Sequence::IsEqual(num1, num1), true, "IsEqual"); |
131 |
|
132 |
return GetErrorStatus (); |
133 |
} |
134 |
//----------------------------------------------------------------------------- |
135 |
class SequenceTestSuite : public TestSuite |
136 |
{ |
137 |
public: |
138 |
SequenceTestSuite (); |
139 |
}; |
140 |
|
141 |
SequenceTestSuite::SequenceTestSuite () |
142 |
: TestSuite ("sequence-number", UNIT) |
143 |
{ |
144 |
AddTestCase (new SequenceTest); |
145 |
} |
146 |
|
147 |
SequenceTestSuite g_sequenceTestSuite; |
148 |
|
149 |
} // namespace ns3 |