diff -r 0c70949a5006 src/common/sequence.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/common/sequence.cc Mon Nov 23 15:21:09 2009 +0100 @@ -0,0 +1,85 @@ +/* -*- 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" +#include "ns3/assert.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_ASSERT_MSG_EQ((bool)(a < b), true, "a < b"); + + NS_TEST_ASSERT_MSG_EQ((bool)(a > b), (bool)(b < a), "fail"); + NS_TEST_ASSERT_MSG_EQ((bool)(a < b), (bool)(b > a), "fail"); + NS_TEST_ASSERT_MSG_EQ((bool)(a <= b), (bool)(b >= a), "fail"); + NS_TEST_ASSERT_MSG_EQ((bool)(a >= b), (bool)(b <= a), "fail"); + NS_TEST_ASSERT_MSG_NE((bool)(a >= b), (bool)(a < b), "fail"); + NS_TEST_ASSERT_MSG_NE((bool)(a <= b), (bool)(a > b), "fail"); + + + } + + sequence32_t x = 10, y = 10; + NS_TEST_ASSERT_MSG_EQ((bool)(x <= y), true, "x <= y"); + NS_TEST_ASSERT_MSG_EQ((bool)(x >= y), true, "x <= y"); + NS_TEST_ASSERT_MSG_EQ((bool)(x == y), true, "x <= y"); + + return GetErrorStatus (); + } +}; + + + +class SequenceTestSuite : public TestSuite +{ +public: + SequenceTestSuite () : TestSuite ("sequence-test", UNIT) + { + AddTestCase (new SequenceTest); + } +} sequenceTestSuite; + +} + diff -r 0c70949a5006 src/common/sequence.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/common/sequence.h Mon Nov 23 15:21:09 2009 +0100 @@ -0,0 +1,107 @@ +/* -*- 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 is defined as the compared number itself and the preceding + * 2^(n-1) -1 numbers in the finite field of 2^n + * + * GreaterThan is defined as the 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; +} + +template +Sequence operator+ (const Sequence l, const Sequence r) +{ + return Sequence (l.seq + r.seq); +} + +template +Sequence operator+ (const Sequence l, const T r) +{ + return Sequence (l.seq + r); +} + +template +Sequence operator- (const Sequence l, const Sequence r) +{ + return Sequence (l.seq - r.seq); +} + +template +Sequence operator- (const Sequence l, const T r) +{ + return Sequence (l.seq - r); +} + +typedef Sequence sequence32_t; +typedef Sequence sequence16_t; +typedef Sequence sequence8_t; + +#endif /* sequence_H_ */ diff -r 0c70949a5006 src/common/wscript --- a/src/common/wscript Sun Nov 22 18:27:14 2009 +0000 +++ b/src/common/wscript Mon Nov 23 15:21:09 2009 +0100 @@ -21,6 +21,7 @@ 'ascii-writer.cc', 'pcap-file.cc', 'pcap-file-test-suite.cc', + 'sequence.cc', ] headers = bld.new_task_gen('ns3header') @@ -43,4 +44,5 @@ 'ascii-writer.h', 'sgi-hashmap.h', 'pcap-file.h', + 'sequence.h', ]