diff --git a/common/queue.h b/common/queue.h index e771ece369f..f1881345e88 100644 --- a/common/queue.h +++ b/common/queue.h @@ -35,35 +35,58 @@ namespace Common { */ template class Queue { -protected: - List _queue; public: - Queue() {} - Queue(const List &queueContent) : _queue(queueContent) {} + typedef T value_type; + +public: + Queue() : _impl() {} + Queue(const Queue &queue) : _impl(queue._impl) {} + + Queue &operator=(const Queue &queue) { + _impl = queue._impl; + return *this; + } bool empty() const { - return _queue.empty(); + return _impl.empty(); } + void clear() { - _queue.clear(); + _impl.clear(); } + void push(const T &x) { - _queue.push_back(x); + _impl.push_back(x); } - T back() const { - return _queue.reverse_begin().operator*(); + + T &front() { + return *_impl.begin(); } - T front() const { - return _queue.begin().operator*(); + + const T &front() const { + return *_impl.begin(); } + + T &back() { + return *_impl.reverse_begin(); + } + + const T &back() const { + return *_impl.reverse_begin(); + } + T pop() { T tmp = front(); - _queue.pop_front(); + _impl.pop_front(); return tmp; } + int size() const { - return _queue.size(); + return _impl.size(); } + +private: + List _impl; }; } // End of namespace Common diff --git a/test/common/queue.h b/test/common/queue.h new file mode 100644 index 00000000000..7eedec9a5db --- /dev/null +++ b/test/common/queue.h @@ -0,0 +1,80 @@ +#include + +#include "common/queue.h" + +class QueueTestSuite : public CxxTest::TestSuite { +public: + void test_empty_clear() { + Common::Queue queue; + TS_ASSERT(queue.empty()); + + queue.push(1); + queue.push(2); + TS_ASSERT(!queue.empty()); + + queue.clear(); + + TS_ASSERT(queue.empty()); + } + + void test_size() { + Common::Queue queue; + TS_ASSERT_EQUALS(queue.size(), 0); + + queue.push(5); + TS_ASSERT_EQUALS(queue.size(), 1); + + queue.push(9); + queue.push(0); + TS_ASSERT_EQUALS(queue.size(), 3); + + queue.pop(); + TS_ASSERT_EQUALS(queue.size(), 2); + } + + void test_front_back_pop() { + Common::Queue queue; + + queue.push( 42); + queue.push(-23); + + TS_ASSERT_EQUALS(queue.front(), 42); + TS_ASSERT_EQUALS(queue.back(), -23); + + queue.front() = -23; + queue.back() = 42; + TS_ASSERT_EQUALS(queue.front(), -23); + TS_ASSERT_EQUALS(queue.back(), 42); + + queue.pop(); + TS_ASSERT_EQUALS(queue.front(), 42); + } + + void test_assign() { + Common::Queue q1, q2; + + for (int i = 0; i < 5; ++i) { + q1.push(i); + q2.push(4-i); + } + + Common::Queue q3(q1); + + for (int i = 0; i < 5; ++i) { + TS_ASSERT_EQUALS(q3.front(), i); + q3.pop(); + } + + TS_ASSERT(q3.empty()); + + q3 = q2; + + for (int i = 4; i >= 0; --i) { + TS_ASSERT_EQUALS(q3.front(), i); + q3.pop(); + } + + TS_ASSERT(q3.empty()); + } +}; +