diff options
-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); } } |