1
2 /*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8 #include "Test.h"
9 #include "SkRandom.h"
10 #include "SkTSearch.h"
11 #include "SkTSort.h"
12
13 extern "C" {
compare_int(const void * a,const void * b)14 int compare_int(const void* a, const void* b) {
15 return *(const int*)a - *(const int*)b;
16 }
17 }
18
rand_array(SkRandom & rand,int array[],int n)19 static void rand_array(SkRandom& rand, int array[], int n) {
20 for (int j = 0; j < n; j++) {
21 array[j] = rand.nextS() & 0xFF;
22 }
23 }
24
check_sort(skiatest::Reporter * reporter,const char label[],const int array[],int n)25 static void check_sort(skiatest::Reporter* reporter, const char label[],
26 const int array[], int n) {
27 for (int j = 1; j < n; j++) {
28 if (array[j-1] > array[j]) {
29 SkString str;
30 str.printf("%sSort [%d] failed %d %d", label, n,
31 array[j-1], array[j]);
32 reporter->reportFailed(str);
33 }
34 }
35 }
36
TestSort(skiatest::Reporter * reporter)37 static void TestSort(skiatest::Reporter* reporter) {
38 int array[500];
39 SkRandom rand;
40
41 for (int i = 0; i < 10000; i++) {
42 int count = rand.nextRangeU(1, SK_ARRAY_COUNT(array));
43
44 rand_array(rand, array, count);
45 SkQSort(array, count, sizeof(int), compare_int);
46 check_sort(reporter, "Quick", array, count);
47
48 rand_array(rand, array, count);
49 SkTHeapSort<int>(array, count);
50 check_sort(reporter, "Heap", array, count);
51 }
52 }
53
54 // need tests for SkStrSearch
55
56 #include "TestClassDef.h"
57 DEFINE_TESTCLASS("Sort", SortTestClass, TestSort)
58