blob: a114f32f762459b9986fb5298a0fb18b701ea9bc [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 "Test.h"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00009#include "TestClassDef.h"
reed@android.com5e5adfd2009-03-07 03:39:23 +000010#include "SkRandom.h"
reed@android.comeff416b2009-03-18 03:08:15 +000011#include "SkTSort.h"
reed@android.com5e5adfd2009-03-07 03:39:23 +000012
13extern "C" {
caryclark@google.com42639cd2012-06-06 12:03:39 +000014 static int compare_int(const void* a, const void* b) {
reed@android.com5e5adfd2009-03-07 03:39:23 +000015 return *(const int*)a - *(const int*)b;
16 }
17}
18
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000019static void rand_array(SkRandom& rand, int array[], int n) {
reed@android.comeff416b2009-03-18 03:08:15 +000020 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[],
bungeman@google.com7c56c162013-01-24 15:06:47 +000026 const int array[], const int reference[], int n) {
27 for (int j = 0; j < n; ++j) {
28 if (array[j] != reference[j]) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +000029 ERRORF(reporter, "%sSort [%d] failed %d %d",
30 label, n, array[j], reference[j]);
reed@android.comeff416b2009-03-18 03:08:15 +000031 }
32 }
33}
34
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000035DEF_TEST(Sort, reporter) {
bungeman@google.com7c56c162013-01-24 15:06:47 +000036 /** An array of random numbers to be sorted. */
37 int randomArray[500];
38 /** The reference sort of the random numbers. */
39 int sortedArray[SK_ARRAY_COUNT(randomArray)];
40 /** The random numbers are copied into this array, sorted by an SkSort,
41 then this array is compared against the reference sort. */
42 int workingArray[SK_ARRAY_COUNT(randomArray)];
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000043 SkRandom rand;
reed@android.com5e5adfd2009-03-07 03:39:23 +000044
reed@android.comeff416b2009-03-18 03:08:15 +000045 for (int i = 0; i < 10000; i++) {
bungeman@google.com7c56c162013-01-24 15:06:47 +000046 int count = rand.nextRangeU(1, SK_ARRAY_COUNT(randomArray));
47 rand_array(rand, randomArray, count);
skia.committer@gmail.com4024f322013-01-25 07:06:46 +000048
bungeman@google.com7c56c162013-01-24 15:06:47 +000049 // Use qsort as the reference sort.
50 memcpy(sortedArray, randomArray, sizeof(randomArray));
51 qsort(sortedArray, count, sizeof(sortedArray[0]), compare_int);
reed@android.comeff416b2009-03-18 03:08:15 +000052
bungeman@google.com7c56c162013-01-24 15:06:47 +000053 memcpy(workingArray, randomArray, sizeof(randomArray));
54 SkTHeapSort<int>(workingArray, count);
55 check_sort(reporter, "Heap", workingArray, sortedArray, count);
rileya@google.com5ee3f672012-08-28 14:40:49 +000056
bungeman@google.com7c56c162013-01-24 15:06:47 +000057 memcpy(workingArray, randomArray, sizeof(randomArray));
58 SkTQSort<int>(workingArray, workingArray + count - 1);
59 check_sort(reporter, "Quick", workingArray, sortedArray, count);
caryclark@google.com42639cd2012-06-06 12:03:39 +000060 }
reed@android.com5e5adfd2009-03-07 03:39:23 +000061}
62
63// need tests for SkStrSearch