Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Functions
SortTest.cpp File Reference
#include "src/base/SkRandom.h"
#include "src/base/SkTSort.h"
#include "tests/Test.h"
#include <cstdlib>
#include <cstring>
#include <string>

Go to the source code of this file.

Functions

static int compare_int (const void *a, const void *b)
 
static void rand_array (SkRandom &rand, int array[], int n)
 
static void check_sort (skiatest::Reporter *reporter, const char label[], const int array[], const int reference[], int n)
 
 DEF_TEST (Sort, reporter)
 

Function Documentation

◆ check_sort()

static void check_sort ( skiatest::Reporter reporter,
const char  label[],
const int  array[],
const int  reference[],
int  n 
)
static

Definition at line 28 of file SortTest.cpp.

29 {
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}
reporter
#define ERRORF(r,...)
Definition Test.h:293

◆ compare_int()

static int compare_int ( const void *  a,
const void *  b 
)
static

Definition at line 17 of file SortTest.cpp.

17 {
18 return *(const int*)a - *(const int*)b;
19 }
static bool b
struct MyStruct a[10]

◆ DEF_TEST()

DEF_TEST ( Sort  ,
reporter   
)

An array of random numbers to be sorted.

The reference sort of the random numbers.

The random numbers are copied into this array, sorted by an SkSort, then this array is compared against the reference sort.

Definition at line 38 of file SortTest.cpp.

38 {
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}
int count
static void rand_array(SkRandom &rand, int array[], int n)
Definition SortTest.cpp:22
static int compare_int(const void *a, const void *b)
Definition SortTest.cpp:17
static void check_sort(skiatest::Reporter *reporter, const char label[], const int array[], const int reference[], int n)
Definition SortTest.cpp:28
uint32_t nextRangeU(uint32_t min, uint32_t max)
Definition SkRandom.h:80

◆ rand_array()

static void rand_array ( SkRandom rand,
int  array[],
int  n 
)
static

Definition at line 22 of file SortTest.cpp.

22 {
23 for (int j = 0; j < n; j++) {
24 array[j] = rand.nextS() & 0xFF;
25 }
26}
int32_t nextS()
Definition SkRandom.h:50