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

(-)eedf90d2a386 (+122 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
24
//all functions are implemented in the header
25
26
//--sequence test
27
28
namespace ns3
29
{
30
31
class SequenceTest : public TestCase
32
{
33
public:
34
  SequenceTest () : TestCase ("Check sequence mathematical correctness"){}
35
  virtual bool DoRun (void)
36
  {
37
    sequence32_t testValues[][2] = {
38
        {1,2},
39
        {-1,0},
40
        {0,0xefffffff},
41
        {0xefffffff,-1},
42
        {0xf0000000,0},
43
        {1,0xf0000000}
44
    };
45
46
    for(int i = 0; i < 2; i++)
47
      {
48
        sequence32_t    a = testValues[i][0],
49
                        b = testValues[i][1];
50
51
        NS_TEST_EXPECT_MSG_EQ((bool)(a < b), true, "a < b");
52
53
        NS_TEST_EXPECT_MSG_EQ((bool)(a > b), (bool)(b < a), "(a > b)=(b < a)");
54
        NS_TEST_EXPECT_MSG_EQ((bool)(a < b), (bool)(b > a), "(a < b)=(b > a)");
55
        NS_TEST_EXPECT_MSG_EQ((bool)(a <= b), (bool)(b >= a), "(a <= b)=(b >= a)");
56
        NS_TEST_EXPECT_MSG_EQ((bool)(a >= b), (bool)(b <= a), "(a >= b)=(b <= a)");
57
        NS_TEST_EXPECT_MSG_NE((bool)(a >= b), (bool)(a < b), "(a >= b)=(a < b)");
58
        NS_TEST_EXPECT_MSG_NE((bool)(a <= b), (bool)(a > b), "(a <= b)=(a > b)");
59
      }
60
61
    sequence32_t x = 10, y = 10;
62
    NS_TEST_EXPECT_MSG_EQ((bool)(x <= y), true, "x <= y");
63
    NS_TEST_EXPECT_MSG_EQ((bool)(x >= y), true, "x <= y");
64
    NS_TEST_EXPECT_MSG_EQ((bool)(x == y), true, "x <= y");
65
66
    {
67
      sequence32_t num1 (3), num2 (5);
68
      uint32_t value;
69
70
      value = num1 + num2;
71
      NS_TEST_EXPECT_MSG_EQ (value, 8, "num1 + num2");
72
73
      num1 += num2;
74
      NS_TEST_EXPECT_MSG_EQ (num1, 8, "num1 += num2");
75
76
      ++num1;
77
      NS_TEST_EXPECT_MSG_EQ (num1, 9, "++num1");
78
79
      --num1;
80
      NS_TEST_EXPECT_MSG_EQ (num1, 8, "--num1");
81
82
      num1++;
83
      NS_TEST_EXPECT_MSG_EQ (num1, 9, "num1++");
84
85
      num1--;
86
      NS_TEST_EXPECT_MSG_EQ (num1, 8, "num1--");
87
    }
88
89
    {
90
      NS_TEST_EXPECT_MSG_EQ ((sequence16_t (1000) + sequence16_t (6000)) - sequence16_t (1000), 6000, "(1000 + 6000) - 1000");
91
      NS_TEST_EXPECT_MSG_EQ ((sequence16_t (60000) + sequence16_t (6000)) - sequence16_t (60000), 6000, "(60000 + 6000) - 60000");
92
      NS_TEST_EXPECT_MSG_EQ (Sequence<int16_t> (1000) - Sequence<int16_t> (6000), -5000, "(1000 - 6000)");
93
      NS_TEST_EXPECT_MSG_EQ ((Sequence<int16_t> (60000) + Sequence<int16_t> (1000)) - Sequence<int16_t> (65000), -4000, "(60000 + 1000) - 65000");
94
    }
95
96
    {
97
      sequence32_t num1 (3);
98
99
      NS_TEST_EXPECT_MSG_EQ (num1 + 10, 13, "num1 + 10");
100
      num1 += -1;
101
      NS_TEST_EXPECT_MSG_EQ (num1, 2, "num1 += -1");
102
103
      NS_TEST_EXPECT_MSG_EQ (num1 - (num1 - 100), 100, "num1 - (num1 - 100)");
104
    }
105
106
    return GetErrorStatus ();
107
  }
108
};
109
110
111
112
class SequenceTestSuite : public TestSuite
113
{
114
public:
115
  SequenceTestSuite () : TestSuite ("sequence-test", BVT)
116
  {
117
    AddTestCase (new SequenceTest);
118
  }
119
} sequenceTestSuite;
120
121
}
122
(-)eedf90d2a386 (+82 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 are the compared number itself and the preceding 2^(n-1) -1
30
 * numbers in the finite field of 2^n
31
 *
32
 * GreaterThan are proceding 2^(n-1) numbers in the finite field of 2^n
33
 *
34
 */
35
36
template <typename T>
37
class Sequence
38
{
39
public:
40
  Sequence () : seq(0) {}
41
  Sequence (const T s) : seq (s) { }
42
  operator T () const { return seq;}
43
  Sequence& operator= (const T s) { seq = s; return *this;}
44
  Sequence& operator+= (const T s) { seq += s; return *this;}
45
  Sequence  operator++ ()              { seq++; return *this;}
46
  Sequence  operator++ (int)           { Sequence ss (seq); seq++; return ss;}
47
  Sequence& operator-= (const T s) { seq -= s; return *this;}
48
  Sequence  operator-- ()              { seq--; return *this;}
49
  Sequence  operator-- (int)           { Sequence ss (seq); seq--; return ss;}
50
51
  T seq;
52
};
53
54
template <typename T>
55
bool operator<= (Sequence<T> a, Sequence<T> b)
56
{
57
  return (b - a) < (((Sequence<T>)(-1))>>1);
58
}
59
60
template <typename T>
61
bool operator>= (Sequence<T> a, Sequence<T> b)
62
{
63
  return b <= a;
64
}
65
66
template <typename T>
67
bool operator< (Sequence<T> a, Sequence<T> b)
68
{
69
  return ! (a >= b);
70
}
71
72
template <typename T>
73
bool operator> (Sequence<T> a, Sequence<T> b)
74
{
75
  return b < a;
76
}
77
78
typedef Sequence<uint32_t> sequence32_t;
79
typedef Sequence<uint16_t> sequence16_t;
80
typedef Sequence<uint8_t> sequence8_t;
81
82
#endif /* sequence_H_ */

Return to bug 385