xref: /third_party/skia/tests/SortTest.cpp (revision cb93a386)
1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2011 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "include/utils/SkRandom.h"
9cb93a386Sopenharmony_ci#include "src/core/SkTSort.h"
10cb93a386Sopenharmony_ci#include "tests/Test.h"
11cb93a386Sopenharmony_ci
12cb93a386Sopenharmony_ci#include <stdlib.h>
13cb93a386Sopenharmony_ci
14cb93a386Sopenharmony_ciextern "C" {
15cb93a386Sopenharmony_ci    static int compare_int(const void* a, const void* b) {
16cb93a386Sopenharmony_ci        return *(const int*)a - *(const int*)b;
17cb93a386Sopenharmony_ci    }
18cb93a386Sopenharmony_ci}
19cb93a386Sopenharmony_ci
20cb93a386Sopenharmony_cistatic void rand_array(SkRandom& rand, int array[], int n) {
21cb93a386Sopenharmony_ci    for (int j = 0; j < n; j++) {
22cb93a386Sopenharmony_ci        array[j] = rand.nextS() & 0xFF;
23cb93a386Sopenharmony_ci    }
24cb93a386Sopenharmony_ci}
25cb93a386Sopenharmony_ci
26cb93a386Sopenharmony_cistatic void check_sort(skiatest::Reporter* reporter, const char label[],
27cb93a386Sopenharmony_ci                       const int array[], const int reference[], int n) {
28cb93a386Sopenharmony_ci    for (int j = 0; j < n; ++j) {
29cb93a386Sopenharmony_ci        if (array[j] != reference[j]) {
30cb93a386Sopenharmony_ci            ERRORF(reporter, "%sSort [%d] failed %d %d",
31cb93a386Sopenharmony_ci                   label, n, array[j], reference[j]);
32cb93a386Sopenharmony_ci        }
33cb93a386Sopenharmony_ci    }
34cb93a386Sopenharmony_ci}
35cb93a386Sopenharmony_ci
36cb93a386Sopenharmony_ciDEF_TEST(Sort, reporter) {
37cb93a386Sopenharmony_ci    /** An array of random numbers to be sorted. */
38cb93a386Sopenharmony_ci    int randomArray[500];
39cb93a386Sopenharmony_ci    /** The reference sort of the random numbers. */
40cb93a386Sopenharmony_ci    int sortedArray[SK_ARRAY_COUNT(randomArray)];
41cb93a386Sopenharmony_ci    /** The random numbers are copied into this array, sorted by an SkSort,
42cb93a386Sopenharmony_ci        then this array is compared against the reference sort. */
43cb93a386Sopenharmony_ci    int workingArray[SK_ARRAY_COUNT(randomArray)];
44cb93a386Sopenharmony_ci    SkRandom    rand;
45cb93a386Sopenharmony_ci
46cb93a386Sopenharmony_ci    for (int i = 0; i < 10000; i++) {
47cb93a386Sopenharmony_ci        int count = rand.nextRangeU(1, SK_ARRAY_COUNT(randomArray));
48cb93a386Sopenharmony_ci        rand_array(rand, randomArray, count);
49cb93a386Sopenharmony_ci
50cb93a386Sopenharmony_ci        // Use qsort as the reference sort.
51cb93a386Sopenharmony_ci        memcpy(sortedArray, randomArray, sizeof(randomArray));
52cb93a386Sopenharmony_ci        qsort(sortedArray, count, sizeof(sortedArray[0]), compare_int);
53cb93a386Sopenharmony_ci
54cb93a386Sopenharmony_ci        memcpy(workingArray, randomArray, sizeof(randomArray));
55cb93a386Sopenharmony_ci        SkTHeapSort<int>(workingArray, count);
56cb93a386Sopenharmony_ci        check_sort(reporter, "Heap", workingArray, sortedArray, count);
57cb93a386Sopenharmony_ci
58cb93a386Sopenharmony_ci        memcpy(workingArray, randomArray, sizeof(randomArray));
59cb93a386Sopenharmony_ci        SkTQSort<int>(workingArray, workingArray + count);
60cb93a386Sopenharmony_ci        check_sort(reporter, "Quick", workingArray, sortedArray, count);
61cb93a386Sopenharmony_ci    }
62cb93a386Sopenharmony_ci}
63cb93a386Sopenharmony_ci
64cb93a386Sopenharmony_ci// need tests for SkStrSearch
65