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

lordhoto at users.sourceforge.net lordhoto at users.sourceforge.net
Sun Dec 27 11:47:34 CET 2009


Revision: 46617
          http://scummvm.svn.sourceforge.net/scummvm/?rev=46617&view=rev
Author:   lordhoto
Date:     2009-12-27 10:47:34 +0000 (Sun, 27 Dec 2009)

Log Message:
-----------
Cleanup.

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

Modified: scummvm/trunk/test/common/algorithm.h
===================================================================
--- scummvm/trunk/test/common/algorithm.h	2009-12-27 10:05:37 UTC (rev 46616)
+++ scummvm/trunk/test/common/algorithm.h	2009-12-27 10:47:34 UTC (rev 46617)
@@ -5,60 +5,64 @@
 #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
+class AlgorithmTestSuite : public CxxTest::TestSuite {
+	template<typename T, class StrictWeakOrdering>
+	void checkSort(T first, T last, StrictWeakOrdering comp = StrictWeakOrdering()) {
+		// Check whether the container is sorted by the given binary predicate, which
+		// decides whether the first value passed preceeds the second value passed.
+		//
+		// To do that it checks an item and its follower in the container with the
+		// given predicate in reverse order, when it returns false everything is
+		// fine, when it returns false, the follower preceeds the item and thus
+		// the order is violated.
+		for (T prev = first++; first != last; ++prev, ++first)
+			TS_ASSERT_EQUALS(comp(*first, *prev), false);
+	}
+
+	struct Item {
+		int value;
+		Item(int v) : value(v) {}
 	};
-}
 
-struct item {
-	int value;
-	item(int v): value(v) {}
-};
+	struct ItemCmp {
+		bool operator()(const Item &a, const Item &b) {
+			return a.value < b.value;
+		}
+	};
 
-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>());
+			checkSort(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>());
+			checkSort(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>());
+			checkSort(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>());
+			checkSort(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());
+
+		Common::List<Item> list;
+		for(int i = 0; i < n; ++i)
+			list.push_back(Item(i));
+
+		Common::sort(list.begin(), list.end(), ItemCmp());
+		checkSort(list.begin(), list.end(), ItemCmp());
+
 		//already sorted
-		Common::sort(list.begin(), list.end(), item_cmp());
-		check_sort(list.begin(), list.end(), item_cmp());
+		Common::sort(list.begin(), list.end(), ItemCmp());
+		checkSort(list.begin(), list.end(), ItemCmp());
 	}
 };
 


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