View | Details | Raw Unified | Return to bug 385
Collapse All | Expand All

(-)1b8a736858cc (+149 lines)
Added Link Here 
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
(-)1b8a736858cc (+114 lines)
Added Link Here 
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
#ifndef SEQUENCE_H
19
#define SEQUENCE_H
20
21
namespace ns3 {
22
23
/**
24
 * \brief Generic sequence number class
25
 *
26
 * This generic sequence number class is templated to easily support 
27
 * 8, 16, 32, or 64 bit sequence numbers.  The design of this class 
28
 * was chosen to facilitate tracing these sequence numbers.
29
 */
30
31
class Sequence
32
{
33
public:
34
  /**
35
    * \param sequence the sequence number to set
36
    * \param value the value to set the sequence number
37
    */
38
  template<class T> static void Set (T * sequence, T value);
39
40
  /**
41
    * \param sequence the sequence number to increment
42
    *
43
    * Increment the sequence number by one.
44
    */
45
  template<class T> static void Inc (T * sequence);
46
47
  /**
48
    * \param sequence the sequence number to increment
49
    * \param count the number to increment the sequence number
50
    *
51
    * Increment the sequence number by count.
52
    */
53
  template<class T> static void Inc (T * sequence, T count);
54
55
  /**
56
    * \param sequence the sequence number to decrement
57
    *
58
    * Decrement the sequence number by one.
59
    */
60
  template<class T> static void Dec (T * sequence);
61
62
  /**
63
    * \param sequence the sequence number to decrement
64
    * \param count the number to decrement the sequence number
65
    *
66
    * Decrement sequence number by count.
67
    */
68
  template<class T> static void Dec (T * sequence, T count);
69
70
  /**
71
    * \return true if parameters equal, false otherwise.
72
    *
73
    * \param sequence the sequence number
74
    * \param other the other sequence number for comparison
75
    */
76
  template<class T> static bool IsEqual (T sequence, T other);
77
78
  /**
79
    * \return true if first argument is greater than the second
80
    *
81
    * \param sequence the sequence number
82
    * \param other the other sequence number for comparison
83
    */
84
  template<class T> static bool IsGreater (T sequence, T other);
85
86
  /**
87
    * \return true if first argument is greater than or equal to the second
88
    *
89
    * \param sequence the sequence number
90
    * \param other the other sequence number for comparison
91
    */
92
  template<class T> static bool IsGreaterOrEqual (T sequence, T other);
93
94
  /**
95
    * \return true if first argument is less than the second
96
    *
97
    * \param sequence the sequence number
98
    * \param other the other sequence number for comparison
99
    */
100
  template<class T> static bool IsLess (T sequence, T other);
101
102
  /**
103
    * \return true if first argument is less than or equal to the second
104
    *
105
    * \param sequence the sequence number
106
    * \param other the other sequence number for comparison
107
    */
108
  template<class T> static bool IsLessOrEqual (T sequence, T other);
109
110
};
111
112
} // namespace ns3
113
114
#endif /* SEQUENCE_H */
(-)a/src/common/wscript (+2 lines)
 Lines 34-39    Link Here 
34
        'spectrum-type.cc',
34
        'spectrum-type.cc',
35
        'spectrum-propagation-loss-model.cc',
35
        'spectrum-propagation-loss-model.cc',
36
        'friis-spectrum-propagation-loss.cc',
36
        'friis-spectrum-propagation-loss.cc',
37
        'sequence.cc',
37
        ]
38
        ]
38
39
39
    headers = bld.new_task_gen('ns3header')
40
    headers = bld.new_task_gen('ns3header')
 Lines 67-70    Link Here 
67
        'spectrum-type.h',
68
        'spectrum-type.h',
68
        'spectrum-propagation-loss-model.h',
69
        'spectrum-propagation-loss-model.h',
69
        'friis-spectrum-propagation-loss.h',
70
        'friis-spectrum-propagation-loss.h',
71
        'sequence.h',
70
        ]
72
        ]

Return to bug 385