[Scummvm-cvs-logs] SF.net SVN: scummvm:[46615] scummvm/trunk/test/common/algorithm.h

megath at users.sourceforge.net megath at users.sourceforge.net
Sun Dec 27 10:54:12 CET 2009


Revision: 46615
          http://scummvm.svn.sourceforge.net/scummvm/?rev=46615&view=rev
Author:   megath
Date:     2009-12-27 09:54:11 +0000 (Sun, 27 Dec 2009)

Log Message:
-----------
added tests for sort() functions

Added Paths:
-----------
    scummvm/trunk/test/common/algorithm.h

Added: scummvm/trunk/test/common/algorithm.h
===================================================================
--- scummvm/trunk/test/common/algorithm.h	                        (rev 0)
+++ scummvm/trunk/test/common/algorithm.h	2009-12-27 09:54:11 UTC (rev 46615)
@@ -0,0 +1,60 @@
+#include <cxxtest/TestSuite.h>
+
+#include "common/util.h"
+#include "common/func.h"
+#include "common/algorithm.h"
+#include "common/list.h"
+
+template<typename T, class StrictWeakOrdering >
+void check_sort(T first, T last, StrictWeakOrdering comp = StrictWeakOrdering()) {
+	for(T i = first; i != last; ++i) {
+		T next = i;
+		++next;
+		if (next == last)
+			break;
+		TS_ASSERT_EQUALS(comp(*next, *i), false); //prev <= next
+	};
+}
+
+struct item {
+	int value;
+	item(int v): value(v) {}
+};
+
+struct item_cmp {
+	bool operator()(const item &a, const item &b) {
+		return a.value < b.value;
+	}
+};
+
+class AlgorithmTestSuite : public CxxTest::TestSuite {
+public:
+	void test_pod_sort() {
+		{
+			int array[] = { 63, 11, 31, 72, 1, 48, 32, 69, 38, 31 };
+			Common::sort(array, array + ARRAYSIZE(array));
+			check_sort(array, array + ARRAYSIZE(array), Common::Less<int>());
+
+			Common::sort(array, array + ARRAYSIZE(array)); //already sorted one
+			check_sort(array, array + ARRAYSIZE(array), Common::Less<int>());
+		}
+		{
+			int array[] = { 90, 80, 70, 60, 50, 40, 30, 20, 10 };
+			Common::sort(array, array + ARRAYSIZE(array));
+			check_sort(array, array + ARRAYSIZE(array), Common::Less<int>());
+
+			Common::sort(array, array + ARRAYSIZE(array), Common::Greater<int>());
+			check_sort(array, array + ARRAYSIZE(array), Common::Greater<int>());
+		}
+	}
+	
+	void test_container_sort() {
+		const int n = 1000;
+		Common::List<item> list;
+		for(int i = 0; i < n; ++i) {
+			list.push_back(item(i));
+			Common::sort(list.begin(), list.end(), item_cmp());
+			check_sort(list.begin(), list.end(), item_cmp());
+		}
+	}
+};


Property changes on: scummvm/trunk/test/common/algorithm.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