diff -r eedf90d2a386 -r c10f880de95a src/common/sequence.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/common/sequence.cc Mon Nov 30 09:54:01 2009 +0100 @@ -0,0 +1,122 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2009 University of Applied Sciences Rapperswil, Switzerland (HSR) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Fabian Mauchle + */ + +#include "sequence.h" +#include "ns3/test.h" + +//all functions are implemented in the header + +//--sequence test + +namespace ns3 +{ + +class SequenceTest : public TestCase +{ +public: + SequenceTest () : TestCase ("Check sequence mathematical correctness"){} + virtual bool DoRun (void) + { + sequence32_t testValues[][2] = { + {1,2}, + {-1,0}, + {0,0xefffffff}, + {0xefffffff,-1}, + {0xf0000000,0}, + {1,0xf0000000} + }; + + for(int i = 0; i < 2; i++) + { + sequence32_t a = testValues[i][0], + b = testValues[i][1]; + + NS_TEST_EXPECT_MSG_EQ((bool)(a < b), true, "a < b"); + + NS_TEST_EXPECT_MSG_EQ((bool)(a > b), (bool)(b < a), "(a > b)=(b < a)"); + NS_TEST_EXPECT_MSG_EQ((bool)(a < b), (bool)(b > a), "(a < b)=(b > a)"); + NS_TEST_EXPECT_MSG_EQ((bool)(a <= b), (bool)(b >= a), "(a <= b)=(b >= a)"); + NS_TEST_EXPECT_MSG_EQ((bool)(a >= b), (bool)(b <= a), "(a >= b)=(b <= a)"); + NS_TEST_EXPECT_MSG_NE((bool)(a >= b), (bool)(a < b), "(a >= b)=(a < b)"); + NS_TEST_EXPECT_MSG_NE((bool)(a <= b), (bool)(a > b), "(a <= b)=(a > b)"); + } + + sequence32_t x = 10, y = 10; + NS_TEST_EXPECT_MSG_EQ((bool)(x <= y), true, "x <= y"); + NS_TEST_EXPECT_MSG_EQ((bool)(x >= y), true, "x <= y"); + NS_TEST_EXPECT_MSG_EQ((bool)(x == y), true, "x <= y"); + + { + sequence32_t num1 (3), num2 (5); + uint32_t value; + + value = num1 + num2; + NS_TEST_EXPECT_MSG_EQ (value, 8, "num1 + num2"); + + num1 += num2; + NS_TEST_EXPECT_MSG_EQ (num1, 8, "num1 += num2"); + + ++num1; + NS_TEST_EXPECT_MSG_EQ (num1, 9, "++num1"); + + --num1; + NS_TEST_EXPECT_MSG_EQ (num1, 8, "--num1"); + + num1++; + NS_TEST_EXPECT_MSG_EQ (num1, 9, "num1++"); + + num1--; + NS_TEST_EXPECT_MSG_EQ (num1, 8, "num1--"); + } + + { + NS_TEST_EXPECT_MSG_EQ ((sequence16_t (1000) + sequence16_t (6000)) - sequence16_t (1000), 6000, "(1000 + 6000) - 1000"); + NS_TEST_EXPECT_MSG_EQ ((sequence16_t (60000) + sequence16_t (6000)) - sequence16_t (60000), 6000, "(60000 + 6000) - 60000"); + NS_TEST_EXPECT_MSG_EQ (Sequence (1000) - Sequence (6000), -5000, "(1000 - 6000)"); + NS_TEST_EXPECT_MSG_EQ ((Sequence (60000) + Sequence (1000)) - Sequence (65000), -4000, "(60000 + 1000) - 65000"); + } + + { + sequence32_t num1 (3); + + NS_TEST_EXPECT_MSG_EQ (num1 + 10, 13, "num1 + 10"); + num1 += -1; + NS_TEST_EXPECT_MSG_EQ (num1, 2, "num1 += -1"); + + NS_TEST_EXPECT_MSG_EQ (num1 - (num1 - 100), 100, "num1 - (num1 - 100)"); + } + + return GetErrorStatus (); + } +}; + + + +class SequenceTestSuite : public TestSuite +{ +public: + SequenceTestSuite () : TestSuite ("sequence-test", BVT) + { + AddTestCase (new SequenceTest); + } +} sequenceTestSuite; + +} + diff -r eedf90d2a386 -r c10f880de95a src/common/sequence.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/common/sequence.h Mon Nov 30 09:54:01 2009 +0100 @@ -0,0 +1,82 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2009 University of Applied Sciences Rapperswil, Switzerland (HSR) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Fabian Mauchle + */ + +#ifndef SEQUENCE_H +#define SEQUENCE_H + +#include + +/** + * \breif generic sequence number + * + * LessOrEqualThan are the compared number itself and the preceding 2^(n-1) -1 + * numbers in the finite field of 2^n + * + * GreaterThan are proceding 2^(n-1) numbers in the finite field of 2^n + * + */ + +template +class Sequence +{ +public: + Sequence () : seq(0) {} + Sequence (const T s) : seq (s) { } + operator T () const { return seq;} + Sequence& operator= (const T s) { seq = s; return *this;} + Sequence& operator+= (const T s) { seq += s; return *this;} + Sequence operator++ () { seq++; return *this;} + Sequence operator++ (int) { Sequence ss (seq); seq++; return ss;} + Sequence& operator-= (const T s) { seq -= s; return *this;} + Sequence operator-- () { seq--; return *this;} + Sequence operator-- (int) { Sequence ss (seq); seq--; return ss;} + + T seq; +}; + +template +bool operator<= (Sequence a, Sequence b) +{ + return (b - a) < (((Sequence)(-1))>>1); +} + +template +bool operator>= (Sequence a, Sequence b) +{ + return b <= a; +} + +template +bool operator< (Sequence a, Sequence b) +{ + return ! (a >= b); +} + +template +bool operator> (Sequence a, Sequence b) +{ + return b < a; +} + +typedef Sequence sequence32_t; +typedef Sequence sequence16_t; +typedef Sequence sequence8_t; + +#endif /* sequence_H_ */