Fix sort test.

https://codereview.appspot.com/7199050/


git-svn-id: http://skia.googlecode.com/svn/trunk@7364 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
bungeman@google.com 2013-01-24 15:06:47 +00:00
parent 9780530f5f
commit 7c56c16322

View File

@ -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);
}
}