blob: 4a5631daef8132716496aac4ebc0a10f9f19f1bf [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"
reed@android.comeff416b2009-03-18 03:08:15 +000010#include "SkTSort.h"
reed@android.com5e5adfd2009-03-07 03:39:23 +000011
12extern "C" {
caryclark@google.com42639cd2012-06-06 12:03:39 +000013 static int compare_int(const void* a, const void* b) {
reed@android.com5e5adfd2009-03-07 03:39:23 +000014 return *(const int*)a - *(const int*)b;
15 }
16}
17
reed@android.comeff416b2009-03-18 03:08:15 +000018static void rand_array(SkRandom& rand, int array[], int n) {
19 for (int j = 0; j < n; j++) {
20 array[j] = rand.nextS() & 0xFF;
21 }
22}
23
24static void check_sort(skiatest::Reporter* reporter, const char label[],
25 const int array[], int n) {
26 for (int j = 1; j < n; j++) {
27 if (array[j-1] > array[j]) {
28 SkString str;
29 str.printf("%sSort [%d] failed %d %d", label, n,
30 array[j-1], array[j]);
31 reporter->reportFailed(str);
32 }
33 }
34}
35
reed@android.com5e5adfd2009-03-07 03:39:23 +000036static void TestSort(skiatest::Reporter* reporter) {
reed@android.comeff416b2009-03-18 03:08:15 +000037 int array[500];
reed@android.com5e5adfd2009-03-07 03:39:23 +000038 SkRandom rand;
39
reed@android.comeff416b2009-03-18 03:08:15 +000040 for (int i = 0; i < 10000; i++) {
41 int count = rand.nextRangeU(1, SK_ARRAY_COUNT(array));
42
43 rand_array(rand, array, count);
reed@android.comeff416b2009-03-18 03:08:15 +000044 SkTHeapSort<int>(array, count);
45 check_sort(reporter, "Heap", array, count);
reed@android.com5e5adfd2009-03-07 03:39:23 +000046 }
caryclark@google.com42639cd2012-06-06 12:03:39 +000047 if (false) { // avoid bit rot, suppress warning
48 compare_int(array, array);
49 }
reed@android.com5e5adfd2009-03-07 03:39:23 +000050}
51
52// need tests for SkStrSearch
53
54#include "TestClassDef.h"
55DEFINE_TESTCLASS("Sort", SortTestClass, TestSort)