blob: e8713dd4d2508255a13eaffb6eaaf07314afdb23 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00007
reed@android.com5e5adfd2009-03-07 03:39:23 +00008#include "SkRandom.h"
reed@android.comeff416b2009-03-18 03:08:15 +00009#include "SkTSort.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000010#include "Test.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
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000018static void rand_array(SkRandom& rand, int array[], int n) {
reed@android.comeff416b2009-03-18 03:08:15 +000019 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[],
bungeman@google.com7c56c162013-01-24 15:06:47 +000025 const int array[], const int reference[], int n) {
26 for (int j = 0; j < n; ++j) {
27 if (array[j] != reference[j]) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +000028 ERRORF(reporter, "%sSort [%d] failed %d %d",
29 label, n, array[j], reference[j]);
reed@android.comeff416b2009-03-18 03:08:15 +000030 }
31 }
32}
33
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000034DEF_TEST(Sort, reporter) {
bungeman@google.com7c56c162013-01-24 15:06:47 +000035 /** An array of random numbers to be sorted. */
36 int randomArray[500];
37 /** The reference sort of the random numbers. */
38 int sortedArray[SK_ARRAY_COUNT(randomArray)];
39 /** The random numbers are copied into this array, sorted by an SkSort,
40 then this array is compared against the reference sort. */
41 int workingArray[SK_ARRAY_COUNT(randomArray)];
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000042 SkRandom rand;
reed@android.com5e5adfd2009-03-07 03:39:23 +000043
reed@android.comeff416b2009-03-18 03:08:15 +000044 for (int i = 0; i < 10000; i++) {
bungeman@google.com7c56c162013-01-24 15:06:47 +000045 int count = rand.nextRangeU(1, SK_ARRAY_COUNT(randomArray));
46 rand_array(rand, randomArray, count);
skia.committer@gmail.com4024f322013-01-25 07:06:46 +000047
bungeman@google.com7c56c162013-01-24 15:06:47 +000048 // Use qsort as the reference sort.
49 memcpy(sortedArray, randomArray, sizeof(randomArray));
50 qsort(sortedArray, count, sizeof(sortedArray[0]), compare_int);
reed@android.comeff416b2009-03-18 03:08:15 +000051
bungeman@google.com7c56c162013-01-24 15:06:47 +000052 memcpy(workingArray, randomArray, sizeof(randomArray));
53 SkTHeapSort<int>(workingArray, count);
54 check_sort(reporter, "Heap", workingArray, sortedArray, count);
rileya@google.com5ee3f672012-08-28 14:40:49 +000055
bungeman@google.com7c56c162013-01-24 15:06:47 +000056 memcpy(workingArray, randomArray, sizeof(randomArray));
57 SkTQSort<int>(workingArray, workingArray + count - 1);
58 check_sort(reporter, "Quick", workingArray, sortedArray, count);
caryclark@google.com42639cd2012-06-06 12:03:39 +000059 }
reed@android.com5e5adfd2009-03-07 03:39:23 +000060}
61
62// need tests for SkStrSearch