epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 8 | #include "Test.h" |
| 9 | #include "SkRandom.h" |
| 10 | #include "SkTSearch.h" |
reed@android.com | eff416b | 2009-03-18 03:08:15 +0000 | [diff] [blame] | 11 | #include "SkTSort.h" |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 12 | |
| 13 | extern "C" { |
| 14 | int compare_int(const void* a, const void* b) { |
| 15 | return *(const int*)a - *(const int*)b; |
| 16 | } |
| 17 | } |
| 18 | |
reed@android.com | eff416b | 2009-03-18 03:08:15 +0000 | [diff] [blame] | 19 | static void rand_array(SkRandom& rand, int array[], int n) { |
| 20 | for (int j = 0; j < n; j++) { |
| 21 | array[j] = rand.nextS() & 0xFF; |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | static void check_sort(skiatest::Reporter* reporter, const char label[], |
| 26 | const int array[], int n) { |
| 27 | for (int j = 1; j < n; j++) { |
| 28 | if (array[j-1] > array[j]) { |
| 29 | SkString str; |
| 30 | str.printf("%sSort [%d] failed %d %d", label, n, |
| 31 | array[j-1], array[j]); |
| 32 | reporter->reportFailed(str); |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 37 | static void TestSort(skiatest::Reporter* reporter) { |
reed@android.com | eff416b | 2009-03-18 03:08:15 +0000 | [diff] [blame] | 38 | int array[500]; |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 39 | SkRandom rand; |
| 40 | |
reed@android.com | eff416b | 2009-03-18 03:08:15 +0000 | [diff] [blame] | 41 | for (int i = 0; i < 10000; i++) { |
| 42 | int count = rand.nextRangeU(1, SK_ARRAY_COUNT(array)); |
| 43 | |
| 44 | rand_array(rand, array, count); |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 45 | SkQSort(array, count, sizeof(int), compare_int); |
reed@android.com | eff416b | 2009-03-18 03:08:15 +0000 | [diff] [blame] | 46 | check_sort(reporter, "Quick", array, count); |
| 47 | |
| 48 | rand_array(rand, array, count); |
| 49 | SkTHeapSort<int>(array, count); |
| 50 | check_sort(reporter, "Heap", array, count); |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | // need tests for SkStrSearch |
| 55 | |
| 56 | #include "TestClassDef.h" |
| 57 | DEFINE_TESTCLASS("Sort", SortTestClass, TestSort) |