- Added tests for newly added Common::Queue

- Changed Common::Queue::front and Common::Queue::back to return references instead of values

svn-id: r34240
This commit is contained in:
Johannes Schickel 2008-09-01 16:52:09 +00:00
parent c2f2269852
commit 2db5747642
2 changed files with 116 additions and 13 deletions

View File

@ -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

80
test/common/queue.h Normal file
View File

@ -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());
}
};