epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/utils/SkRandom.h" |
| 9 | #include "src/core/SkTSort.h" |
| 10 | #include "tests/Test.h" |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 11 | |
bungeman | 60e0fee | 2015-08-26 05:15:46 -0700 | [diff] [blame] | 12 | #include <stdlib.h> |
| 13 | |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 14 | extern "C" { |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 15 | static int compare_int(const void* a, const void* b) { |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 16 | return *(const int*)a - *(const int*)b; |
| 17 | } |
| 18 | } |
| 19 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 20 | static void rand_array(SkRandom& rand, int array[], int n) { |
reed@android.com | eff416b | 2009-03-18 03:08:15 +0000 | [diff] [blame] | 21 | for (int j = 0; j < n; j++) { |
| 22 | array[j] = rand.nextS() & 0xFF; |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | static void check_sort(skiatest::Reporter* reporter, const char label[], |
bungeman@google.com | 7c56c16 | 2013-01-24 15:06:47 +0000 | [diff] [blame] | 27 | const int array[], const int reference[], int n) { |
| 28 | for (int j = 0; j < n; ++j) { |
| 29 | if (array[j] != reference[j]) { |
halcanary@google.com | a9325fa | 2014-01-10 14:58:10 +0000 | [diff] [blame] | 30 | ERRORF(reporter, "%sSort [%d] failed %d %d", |
| 31 | label, n, array[j], reference[j]); |
reed@android.com | eff416b | 2009-03-18 03:08:15 +0000 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 36 | DEF_TEST(Sort, reporter) { |
bungeman@google.com | 7c56c16 | 2013-01-24 15:06:47 +0000 | [diff] [blame] | 37 | /** 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.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 44 | SkRandom rand; |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 45 | |
reed@android.com | eff416b | 2009-03-18 03:08:15 +0000 | [diff] [blame] | 46 | for (int i = 0; i < 10000; i++) { |
bungeman@google.com | 7c56c16 | 2013-01-24 15:06:47 +0000 | [diff] [blame] | 47 | int count = rand.nextRangeU(1, SK_ARRAY_COUNT(randomArray)); |
| 48 | rand_array(rand, randomArray, count); |
skia.committer@gmail.com | 4024f32 | 2013-01-25 07:06:46 +0000 | [diff] [blame] | 49 | |
bungeman@google.com | 7c56c16 | 2013-01-24 15:06:47 +0000 | [diff] [blame] | 50 | // Use qsort as the reference sort. |
| 51 | memcpy(sortedArray, randomArray, sizeof(randomArray)); |
| 52 | qsort(sortedArray, count, sizeof(sortedArray[0]), compare_int); |
reed@android.com | eff416b | 2009-03-18 03:08:15 +0000 | [diff] [blame] | 53 | |
bungeman@google.com | 7c56c16 | 2013-01-24 15:06:47 +0000 | [diff] [blame] | 54 | memcpy(workingArray, randomArray, sizeof(randomArray)); |
| 55 | SkTHeapSort<int>(workingArray, count); |
| 56 | check_sort(reporter, "Heap", workingArray, sortedArray, count); |
rileya@google.com | 5ee3f67 | 2012-08-28 14:40:49 +0000 | [diff] [blame] | 57 | |
bungeman@google.com | 7c56c16 | 2013-01-24 15:06:47 +0000 | [diff] [blame] | 58 | memcpy(workingArray, randomArray, sizeof(randomArray)); |
| 59 | SkTQSort<int>(workingArray, workingArray + count - 1); |
| 60 | check_sort(reporter, "Quick", workingArray, sortedArray, count); |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 61 | } |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // need tests for SkStrSearch |