Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(456)

Side by Side Diff: tests/RandomTest.cpp

Issue 23576015: Change old PRG to be SkLCGRandom; change new one to SkRandom (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Fix some spurious SkMWCRandoms Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tests/RTreeTest.cpp ('k') | tests/RegionTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2013 Google Inc. 3 * Copyright 2013 Google Inc.
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 8
9 #include "Test.h" 9 #include "Test.h"
10 #include "SkRandom.h" 10 #include "SkRandom.h"
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 t *= -1.77245385091; // -sqrt(PI) 54 t *= -1.77245385091; // -sqrt(PI)
55 double p = 1.0/(1.0 + exp(t)); 55 double p = 1.0/(1.0 + exp(t));
56 56
57 return p; 57 return p;
58 } 58 }
59 59
60 static void test_random_byte(skiatest::Reporter* reporter, int shift) { 60 static void test_random_byte(skiatest::Reporter* reporter, int shift) {
61 int bins[256]; 61 int bins[256];
62 memset(bins, 0, sizeof(int)*256); 62 memset(bins, 0, sizeof(int)*256);
63 63
64 SkMWCRandom rand; 64 SkRandom rand;
65 for (int i = 0; i < 256*10000; ++i) { 65 for (int i = 0; i < 256*10000; ++i) {
66 bins[(rand.nextU() >> shift) & 0xff]++; 66 bins[(rand.nextU() >> shift) & 0xff]++;
67 } 67 }
68 68
69 REPORTER_ASSERT(reporter, chi_square_test(bins, 10000)); 69 REPORTER_ASSERT(reporter, chi_square_test(bins, 10000));
70 } 70 }
71 71
72 static void test_random_float(skiatest::Reporter* reporter) { 72 static void test_random_float(skiatest::Reporter* reporter) {
73 int bins[256]; 73 int bins[256];
74 memset(bins, 0, sizeof(int)*256); 74 memset(bins, 0, sizeof(int)*256);
75 75
76 SkMWCRandom rand; 76 SkRandom rand;
77 for (int i = 0; i < 256*10000; ++i) { 77 for (int i = 0; i < 256*10000; ++i) {
78 float f = rand.nextF(); 78 float f = rand.nextF();
79 REPORTER_ASSERT(reporter, 0.0f <= f && f < 1.0f); 79 REPORTER_ASSERT(reporter, 0.0f <= f && f < 1.0f);
80 bins[(int)(f*256.f)]++; 80 bins[(int)(f*256.f)]++;
81 } 81 }
82 REPORTER_ASSERT(reporter, chi_square_test(bins, 10000)); 82 REPORTER_ASSERT(reporter, chi_square_test(bins, 10000));
83 83
84 double p[32]; 84 double p[32];
85 for (int j = 0; j < 32; ++j) { 85 for (int j = 0; j < 32; ++j) {
86 float f = rand.nextF(); 86 float f = rand.nextF();
(...skipping 14 matching lines...) Expand all
101 // The original test used 26 bit strings, but is somewhat slow. This version use s 16 101 // The original test used 26 bit strings, but is somewhat slow. This version use s 16
102 // bits which is less rigorous but much faster to generate. 102 // bits which is less rigorous but much faster to generate.
103 static double test_single_gorilla(skiatest::Reporter* reporter, int shift) { 103 static double test_single_gorilla(skiatest::Reporter* reporter, int shift) {
104 const int kWordWidth = 16; 104 const int kWordWidth = 16;
105 const double kMean = 24108.0; 105 const double kMean = 24108.0;
106 const double kStandardDeviation = 127.0; 106 const double kStandardDeviation = 127.0;
107 const int kN = (1 << kWordWidth); 107 const int kN = (1 << kWordWidth);
108 const int kNumEntries = kN >> 5; // dividing by 32 108 const int kNumEntries = kN >> 5; // dividing by 32
109 unsigned int entries[kNumEntries]; 109 unsigned int entries[kNumEntries];
110 110
111 SkMWCRandom rand; 111 SkRandom rand;
112 memset(entries, 0, sizeof(unsigned int)*kNumEntries); 112 memset(entries, 0, sizeof(unsigned int)*kNumEntries);
113 // pre-seed our string value 113 // pre-seed our string value
114 int value = 0; 114 int value = 0;
115 for (int i = 0; i < kWordWidth-1; ++i) { 115 for (int i = 0; i < kWordWidth-1; ++i) {
116 value <<= 1; 116 value <<= 1;
117 unsigned int rnd = rand.nextU(); 117 unsigned int rnd = rand.nextU();
118 value |= ((rnd >> shift) & 0x1); 118 value |= ((rnd >> shift) & 0x1);
119 } 119 }
120 120
121 // now make some strings and track them 121 // now make some strings and track them
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 154
155 double p[32]; 155 double p[32];
156 for (int bit_position = 0; bit_position < 32; ++bit_position) { 156 for (int bit_position = 0; bit_position < 32; ++bit_position) {
157 p[bit_position] = test_single_gorilla(reporter, bit_position); 157 p[bit_position] = test_single_gorilla(reporter, bit_position);
158 } 158 }
159 159
160 REPORTER_ASSERT(reporter, anderson_darling_test(p)); 160 REPORTER_ASSERT(reporter, anderson_darling_test(p));
161 } 161 }
162 162
163 static void test_range(skiatest::Reporter* reporter) { 163 static void test_range(skiatest::Reporter* reporter) {
164 SkMWCRandom rand; 164 SkRandom rand;
165 165
166 // just to make sure we don't crash in this case 166 // just to make sure we don't crash in this case
167 (void) rand.nextRangeU(0, 0xffffffff); 167 (void) rand.nextRangeU(0, 0xffffffff);
168 168
169 // check a case to see if it's uniform 169 // check a case to see if it's uniform
170 int bins[256]; 170 int bins[256];
171 memset(bins, 0, sizeof(int)*256); 171 memset(bins, 0, sizeof(int)*256);
172 for (int i = 0; i < 256*10000; ++i) { 172 for (int i = 0; i < 256*10000; ++i) {
173 unsigned int u = rand.nextRangeU(17, 17+255); 173 unsigned int u = rand.nextRangeU(17, 17+255);
174 REPORTER_ASSERT(reporter, 17 <= u && u <= 17+255); 174 REPORTER_ASSERT(reporter, 17 <= u && u <= 17+255);
(...skipping 12 matching lines...) Expand all
187 187
188 test_random_float(reporter); 188 test_random_float(reporter);
189 189
190 test_gorilla(reporter); 190 test_gorilla(reporter);
191 191
192 test_range(reporter); 192 test_range(reporter);
193 } 193 }
194 194
195 #include "TestClassDef.h" 195 #include "TestClassDef.h"
196 DEFINE_TESTCLASS("Random", RandomTestClass, TestRandom) 196 DEFINE_TESTCLASS("Random", RandomTestClass, TestRandom)
OLDNEW
« no previous file with comments | « tests/RTreeTest.cpp ('k') | tests/RegionTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698