aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--common/stack.h11
-rw-r--r--test/common/fixedstack.h82
-rw-r--r--test/common/queue.h2
-rw-r--r--test/common/stack.h82
4 files changed, 175 insertions, 2 deletions
diff --git a/common/stack.h b/common/stack.h
index 238d0f6433..e20e81028d 100644
--- a/common/stack.h
+++ b/common/stack.h
@@ -102,7 +102,11 @@ public:
void push(const T &x) {
_stack.push_back(x);
}
- T top() const {
+ T &top() {
+ const int s = size();
+ return _stack[s - 1];
+ }
+ const T &top() const {
const int s = size();
return _stack[s - 1];
}
@@ -114,7 +118,10 @@ public:
int size() const {
return _stack.size();
}
- T operator[](int i) {
+ T &operator[](int i) {
+ return _stack[i];
+ }
+ const T &operator[](int i) const {
return _stack[i];
}
};
diff --git a/test/common/fixedstack.h b/test/common/fixedstack.h
new file mode 100644
index 0000000000..62e536d5fa
--- /dev/null
+++ b/test/common/fixedstack.h
@@ -0,0 +1,82 @@
+#include <cxxtest/TestSuite.h>
+
+#include "common/stack.h"
+
+class FixedStackTestSuite : public CxxTest::TestSuite {
+public:
+ void test_empty_clear() {
+ Common::FixedStack<int> stack;
+ TS_ASSERT(stack.empty());
+
+ stack.push(1);
+ stack.push(2);
+ TS_ASSERT(!stack.empty());
+
+ stack.clear();
+
+ TS_ASSERT(stack.empty());
+ }
+
+ void test_size() {
+ Common::FixedStack<int> stack;
+ TS_ASSERT_EQUALS(stack.size(), 0);
+
+ stack.push(5);
+ TS_ASSERT_EQUALS(stack.size(), 1);
+
+ stack.push(9);
+ stack.push(0);
+ TS_ASSERT_EQUALS(stack.size(), 3);
+
+ stack.pop();
+ TS_ASSERT_EQUALS(stack.size(), 2);
+ }
+
+ void test_top_pop() {
+ Common::FixedStack<int> stack;
+
+ stack.push( 42);
+ stack.push(-23);
+
+ TS_ASSERT_EQUALS(stack[0], 42);
+ TS_ASSERT_EQUALS(stack.top(), -23);
+
+ stack[0] = -23;
+ stack.top() = 42;
+ TS_ASSERT_EQUALS(stack[0], -23);
+ TS_ASSERT_EQUALS(stack.top(), 42);
+
+ stack.pop();
+ TS_ASSERT_EQUALS(stack[0], -23);
+ }
+
+ void test_assign() {
+ Common::FixedStack<int> q1, q2;
+
+ for (int i = 0; i <= 4; ++i) {
+ q1.push(4-i);
+ q2.push(i);
+ }
+
+ Common::FixedStack<int> q3(q1);
+
+ for (int i = 0; i < 5; ++i) {
+ TS_ASSERT_EQUALS(q3.top(), i);
+ q3.pop();
+ }
+
+ TS_ASSERT(q3.empty());
+
+ q3 = q2;
+
+ for (int i = 4; i >= 0; --i) {
+ TS_ASSERT_EQUALS(q3.top(), i);
+ q3.pop();
+ }
+
+ TS_ASSERT(q3.empty());
+ TS_ASSERT(!q1.empty());
+ TS_ASSERT(!q2.empty());
+ }
+};
+
diff --git a/test/common/queue.h b/test/common/queue.h
index e3c0cb0a19..505e056722 100644
--- a/test/common/queue.h
+++ b/test/common/queue.h
@@ -75,6 +75,8 @@ public:
}
TS_ASSERT(q3.empty());
+ TS_ASSERT(!q1.empty());
+ TS_ASSERT(!q2.empty());
}
};
diff --git a/test/common/stack.h b/test/common/stack.h
new file mode 100644
index 0000000000..66ba5f5e2b
--- /dev/null
+++ b/test/common/stack.h
@@ -0,0 +1,82 @@
+#include <cxxtest/TestSuite.h>
+
+#include "common/stack.h"
+
+class StackTestSuite : public CxxTest::TestSuite {
+public:
+ void test_empty_clear() {
+ Common::Stack<int> stack;
+ TS_ASSERT(stack.empty());
+
+ stack.push(1);
+ stack.push(2);
+ TS_ASSERT(!stack.empty());
+
+ stack.clear();
+
+ TS_ASSERT(stack.empty());
+ }
+
+ void test_size() {
+ Common::Stack<int> stack;
+ TS_ASSERT_EQUALS(stack.size(), 0);
+
+ stack.push(5);
+ TS_ASSERT_EQUALS(stack.size(), 1);
+
+ stack.push(9);
+ stack.push(0);
+ TS_ASSERT_EQUALS(stack.size(), 3);
+
+ stack.pop();
+ TS_ASSERT_EQUALS(stack.size(), 2);
+ }
+
+ void test_top_pop() {
+ Common::Stack<int> stack;
+
+ stack.push( 42);
+ stack.push(-23);
+
+ TS_ASSERT_EQUALS(stack[0], 42);
+ TS_ASSERT_EQUALS(stack.top(), -23);
+
+ stack[0] = -23;
+ stack.top() = 42;
+ TS_ASSERT_EQUALS(stack[0], -23);
+ TS_ASSERT_EQUALS(stack.top(), 42);
+
+ stack.pop();
+ TS_ASSERT_EQUALS(stack[0], -23);
+ }
+
+ void test_assign() {
+ Common::Stack<int> q1, q2;
+
+ for (int i = 0; i <= 4; ++i) {
+ q1.push(4-i);
+ q2.push(i);
+ }
+
+ Common::Stack<int> q3(q1);
+
+ for (int i = 0; i < 5; ++i) {
+ TS_ASSERT_EQUALS(q3.top(), i);
+ q3.pop();
+ }
+
+ TS_ASSERT(q3.empty());
+
+ q3 = q2;
+
+ for (int i = 4; i >= 0; --i) {
+ TS_ASSERT_EQUALS(q3.top(), i);
+ q3.pop();
+ }
+
+ TS_ASSERT(q3.empty());
+ TS_ASSERT(!q1.empty());
+ TS_ASSERT(!q2.empty());
+ }
+};
+