1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
15 numTestSamples = 10000
18 type statsResults struct {
25 func max(a, b float64) float64 {
32 func nearEqual(a, b, closeEnough, maxError float64) bool {
33 absDiff := math.Abs(a - b)
34 if absDiff < closeEnough { // Necessary when one value is zero and one value is close to zero.
37 return absDiff/max(math.Abs(a), math.Abs(b)) < maxError
40 var testSeeds = []int64{1, 1754801282, 1698661970, 1550503961}
42 // checkSimilarDistribution returns success if the mean and stddev of the
43 // two statsResults are similar.
44 func (this *statsResults) checkSimilarDistribution(expected *statsResults) error {
45 if !nearEqual(this.mean, expected.mean, expected.closeEnough, expected.maxError) {
46 s := fmt.Sprintf("mean %v != %v (allowed error %v, %v)", this.mean, expected.mean, expected.closeEnough, expected.maxError)
50 if !nearEqual(this.stddev, expected.stddev, 0, expected.maxError) {
51 s := fmt.Sprintf("stddev %v != %v (allowed error %v, %v)", this.stddev, expected.stddev, expected.closeEnough, expected.maxError)
58 func getStatsResults(samples []float64) *statsResults {
59 res := new(statsResults)
61 for i := range samples {
64 res.mean = sum / float64(len(samples))
66 for i := range samples {
67 devsum += math.Pow(samples[i]-res.mean, 2)
69 res.stddev = math.Sqrt(devsum / float64(len(samples)))
73 func checkSampleDistribution(t *testing.T, samples []float64, expected *statsResults) {
74 actual := getStatsResults(samples)
75 err := actual.checkSimilarDistribution(expected)
81 func checkSampleSliceDistributions(t *testing.T, samples []float64, nslices int, expected *statsResults) {
82 chunk := len(samples) / nslices
83 for i := 0; i < nslices; i++ {
87 high = len(samples) - 1
89 high = (i + 1) * chunk
91 checkSampleDistribution(t, samples[low:high], expected)
96 // Normal distribution tests
99 func generateNormalSamples(nsamples int, mean, stddev float64, seed int64) []float64 {
100 r := New(NewSource(seed))
101 samples := make([]float64, nsamples)
102 for i := range samples {
103 samples[i] = r.NormFloat64()*stddev + mean
108 func testNormalDistribution(t *testing.T, nsamples int, mean, stddev float64, seed int64) {
109 //fmt.Printf("testing nsamples=%v mean=%v stddev=%v seed=%v\n", nsamples, mean, stddev, seed);
111 samples := generateNormalSamples(nsamples, mean, stddev, seed)
112 errorScale := max(1.0, stddev) // Error scales with stddev
113 expected := &statsResults{mean, stddev, 0.10 * errorScale, 0.08 * errorScale}
115 // Make sure that the entire set matches the expected distribution.
116 checkSampleDistribution(t, samples, expected)
118 // Make sure that each half of the set matches the expected distribution.
119 checkSampleSliceDistributions(t, samples, 2, expected)
121 // Make sure that each 7th of the set matches the expected distribution.
122 checkSampleSliceDistributions(t, samples, 7, expected)
127 func TestStandardNormalValues(t *testing.T) {
128 for _, seed := range testSeeds {
129 testNormalDistribution(t, numTestSamples, 0, 1, seed)
133 func TestNonStandardNormalValues(t *testing.T) {
134 for sd := 0.5; sd < 1000; sd *= 2 {
135 for m := 0.5; m < 1000; m *= 2 {
136 for _, seed := range testSeeds {
137 testNormalDistribution(t, numTestSamples, m, sd, seed)
144 // Exponential distribution tests
147 func generateExponentialSamples(nsamples int, rate float64, seed int64) []float64 {
148 r := New(NewSource(seed))
149 samples := make([]float64, nsamples)
150 for i := range samples {
151 samples[i] = r.ExpFloat64() / rate
156 func testExponentialDistribution(t *testing.T, nsamples int, rate float64, seed int64) {
157 //fmt.Printf("testing nsamples=%v rate=%v seed=%v\n", nsamples, rate, seed);
162 samples := generateExponentialSamples(nsamples, rate, seed)
163 errorScale := max(1.0, 1/rate) // Error scales with the inverse of the rate
164 expected := &statsResults{mean, stddev, 0.10 * errorScale, 0.20 * errorScale}
166 // Make sure that the entire set matches the expected distribution.
167 checkSampleDistribution(t, samples, expected)
169 // Make sure that each half of the set matches the expected distribution.
170 checkSampleSliceDistributions(t, samples, 2, expected)
172 // Make sure that each 7th of the set matches the expected distribution.
173 checkSampleSliceDistributions(t, samples, 7, expected)
178 func TestStandardExponentialValues(t *testing.T) {
179 for _, seed := range testSeeds {
180 testExponentialDistribution(t, numTestSamples, 1, seed)
184 func TestNonStandardExponentialValues(t *testing.T) {
185 for rate := 0.05; rate < 10; rate *= 2 {
186 for _, seed := range testSeeds {
187 testExponentialDistribution(t, numTestSamples, rate, seed)
193 // Table generation tests
196 func initNorm() (testKn []uint32, testWn, testFn []float32) {
201 vn float64 = 9.91256303526217e-3
204 testKn = make([]uint32, 128)
205 testWn = make([]float32, 128)
206 testFn = make([]float32, 128)
208 q := vn / math.Exp(-0.5*dn*dn)
209 testKn[0] = uint32((dn / q) * m1)
211 testWn[0] = float32(q / m1)
212 testWn[127] = float32(dn / m1)
214 testFn[127] = float32(math.Exp(-0.5 * dn * dn))
215 for i := 126; i >= 1; i-- {
216 dn = math.Sqrt(-2.0 * math.Log(vn/dn+math.Exp(-0.5*dn*dn)))
217 testKn[i+1] = uint32((dn / tn) * m1)
219 testFn[i] = float32(math.Exp(-0.5 * dn * dn))
220 testWn[i] = float32(dn / m1)
225 func initExp() (testKe []uint32, testWe, testFe []float32) {
230 ve float64 = 3.9496598225815571993e-3
233 testKe = make([]uint32, 256)
234 testWe = make([]float32, 256)
235 testFe = make([]float32, 256)
237 q := ve / math.Exp(-de)
238 testKe[0] = uint32((de / q) * m2)
240 testWe[0] = float32(q / m2)
241 testWe[255] = float32(de / m2)
243 testFe[255] = float32(math.Exp(-de))
244 for i := 254; i >= 1; i-- {
245 de = -math.Log(ve/de + math.Exp(-de))
246 testKe[i+1] = uint32((de / te) * m2)
248 testFe[i] = float32(math.Exp(-de))
249 testWe[i] = float32(de / m2)
254 // compareUint32Slices returns the first index where the two slices
255 // disagree, or <0 if the lengths are the same and all elements
257 func compareUint32Slices(s1, s2 []uint32) int {
258 if len(s1) != len(s2) {
259 if len(s1) > len(s2) {
272 // compareFloat32Slices returns the first index where the two slices
273 // disagree, or <0 if the lengths are the same and all elements
275 func compareFloat32Slices(s1, s2 []float32) int {
276 if len(s1) != len(s2) {
277 if len(s1) > len(s2) {
283 if !nearEqual(float64(s1[i]), float64(s2[i]), 0, 1e-7) {
290 func TestNormTables(t *testing.T) {
291 testKn, testWn, testFn := initNorm()
292 if i := compareUint32Slices(kn[0:], testKn); i >= 0 {
293 t.Errorf("kn disagrees at index %v; %v != %v", i, kn[i], testKn[i])
295 if i := compareFloat32Slices(wn[0:], testWn); i >= 0 {
296 t.Errorf("wn disagrees at index %v; %v != %v", i, wn[i], testWn[i])
298 if i := compareFloat32Slices(fn[0:], testFn); i >= 0 {
299 t.Errorf("fn disagrees at index %v; %v != %v", i, fn[i], testFn[i])
303 func TestExpTables(t *testing.T) {
304 testKe, testWe, testFe := initExp()
305 if i := compareUint32Slices(ke[0:], testKe); i >= 0 {
306 t.Errorf("ke disagrees at index %v; %v != %v", i, ke[i], testKe[i])
308 if i := compareFloat32Slices(we[0:], testWe); i >= 0 {
309 t.Errorf("we disagrees at index %v; %v != %v", i, we[i], testWe[i])
311 if i := compareFloat32Slices(fe[0:], testFe); i >= 0 {
312 t.Errorf("fe disagrees at index %v; %v != %v", i, fe[i], testFe[i])
318 func BenchmarkInt63Threadsafe(b *testing.B) {
319 for n := b.N; n > 0; n-- {
324 func BenchmarkInt63Unthreadsafe(b *testing.B) {
325 r := New(NewSource(1))
326 for n := b.N; n > 0; n-- {
331 func BenchmarkIntn1000(b *testing.B) {
332 r := New(NewSource(1))
333 for n := b.N; n > 0; n-- {
338 func BenchmarkInt63n1000(b *testing.B) {
339 r := New(NewSource(1))
340 for n := b.N; n > 0; n-- {
345 func BenchmarkInt31n1000(b *testing.B) {
346 r := New(NewSource(1))
347 for n := b.N; n > 0; n-- {