Flutter Engine
The Flutter Engine
Functions
RandomTest.cpp File Reference
#include "include/core/SkTypes.h"
#include "include/private/base/SkMath.h"
#include "src/base/SkRandom.h"
#include "src/base/SkTSort.h"
#include "tests/Test.h"
#include <cmath>
#include <cstring>

Go to the source code of this file.

Functions

static bool anderson_darling_test (double p[32])
 
static bool chi_square_test (int bins[256], int e)
 
static double normal_cdf (double z)
 
static void test_random_byte (skiatest::Reporter *reporter, int shift)
 
static void test_random_float (skiatest::Reporter *reporter)
 
static double test_single_gorilla (skiatest::Reporter *reporter, int shift)
 
static void test_gorilla (skiatest::Reporter *reporter)
 
static void test_range (skiatest::Reporter *reporter)
 
 DEF_TEST (Random, reporter)
 

Function Documentation

◆ anderson_darling_test()

static bool anderson_darling_test ( double  p[32])
static

Definition at line 17 of file RandomTest.cpp.

17 {
18 // Min and max Anderson-Darling values allowable for k=32
19 const double kADMin32 = 0.202; // p-value of ~0.1
20 const double kADMax32 = 3.89; // p-value of ~0.99
21
22 // sort p values
23 SkTQSort<double>(p, p + 32);
24
25 // and compute Anderson-Darling statistic to ensure these are uniform
26 double s = 0.0;
27 for(int k = 0; k < 32; k++) {
28 double v = p[k]*(1.0 - p[31-k]);
29 if (v < 1.0e-30) {
30 v = 1.0e-30;
31 }
32 s += (2.0*(k+1)-1.0)*log(v);
33 }
34 double a2 = -32.0 - 0.03125*s;
35
36 return (kADMin32 < a2 && a2 < kADMax32);
37}
struct MyStruct s

◆ chi_square_test()

static bool chi_square_test ( int  bins[256],
int  e 
)
static

Definition at line 39 of file RandomTest.cpp.

39 {
40 // Min and max chisquare values allowable
41 const double kChiSqMin256 = 206.3179; // probability of chance = 0.99 with k=256
42 const double kChiSqMax256 = 311.5603; // probability of chance = 0.01 with k=256
43
44 // compute chi-square
45 double chi2 = 0.0;
46 for (int j = 0; j < 256; ++j) {
47 double delta = bins[j] - e;
48 chi2 += delta*delta/e;
49 }
50
51 return (kChiSqMin256 < chi2 && chi2 < kChiSqMax256);
52}

◆ DEF_TEST()

DEF_TEST ( Random  ,
reporter   
)

Definition at line 185 of file RandomTest.cpp.

185 {
186 // check uniform distributions of each byte in 32-bit word
191
193
195
197}
reporter
Definition: FontMgrTest.cpp:39
static void test_random_byte(skiatest::Reporter *reporter, int shift)
Definition: RandomTest.cpp:64
static void test_random_float(skiatest::Reporter *reporter)
Definition: RandomTest.cpp:76
static void test_range(skiatest::Reporter *reporter)
Definition: RandomTest.cpp:167
static void test_gorilla(skiatest::Reporter *reporter)
Definition: RandomTest.cpp:157

◆ normal_cdf()

static double normal_cdf ( double  z)
static

Definition at line 56 of file RandomTest.cpp.

56 {
57 double t = ((-0.0004406*z*z* + 0.0418198)*z*z + 0.9)*z;
58 t *= -1.77245385091; // -sqrt(PI)
59 double p = 1.0/(1.0 + exp(t));
60
61 return p;
62}

◆ test_gorilla()

static void test_gorilla ( skiatest::Reporter reporter)
static

Definition at line 157 of file RandomTest.cpp.

157 {
158
159 double p[32];
160 for (int bit_position = 0; bit_position < 32; ++bit_position) {
161 p[bit_position] = test_single_gorilla(reporter, bit_position);
162 }
163
165}
static double test_single_gorilla(skiatest::Reporter *reporter, int shift)
Definition: RandomTest.cpp:107
static bool anderson_darling_test(double p[32])
Definition: RandomTest.cpp:17
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286

◆ test_random_byte()

static void test_random_byte ( skiatest::Reporter reporter,
int  shift 
)
static

