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