blob: 9b4642fe2a07a3b6b06e445e6a73d0e9b4cfbcfa [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
reed@android.com5e5adfd2009-03-07 03:39:23 +00008#include "Test.h"
9#include "SkRandom.h"
10#include "SkTSearch.h"
reed@android.comeff416b2009-03-18 03:08:15 +000011#include "SkTSort.h"
reed@android.com5e5adfd2009-03-07 03:39:23 +000012
13extern "C" {
14 int compare_int(const void* a, const void* b) {
15 return *(const int*)a - *(const int*)b;
16 }
17}
18
reed@android.comeff416b2009-03-18 03:08:15 +000019static 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
25static 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
reed@android.com5e5adfd2009-03-07 03:39:23 +000037static void TestSort(skiatest::Reporter* reporter) {
reed@android.comeff416b2009-03-18 03:08:15 +000038 int array[500];
reed@android.com5e5adfd2009-03-07 03:39:23 +000039 SkRandom rand;
40
reed@android.comeff416b2009-03-18 03:08:15 +000041 for (int i = 0; i < 10000; i++) {
42 int count = rand.nextRangeU(1, SK_ARRAY_COUNT(array));
43
44 rand_array(rand, array, count);
reed@android.com5e5adfd2009-03-07 03:39:23 +000045 SkQSort(array, count, sizeof(int), compare_int);
reed@android.comeff416b2009-03-18 03:08:15 +000046 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);
reed@android.com5e5adfd2009-03-07 03:39:23 +000051 }
52}
53
54// need tests for SkStrSearch
55
56#include "TestClassDef.h"
57DEFINE_TESTCLASS("Sort", SortTestClass, TestSort)