From 60b33656863c080e69a845f02d2f6311850efb79 Mon Sep 17 00:00:00 2001 From: Vladimir Menshakov Date: Sun, 27 Dec 2009 09:54:11 +0000 Subject: added tests for sort() functions svn-id: r46615 --- test/common/algorithm.h | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 test/common/algorithm.h (limited to 'test/common/algorithm.h') diff --git a/test/common/algorithm.h b/test/common/algorithm.h new file mode 100644 index 0000000000..b07eb2996e --- /dev/null +++ b/test/common/algorithm.h @@ -0,0 +1,60 @@ +#include + +#include "common/util.h" +#include "common/func.h" +#include "common/algorithm.h" +#include "common/list.h" + +template +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()); + + Common::sort(array, array + ARRAYSIZE(array)); //already sorted one + check_sort(array, array + ARRAYSIZE(array), Common::Less()); + } + { + 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()); + + Common::sort(array, array + ARRAYSIZE(array), Common::Greater()); + check_sort(array, array + ARRAYSIZE(array), Common::Greater()); + } + } + + void test_container_sort() { + const int n = 1000; + Common::List 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()); + } + } +}; -- cgit v1.2.3