blob: 2ca5553c2e734a53272eed526c0f563a70d1343e [file] [log] [blame]
reed@android.com5e5adfd2009-03-07 03:39:23 +00001#include "Test.h"
2#include "SkRandom.h"
3#include "SkTSearch.h"
reed@android.comeff416b2009-03-18 03:08:15 +00004#include "SkTSort.h"
reed@android.com5e5adfd2009-03-07 03:39:23 +00005
6extern "C" {
7 int compare_int(const void* a, const void* b) {
8 return *(const int*)a - *(const int*)b;
9 }
10}
11
reed@android.comeff416b2009-03-18 03:08:15 +000012static void rand_array(SkRandom& rand, int array[], int n) {
13 for (int j = 0; j < n; j++) {
14 array[j] = rand.nextS() & 0xFF;
15 }
16}
17
18static void check_sort(skiatest::Reporter* reporter, const char label[],
19 const int array[], int n) {
20 for (int j = 1; j < n; j++) {
21 if (array[j-1] > array[j]) {
22 SkString str;
23 str.printf("%sSort [%d] failed %d %d", label, n,
24 array[j-1], array[j]);
25 reporter->reportFailed(str);
26 }
27 }
28}
29
reed@android.com5e5adfd2009-03-07 03:39:23 +000030static void TestSort(skiatest::Reporter* reporter) {
reed@android.comeff416b2009-03-18 03:08:15 +000031 int array[500];
reed@android.com5e5adfd2009-03-07 03:39:23 +000032 SkRandom rand;
33
reed@android.comeff416b2009-03-18 03:08:15 +000034 for (int i = 0; i < 10000; i++) {
35 int count = rand.nextRangeU(1, SK_ARRAY_COUNT(array));
36
37 rand_array(rand, array, count);
reed@android.com5e5adfd2009-03-07 03:39:23 +000038 SkQSort(array, count, sizeof(int), compare_int);
reed@android.comeff416b2009-03-18 03:08:15 +000039 check_sort(reporter, "Quick", array, count);
40
41 rand_array(rand, array, count);
42 SkTHeapSort<int>(array, count);
43 check_sort(reporter, "Heap", array, count);
reed@android.com5e5adfd2009-03-07 03:39:23 +000044 }
45}
46
47// need tests for SkStrSearch
48
49#include "TestClassDef.h"
50DEFINE_TESTCLASS("Sort", SortTestClass, TestSort)