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