diff options
author | bungeman@google.com <bungeman@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2013-01-24 15:06:47 +0000 |
---|---|---|
committer | bungeman@google.com <bungeman@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2013-01-24 15:06:47 +0000 |
commit | 7c56c16322ff64de969450cc6dc30605e8537a02 (patch) | |
tree | f85138f553bfe6672860a8dd869f6032c93ce378 /tests/SortTest.cpp | |
parent | 9780530f5fb5032846b120bcd207ba5751a9b2b6 (diff) |
Fix sort test.
https://codereview.appspot.com/7199050/
git-svn-id: http://skia.googlecode.com/svn/trunk@7364 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'tests/SortTest.cpp')
-rw-r--r-- | tests/SortTest.cpp | 39 |
1 files changed, 23 insertions, 16 deletions
diff --git a/tests/SortTest.cpp b/tests/SortTest.cpp index 4783ba989f..b9c32fdb81 100644 --- a/tests/SortTest.cpp +++ b/tests/SortTest.cpp @@ -22,34 +22,41 @@ static void rand_array(SkRandom& rand, int array[], int n) { } static void check_sort(skiatest::Reporter* reporter, const char label[], - const int array[], int n) { - for (int j = 1; j < n; j++) { - if (array[j-1] > array[j]) { + const int array[], const int reference[], int n) { + for (int j = 0; j < n; ++j) { + if (array[j] != reference[j]) { SkString str; - str.printf("%sSort [%d] failed %d %d", label, n, - array[j-1], array[j]); + str.printf("%sSort [%d] failed %d %d", label, n, array[j], reference[j]); reporter->reportFailed(str); } } } static void TestSort(skiatest::Reporter* reporter) { - int array[500]; + /** An array of random numbers to be sorted. */ + int randomArray[500]; + /** The reference sort of the random numbers. */ + int sortedArray[SK_ARRAY_COUNT(randomArray)]; + /** The random numbers are copied into this array, sorted by an SkSort, + then this array is compared against the reference sort. */ + int workingArray[SK_ARRAY_COUNT(randomArray)]; SkRandom rand; for (int i = 0; i < 10000; i++) { - int count = rand.nextRangeU(1, SK_ARRAY_COUNT(array)); + int count = rand.nextRangeU(1, SK_ARRAY_COUNT(randomArray)); + rand_array(rand, randomArray, count); + + // Use qsort as the reference sort. + memcpy(sortedArray, randomArray, sizeof(randomArray)); + qsort(sortedArray, count, sizeof(sortedArray[0]), compare_int); - rand_array(rand, array, count); - SkTHeapSort<int>(array, count); - check_sort(reporter, "Heap", array, count); + memcpy(workingArray, randomArray, sizeof(randomArray)); + SkTHeapSort<int>(workingArray, count); + check_sort(reporter, "Heap", workingArray, sortedArray, count); - rand_array(rand, array, count); - SkTQSort<int>(array, array + count - 1); - check_sort(reporter, "Quick", array, count); - } - if (false) { // avoid bit rot, suppress warning - compare_int(array, array); + memcpy(workingArray, randomArray, sizeof(randomArray)); + SkTQSort<int>(workingArray, workingArray + count - 1); + check_sort(reporter, "Quick", workingArray, sortedArray, count); } } |