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

(-)0c70949a5006 (+85 lines)
Added Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * Copyright (c) 2009 University of Applied Sciences Rapperswil, Switzerland (HSR)
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:  Fabian Mauchle <fabian.mauchle@hsr.ch>
19
 */
20
21
#include "sequence.h"
22
#include "ns3/test.h"
23
#include "ns3/assert.h"
24
25
//all functions are implemented in the header
26
27
//--sequence test
28
29
namespace ns3
30
{
31
32
class SequenceTest : public TestCase
33
{
34
public:
35
  SequenceTest () : TestCase ("Check sequence mathematical correctness"){}
36
  virtual bool DoRun (void)
37
  {
38
    sequence32_t testValues[][2] = {
39
        {1,2},
40
        {-1,0},
41
        {0,0xefffffff},
42
        {0xefffffff,-1},
43
        {0xf0000000,0},
44
        {1,0xf0000000}
45
    };
46
47
    for(int i = 0; i < 2; i++)
48
      {
49
        sequence32_t    a = testValues[i][0],
50
                        b = testValues[i][1];
51
52
        NS_TEST_ASSERT_MSG_EQ((bool)(a < b), true, "a < b");
53
54
        NS_TEST_ASSERT_MSG_EQ((bool)(a > b), (bool)(b < a), "fail");
55
        NS_TEST_ASSERT_MSG_EQ((bool)(a < b), (bool)(b > a), "fail");
56
        NS_TEST_ASSERT_MSG_EQ((bool)(a <= b), (bool)(b >= a), "fail");
57
        NS_TEST_ASSERT_MSG_EQ((bool)(a >= b), (bool)(b <= a), "fail");
58
        NS_TEST_ASSERT_MSG_NE((bool)(a >= b), (bool)(a < b), "fail");
59
        NS_TEST_ASSERT_MSG_NE((bool)(a <= b), (bool)(a > b), "fail");
60
61
62
      }
63
64
    sequence32_t x = 10, y = 10;
65
    NS_TEST_ASSERT_MSG_EQ((bool)(x <= y), true, "x <= y");
66
    NS_TEST_ASSERT_MSG_EQ((bool)(x >= y), true, "x <= y");
67
    NS_TEST_ASSERT_MSG_EQ((bool)(x == y), true, "x <= y");
68
69
    return GetErrorStatus ();
70
  }
71
};
72
73
74
75
class SequenceTestSuite : public TestSuite
76
{
77
public:
78
  SequenceTestSuite () : TestSuite ("sequence-test", UNIT)
79
  {
80
    AddTestCase (new SequenceTest);
81
  }
82
} sequenceTestSuite;
83
84
}
85
(-)0c70949a5006 (+107 lines)
Added Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * Copyright (c) 2009 University of Applied Sciences Rapperswil, Switzerland (HSR)
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:  Fabian Mauchle <fabian.mauchle@hsr.ch>
19
 */
20
21
#ifndef SEQUENCE_H
22
#define SEQUENCE_H
23
24
#include <stdint.h>
25
26
/**
27
 * \breif generic sequence number
28
 *
29
 * LessOrEqualThan is defined as the compared number itself and the preceding
30
 * 2^(n-1) -1 numbers in the finite field of 2^n
31
 *
32
 * GreaterThan is defined as the proceding 2^(n-1) numbers in the finite field
33
 * of 2^n
34
 *
35
 */
36
37
template <typename T>
38
class Sequence
39
{
40
public:
41
  Sequence () : seq(0) {}
42
  Sequence (const T s) : seq (s) { }
43
  operator T () const { return seq;}
44
  Sequence& operator= (const T s) { seq = s; return *this;}
45
  Sequence& operator+= (const T s) { seq += s; return *this;}
46
  Sequence  operator++ ()              { seq++; return *this;}
47
  Sequence  operator++ (int)           { Sequence ss (seq); seq++; return ss;}
48
  Sequence& operator-= (const T s) { seq -= s; return *this;}
49
  Sequence  operator-- ()              { seq--; return *this;}
50
  Sequence  operator-- (int)           { Sequence ss (seq); seq--; return ss;}
51
52
  T seq;
53
};
54
55
template <typename T>
56
bool operator<= (Sequence<T> a, Sequence<T> b)
57
{
58
  return (b - a) < (((Sequence<T>)(-1))>>1);
59
}
60
61
template <typename T>
62
bool operator>= (Sequence<T> a, Sequence<T> b)
63
{
64
  return b <= a;
65
}
66
67
template <typename T>
68
bool operator< (Sequence<T> a, Sequence<T> b)
69
{
70
  return ! (a >= b);
71
}
72
73
template <typename T>
74
bool operator> (Sequence<T> a, Sequence<T> b)
75
{
76
  return b < a;
77
}
78
79
template <typename T>
80
Sequence<T>  operator+ (const Sequence<T> l, const Sequence<T> r)
81
{
82
  return Sequence<T> (l.seq + r.seq);
83
}
84
85
template <typename T>
86
Sequence<T>  operator+ (const Sequence<T> l, const T r)
87
{
88
  return Sequence<T> (l.seq + r);
89
}
90
91
template <typename T>
92
Sequence<T>  operator- (const Sequence<T> l, const Sequence<T> r)
93
{
94
  return Sequence<T> (l.seq - r.seq);
95
}
96
97
template <typename T>
98
Sequence<T>  operator- (const Sequence<T> l, const T r)
99
{
100
  return Sequence<T> (l.seq - r);
101
}
102
103
typedef Sequence<uint32_t> sequence32_t;
104
typedef Sequence<uint16_t> sequence16_t;
105
typedef Sequence<uint8_t> sequence8_t;
106
107
#endif /* sequence_H_ */
(-)a/src/common/wscript (+2 lines)
 Lines 21-26    Link Here 
21
        'ascii-writer.cc',
21
        'ascii-writer.cc',
22
        'pcap-file.cc',
22
        'pcap-file.cc',
23
        'pcap-file-test-suite.cc',
23
        'pcap-file-test-suite.cc',
24
        'sequence.cc',
24
        ]
25
        ]
25
26
26
    headers = bld.new_task_gen('ns3header')
27
    headers = bld.new_task_gen('ns3header')
 Lines 43-46    Link Here 
43
        'ascii-writer.h',
44
        'ascii-writer.h',
44
        'sgi-hashmap.h',
45
        'sgi-hashmap.h',
45
        'pcap-file.h',
46
        'pcap-file.h',
47
        'sequence.h',
46
        ]
48
        ]

Return to bug 385