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