aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--common/queue.h49
-rw-r--r--test/common/queue.h80
2 files changed, 116 insertions, 13 deletions
diff --git a/common/queue.h b/common/queue.h
index e771ece369..f1881345e8 100644
--- a/common/queue.h
+++ b/common/queue.h
@@ -35,35 +35,58 @@ namespace Common {
*/
template<class T>
class Queue {
-protected:
- List<T> _queue;
public:
- Queue<T>() {}
- Queue<T>(const List<T> &queueContent) : _queue(queueContent) {}
+ typedef T value_type;
+
+public:
+ Queue<T>() : _impl() {}
+ Queue<T>(const Queue<T> &queue) : _impl(queue._impl) {}
+
+ Queue<T> &operator=(const Queue<T> &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<T> _impl;
};
} // End of namespace Common
diff --git a/test/common/queue.h b/test/common/queue.h
new file mode 100644
index 0000000000..7eedec9a5d
--- /dev/null
+++ b/test/common/queue.h
@@ -0,0 +1,80 @@
+#include <cxxtest/TestSuite.h>
+
+#include "common/queue.h"
+
+class QueueTestSuite : public CxxTest::TestSuite {
+public:
+ void test_empty_clear() {
+ Common::Queue<int> 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<int> 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<int> 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<int> q1, q2;
+
+ for (int i = 0; i < 5; ++i) {
+ q1.push(i);
+ q2.push(4-i);
+ }
+
+ Common::Queue<int> 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());
+ }
+};
+