aboutsummaryrefslogtreecommitdiff
path: root/test/common/algorithm.h
blob: 4d53c87099f7255fe24566ae7b4abf24c7d5c861 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <cxxtest/TestSuite.h>

#include "common/util.h"
#include "common/func.h"
#include "common/algorithm.h"
#include "common/list.h"

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 ItemCmp {
		bool operator()(const Item &a, const Item &b) {
			return a.value < b.value;
		}
	};

public:
	void test_pod_sort() {
		{
			int array[] = { 63, 11, 31, 72, 1, 48, 32, 69, 38, 31 };
			Common::sort(array, array + ARRAYSIZE(array));
			checkSort(array, array + ARRAYSIZE(array), Common::Less<int>());

			Common::sort(array, array + ARRAYSIZE(array)); //already sorted one
			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));
			checkSort(array, array + ARRAYSIZE(array), Common::Less<int>());

			Common::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 * 0xDEADBEEF % 1337));

		Common::sort(list.begin(), list.end(), ItemCmp());
		checkSort(list.begin(), list.end(), ItemCmp());

		//already sorted
		Common::sort(list.begin(), list.end(), ItemCmp());
		checkSort(list.begin(), list.end(), ItemCmp());
	}
};