Definition at line 64 of file RandomTest.cpp.

64 {
65 int bins[256];
66 memset(bins, 0, sizeof(int)*256);
67
68 SkRandom rand;
69 for (int i = 0; i < 256*10000; ++i) {
70 bins[(rand.nextU() >> shift) & 0xff]++;
71 }
72
74}
static bool chi_square_test(int bins[256], int e)
Definition: RandomTest.cpp:39
uint32_t nextU()
Definition: SkRandom.h:42

◆ test_random_float()

static void test_random_float ( skiatest::Reporter reporter)
static

Definition at line 76 of file RandomTest.cpp.

76 {
77 int bins[256];
78 memset(bins, 0, sizeof(int)*256);
79
80 SkRandom rand;
81 for (int i = 0; i < 256*10000; ++i) {
82 float f = rand.nextF();
83 REPORTER_ASSERT(reporter, 0.0f <= f && f < 1.0f);
84 bins[(int)(f*256.f)]++;
85 }
87
88 double p[32];
89 for (int j = 0; j < 32; ++j) {
90 float f = rand.nextF();
91 REPORTER_ASSERT(reporter, 0.0f <= f && f < 1.0f);
92 p[j] = f;
93 }
95}
float nextF()
Definition: SkRandom.h:55

◆ test_range()

static void test_range ( skiatest::Reporter reporter)
static

Definition at line 167 of file RandomTest.cpp.

167 {
168 SkRandom rand;
169
170 // just to make sure we don't crash in this case
171 (void) rand.nextRangeU(0, 0xffffffff);
172
173 // check a case to see if it's uniform
174 int bins[256];
175 memset(bins, 0, sizeof(int)*256);
176 for (int i = 0; i < 256*10000; ++i) {
177 unsigned int u = rand.nextRangeU(17, 17+255);
178 REPORTER_ASSERT(reporter, 17 <= u && u <= 17+255);
179 bins[u - 17]++;
180 }
181
183}
uint32_t nextRangeU(uint32_t min, uint32_t max)
Definition: SkRandom.h:80

◆ test_single_gorilla()

static double test_single_gorilla ( skiatest::Reporter reporter,
int  shift 
)
static

Definition at line 107 of file RandomTest.cpp.

107 {
108 const int kWordWidth = 16;
109 const double kMean = 24108.0;
110 const double kStandardDeviation = 127.0;
111 const int kN = (1 << kWordWidth);
112 const int kNumEntries = kN >> 5; // dividing by 32
113 unsigned int entries[kNumEntries];
114
115 SkRandom rand;
116 memset(entries, 0, sizeof(unsigned int)*kNumEntries);
117 // pre-seed our string value
118 int value = 0;
119 for (int i = 0; i < kWordWidth-1; ++i) {
120 value <<= 1;
121 unsigned int rnd = rand.nextU();
122 value |= ((rnd >> shift) & 0x1);
123 }
124
125 // now make some strings and track them
126 for (int i = 0; i < kN; ++i) {
127 value = SkLeftShift(value, 1);
128 unsigned int rnd = rand.nextU();
129 value |= ((rnd >> shift) & 0x1);
130
131 int index = value & (kNumEntries-1);
132 SkASSERT(index < kNumEntries);
133 int entry_shift = (value >> (kWordWidth-5)) & 0x1f;
134 entries[index] |= (0x1 << entry_shift);
135 }
136
137 // count entries
138 int total = 0;
139 for (int i = 0; i < kNumEntries; ++i) {
140 unsigned int entry = entries[i];
141 while (entry) {
142 total += (entry & 0x1);
143 entry >>= 1;
144 }
145 }
146
147 // convert counts to normal distribution z-score
148 double z = ((kN-total)-kMean)/kStandardDeviation;
149
150 // compute probability from normal distibution CDF
151 double p = normal_cdf(z);
152
153 REPORTER_ASSERT(reporter, 0.01 < p && p < 0.99);
154 return p;
155}
static double normal_cdf(double z)
Definition: RandomTest.cpp:56
#define SkASSERT(cond)
Definition: SkAssert.h:116
static constexpr int32_t SkLeftShift(int32_t value, int32_t shift)
Definition: SkMath.h:37
uint8_t value