[Scummvm-cvs-logs] SF.net SVN: scummvm:[35974] scummvm/trunk

fingolfin at users.sourceforge.net fingolfin at users.sourceforge.net
Wed Jan 21 03:23:09 CET 2009


Revision: 35974
          http://scummvm.svn.sourceforge.net/scummvm/?rev=35974&view=rev
Author:   fingolfin
Date:     2009-01-21 02:23:09 +0000 (Wed, 21 Jan 2009)

Log Message:
-----------
Made Common::Stack return refs, thus ensuring that it matches exactly the behavior of FixedStack; added unit tests

Modified Paths:
--------------
    scummvm/trunk/common/stack.h
    scummvm/trunk/test/common/queue.h

Added Paths:
-----------
    scummvm/trunk/test/common/fixedstack.h
    scummvm/trunk/test/common/stack.h

Modified: scummvm/trunk/common/stack.h
===================================================================
--- scummvm/trunk/common/stack.h	2009-01-21 02:11:25 UTC (rev 35973)
+++ scummvm/trunk/common/stack.h	2009-01-21 02:23:09 UTC (rev 35974)
@@ -102,10 +102,14 @@
 	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];
+	}
 	T pop() {
 		T tmp = top();
 		_stack.remove_at(size() - 1);
@@ -114,9 +118,12 @@
 	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];
+	}
 };
 
 } // End of namespace Common

Added: scummvm/trunk/test/common/fixedstack.h
===================================================================
--- scummvm/trunk/test/common/fixedstack.h	                        (rev 0)
+++ scummvm/trunk/test/common/fixedstack.h	2009-01-21 02:23:09 UTC (rev 35974)
@@ -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());
+	}
+};
+


Property changes on: scummvm/trunk/test/common/fixedstack.h
___________________________________________________________________
Added: svn:mime-type
   + text/plain
Added: svn:keywords
   + Date Rev Author URL Id
Added: svn:eol-style
   + native

Modified: scummvm/trunk/test/common/queue.h
===================================================================
--- scummvm/trunk/test/common/queue.h	2009-01-21 02:11:25 UTC (rev 35973)
+++ scummvm/trunk/test/common/queue.h	2009-01-21 02:23:09 UTC (rev 35974)
@@ -75,6 +75,8 @@
 		}
 
 		TS_ASSERT(q3.empty());
+		TS_ASSERT(!q1.empty());
+		TS_ASSERT(!q2.empty());
 	}
 };
 

Added: scummvm/trunk/test/common/stack.h
===================================================================
--- scummvm/trunk/test/common/stack.h	                        (rev 0)
+++ scummvm/trunk/test/common/stack.h	2009-01-21 02:23:09 UTC (rev 35974)
@@ -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());
+	}
+};
+


Property changes on: scummvm/trunk/test/common/stack.h
___________________________________________________________________
Added: svn:mime-type
   + text/plain
Added: svn:keywords
   + Date Rev Author URL Id
Added: svn:eol-style
   + native


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list