blob: d544ebd6656fed474e732aeabea1f3be9ebcb7fd [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
bungeman60e0fee2015-08-26 05:15:46 -070012#include <stdlib.h>
13
reed@android.com5e5adfd2009-03-07 03:39:23 +000014extern "C" {
caryclark@google.com42639cd2012-06-06 12:03:39 +000015 static int compare_int(const void* a, const void* b) {
reed@android.com5e5adfd2009-03-07 03:39:23 +000016 return *(const int*)a - *(const int*)b;
17 }
18}
19
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000020static void rand_array(SkRandom& rand, int array[], int n) {
reed@android.comeff416b2009-03-18 03:08:15 +000021 for (int j = 0; j < n; j++) {
22 array[j] = rand.nextS() & 0xFF;
23 }
24}
25
26static void check_sort(skiatest::Reporter* reporter, const char label[],
bungeman@google.com7c56c162013-01-24 15:06:47 +000027 const int array[], const int reference[], int n) {
28 for (int j = 0; j < n; ++j) {
29 if (array[j] != reference[j]) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +000030 ERRORF(reporter, "%sSort [%d] failed %d %d",
31 label, n, array[j], reference[j]);
reed@android.comeff416b2009-03-18 03:08:15 +000032 }
33 }
34}
35
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000036DEF_TEST(Sort, reporter) {
bungeman@google.com7c56c162013-01-24 15:06:47 +000037 /** An array of random numbers to be sorted. */
38 int randomArray[500];
39 /** The reference sort of the random numbers. */
40 int sortedArray[SK_ARRAY_COUNT(randomArray)];
41 /** The random numbers are copied into this array, sorted by an SkSort,
42 then this array is compared against the reference sort. */
43 int workingArray[SK_ARRAY_COUNT(randomArray)];
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000044 SkRandom rand;
reed@android.com5e5adfd2009-03-07 03:39:23 +000045
reed@android.comeff416b2009-03-18 03:08:15 +000046 for (int i = 0; i < 10000; i++) {
bungeman@google.com7c56c162013-01-24 15:06:47 +000047 int count = rand.nextRangeU(1, SK_ARRAY_COUNT(randomArray));
48 rand_array(rand, randomArray, count);
skia.committer@gmail.com4024f322013-01-25 07:06:46 +000049
bungeman@google.com7c56c162013-01-24 15:06:47 +000050 // Use qsort as the reference sort.
51 memcpy(sortedArray, randomArray, sizeof(randomArray));
52 qsort(sortedArray, count, sizeof(sortedArray[0]), compare_int);
reed@android.comeff416b2009-03-18 03:08:15 +000053
bungeman@google.com7c56c162013-01-24 15:06:47 +000054 memcpy(workingArray, randomArray, sizeof(randomArray));
55 SkTHeapSort<int>(workingArray, count);
56 check_sort(reporter, "Heap", workingArray, sortedArray, count);
rileya@google.com5ee3f672012-08-28 14:40:49 +000057
bungeman@google.com7c56c162013-01-24 15:06:47 +000058 memcpy(workingArray, randomArray, sizeof(randomArray));
59 SkTQSort<int>(workingArray, workingArray + count - 1);
60 check_sort(reporter, "Quick", workingArray, sortedArray, count);
caryclark@google.com42639cd2012-06-06 12:03:39 +000061 }
reed@android.com5e5adfd2009-03-07 03:39:23 +000062}
63
64// need tests for SkStrSearch