]> Cypherpunks.ru repositories - gostls13.git/blob - src/math/rand/v2/rand_test.go
ab7fb56796fbe18866296090e815f616b0712de3
[gostls13.git] / src / math / rand / v2 / rand_test.go
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.
4
5 package rand_test
6
7 import (
8         "errors"
9         "fmt"
10         "internal/testenv"
11         "math"
12         . "math/rand/v2"
13         "os"
14         "runtime"
15         "sync"
16         "sync/atomic"
17         "testing"
18 )
19
20 const (
21         numTestSamples = 10000
22 )
23
24 var rn, kn, wn, fn = GetNormalDistributionParameters()
25 var re, ke, we, fe = GetExponentialDistributionParameters()
26
27 type statsResults struct {
28         mean        float64
29         stddev      float64
30         closeEnough float64
31         maxError    float64
32 }
33
34 func max(a, b float64) float64 {
35         if a > b {
36                 return a
37         }
38         return b
39 }
40
41 func nearEqual(a, b, closeEnough, maxError float64) bool {
42         absDiff := math.Abs(a - b)
43         if absDiff < closeEnough { // Necessary when one value is zero and one value is close to zero.
44                 return true
45         }
46         return absDiff/max(math.Abs(a), math.Abs(b)) < maxError
47 }
48
49 var testSeeds = []int64{1, 1754801282, 1698661970, 1550503961}
50
51 // checkSimilarDistribution returns success if the mean and stddev of the
52 // two statsResults are similar.
53 func (this *statsResults) checkSimilarDistribution(expected *statsResults) error {
54         if !nearEqual(this.mean, expected.mean, expected.closeEnough, expected.maxError) {
55                 s := fmt.Sprintf("mean %v != %v (allowed error %v, %v)", this.mean, expected.mean, expected.closeEnough, expected.maxError)
56                 fmt.Println(s)
57                 return errors.New(s)
58         }
59         if !nearEqual(this.stddev, expected.stddev, expected.closeEnough, expected.maxError) {
60                 s := fmt.Sprintf("stddev %v != %v (allowed error %v, %v)", this.stddev, expected.stddev, expected.closeEnough, expected.maxError)
61                 fmt.Println(s)
62                 return errors.New(s)
63         }
64         return nil
65 }
66
67 func getStatsResults(samples []float64) *statsResults {
68         res := new(statsResults)
69         var sum, squaresum float64
70         for _, s := range samples {
71                 sum += s
72                 squaresum += s * s
73         }
74         res.mean = sum / float64(len(samples))
75         res.stddev = math.Sqrt(squaresum/float64(len(samples)) - res.mean*res.mean)
76         return res
77 }
78
79 func checkSampleDistribution(t *testing.T, samples []float64, expected *statsResults) {
80         t.Helper()
81         actual := getStatsResults(samples)
82         err := actual.checkSimilarDistribution(expected)
83         if err != nil {
84                 t.Errorf(err.Error())
85         }
86 }
87
88 func checkSampleSliceDistributions(t *testing.T, samples []float64, nslices int, expected *statsResults) {
89         t.Helper()
90         chunk := len(samples) / nslices
91         for i := 0; i < nslices; i++ {
92                 low := i * chunk
93                 var high int
94                 if i == nslices-1 {
95                         high = len(samples) - 1
96                 } else {
97                         high = (i + 1) * chunk
98                 }
99                 checkSampleDistribution(t, samples[low:high], expected)
100         }
101 }
102
103 //
104 // Normal distribution tests
105 //
106
107 func generateNormalSamples(nsamples int, mean, stddev float64, seed int64) []float64 {
108         r := New(NewSource(seed))
109         samples := make([]float64, nsamples)
110         for i := range samples {
111                 samples[i] = r.NormFloat64()*stddev + mean
112         }
113         return samples
114 }
115
116 func testNormalDistribution(t *testing.T, nsamples int, mean, stddev float64, seed int64) {
117         //fmt.Printf("testing nsamples=%v mean=%v stddev=%v seed=%v\n", nsamples, mean, stddev, seed);
118
119         samples := generateNormalSamples(nsamples, mean, stddev, seed)
120         errorScale := max(1.0, stddev) // Error scales with stddev
121         expected := &statsResults{mean, stddev, 0.10 * errorScale, 0.08 * errorScale}
122
123         // Make sure that the entire set matches the expected distribution.
124         checkSampleDistribution(t, samples, expected)
125
126         // Make sure that each half of the set matches the expected distribution.
127         checkSampleSliceDistributions(t, samples, 2, expected)
128
129         // Make sure that each 7th of the set matches the expected distribution.
130         checkSampleSliceDistributions(t, samples, 7, expected)
131 }
132
133 // Actual tests
134
135 func TestStandardNormalValues(t *testing.T) {
136         for _, seed := range testSeeds {
137                 testNormalDistribution(t, numTestSamples, 0, 1, seed)
138         }
139 }
140
141 func TestNonStandardNormalValues(t *testing.T) {
142         sdmax := 1000.0
143         mmax := 1000.0
144         if testing.Short() {
145                 sdmax = 5
146                 mmax = 5
147         }
148         for sd := 0.5; sd < sdmax; sd *= 2 {
149                 for m := 0.5; m < mmax; m *= 2 {
150                         for _, seed := range testSeeds {
151                                 testNormalDistribution(t, numTestSamples, m, sd, seed)
152                                 if testing.Short() {
153                                         break
154                                 }
155                         }
156                 }
157         }
158 }
159
160 //
161 // Exponential distribution tests
162 //
163
164 func generateExponentialSamples(nsamples int, rate float64, seed int64) []float64 {
165         r := New(NewSource(seed))
166         samples := make([]float64, nsamples)
167         for i := range samples {
168                 samples[i] = r.ExpFloat64() / rate
169         }
170         return samples
171 }
172
173 func testExponentialDistribution(t *testing.T, nsamples int, rate float64, seed int64) {
174         //fmt.Printf("testing nsamples=%v rate=%v seed=%v\n", nsamples, rate, seed);
175
176         mean := 1 / rate
177         stddev := mean
178
179         samples := generateExponentialSamples(nsamples, rate, seed)
180         errorScale := max(1.0, 1/rate) // Error scales with the inverse of the rate
181         expected := &statsResults{mean, stddev, 0.10 * errorScale, 0.20 * errorScale}
182
183         // Make sure that the entire set matches the expected distribution.
184         checkSampleDistribution(t, samples, expected)
185
186         // Make sure that each half of the set matches the expected distribution.
187         checkSampleSliceDistributions(t, samples, 2, expected)
188
189         // Make sure that each 7th of the set matches the expected distribution.
190         checkSampleSliceDistributions(t, samples, 7, expected)
191 }
192
193 // Actual tests
194
195 func TestStandardExponentialValues(t *testing.T) {
196         for _, seed := range testSeeds {
197                 testExponentialDistribution(t, numTestSamples, 1, seed)
198         }
199 }
200
201 func TestNonStandardExponentialValues(t *testing.T) {
202         for rate := 0.05; rate < 10; rate *= 2 {
203                 for _, seed := range testSeeds {
204                         testExponentialDistribution(t, numTestSamples, rate, seed)
205                         if testing.Short() {
206                                 break
207                         }
208                 }
209         }
210 }
211
212 //
213 // Table generation tests
214 //
215
216 func initNorm() (testKn []uint32, testWn, testFn []float32) {
217         const m1 = 1 << 31
218         var (
219                 dn float64 = rn
220                 tn         = dn
221                 vn float64 = 9.91256303526217e-3
222         )
223
224         testKn = make([]uint32, 128)
225         testWn = make([]float32, 128)
226         testFn = make([]float32, 128)
227
228         q := vn / math.Exp(-0.5*dn*dn)
229         testKn[0] = uint32((dn / q) * m1)
230         testKn[1] = 0
231         testWn[0] = float32(q / m1)
232         testWn[127] = float32(dn / m1)
233         testFn[0] = 1.0
234         testFn[127] = float32(math.Exp(-0.5 * dn * dn))
235         for i := 126; i >= 1; i-- {
236                 dn = math.Sqrt(-2.0 * math.Log(vn/dn+math.Exp(-0.5*dn*dn)))
237                 testKn[i+1] = uint32((dn / tn) * m1)
238                 tn = dn
239                 testFn[i] = float32(math.Exp(-0.5 * dn * dn))
240                 testWn[i] = float32(dn / m1)
241         }
242         return
243 }
244
245 func initExp() (testKe []uint32, testWe, testFe []float32) {
246         const m2 = 1 << 32
247         var (
248                 de float64 = re
249                 te         = de
250                 ve float64 = 3.9496598225815571993e-3
251         )
252
253         testKe = make([]uint32, 256)
254         testWe = make([]float32, 256)
255         testFe = make([]float32, 256)
256
257         q := ve / math.Exp(-de)
258         testKe[0] = uint32((de / q) * m2)
259         testKe[1] = 0
260         testWe[0] = float32(q / m2)
261         testWe[255] = float32(de / m2)
262         testFe[0] = 1.0
263         testFe[255] = float32(math.Exp(-de))
264         for i := 254; i >= 1; i-- {
265                 de = -math.Log(ve/de + math.Exp(-de))
266                 testKe[i+1] = uint32((de / te) * m2)
267                 te = de
268                 testFe[i] = float32(math.Exp(-de))
269                 testWe[i] = float32(de / m2)
270         }
271         return
272 }
273
274 // compareUint32Slices returns the first index where the two slices
275 // disagree, or <0 if the lengths are the same and all elements
276 // are identical.
277 func compareUint32Slices(s1, s2 []uint32) int {
278         if len(s1) != len(s2) {
279                 if len(s1) > len(s2) {
280                         return len(s2) + 1
281                 }
282                 return len(s1) + 1
283         }
284         for i := range s1 {
285                 if s1[i] != s2[i] {
286                         return i
287                 }
288         }
289         return -1
290 }
291
292 // compareFloat32Slices returns the first index where the two slices
293 // disagree, or <0 if the lengths are the same and all elements
294 // are identical.
295 func compareFloat32Slices(s1, s2 []float32) int {
296         if len(s1) != len(s2) {
297                 if len(s1) > len(s2) {
298                         return len(s2) + 1
299                 }
300                 return len(s1) + 1
301         }
302         for i := range s1 {
303                 if !nearEqual(float64(s1[i]), float64(s2[i]), 0, 1e-7) {
304                         return i
305                 }
306         }
307         return -1
308 }
309
310 func TestNormTables(t *testing.T) {
311         testKn, testWn, testFn := initNorm()
312         if i := compareUint32Slices(kn[0:], testKn); i >= 0 {
313                 t.Errorf("kn disagrees at index %v; %v != %v", i, kn[i], testKn[i])
314         }
315         if i := compareFloat32Slices(wn[0:], testWn); i >= 0 {
316                 t.Errorf("wn disagrees at index %v; %v != %v", i, wn[i], testWn[i])
317         }
318         if i := compareFloat32Slices(fn[0:], testFn); i >= 0 {
319                 t.Errorf("fn disagrees at index %v; %v != %v", i, fn[i], testFn[i])
320         }
321 }
322
323 func TestExpTables(t *testing.T) {
324         testKe, testWe, testFe := initExp()
325         if i := compareUint32Slices(ke[0:], testKe); i >= 0 {
326                 t.Errorf("ke disagrees at index %v; %v != %v", i, ke[i], testKe[i])
327         }
328         if i := compareFloat32Slices(we[0:], testWe); i >= 0 {
329                 t.Errorf("we disagrees at index %v; %v != %v", i, we[i], testWe[i])
330         }
331         if i := compareFloat32Slices(fe[0:], testFe); i >= 0 {
332                 t.Errorf("fe disagrees at index %v; %v != %v", i, fe[i], testFe[i])
333         }
334 }
335
336 func hasSlowFloatingPoint() bool {
337         switch runtime.GOARCH {
338         case "arm":
339                 return os.Getenv("GOARM") == "5"
340         case "mips", "mipsle", "mips64", "mips64le":
341                 // Be conservative and assume that all mips boards
342                 // have emulated floating point.
343                 // TODO: detect what it actually has.
344                 return true
345         }
346         return false
347 }
348
349 func TestFloat32(t *testing.T) {
350         // For issue 6721, the problem came after 7533753 calls, so check 10e6.
351         num := int(10e6)
352         // But do the full amount only on builders (not locally).
353         // But ARM5 floating point emulation is slow (Issue 10749), so
354         // do less for that builder:
355         if testing.Short() && (testenv.Builder() == "" || hasSlowFloatingPoint()) {
356                 num /= 100 // 1.72 seconds instead of 172 seconds
357         }
358
359         r := testRand()
360         for ct := 0; ct < num; ct++ {
361                 f := r.Float32()
362                 if f >= 1 {
363                         t.Fatal("Float32() should be in range [0,1). ct:", ct, "f:", f)
364                 }
365         }
366 }
367
368 func TestShuffleSmall(t *testing.T) {
369         // Check that Shuffle allows n=0 and n=1, but that swap is never called for them.
370         r := testRand()
371         for n := 0; n <= 1; n++ {
372                 r.Shuffle(n, func(i, j int) { t.Fatalf("swap called, n=%d i=%d j=%d", n, i, j) })
373         }
374 }
375
376 // encodePerm converts from a permuted slice of length n, such as Perm generates, to an int in [0, n!).
377 // See https://en.wikipedia.org/wiki/Lehmer_code.
378 // encodePerm modifies the input slice.
379 func encodePerm(s []int) int {
380         // Convert to Lehmer code.
381         for i, x := range s {
382                 r := s[i+1:]
383                 for j, y := range r {
384                         if y > x {
385                                 r[j]--
386                         }
387                 }
388         }
389         // Convert to int in [0, n!).
390         m := 0
391         fact := 1
392         for i := len(s) - 1; i >= 0; i-- {
393                 m += s[i] * fact
394                 fact *= len(s) - i
395         }
396         return m
397 }
398
399 // TestUniformFactorial tests several ways of generating a uniform value in [0, n!).
400 func TestUniformFactorial(t *testing.T) {
401         r := New(NewSource(testSeeds[0]))
402         top := 6
403         if testing.Short() {
404                 top = 3
405         }
406         for n := 3; n <= top; n++ {
407                 t.Run(fmt.Sprintf("n=%d", n), func(t *testing.T) {
408                         // Calculate n!.
409                         nfact := 1
410                         for i := 2; i <= n; i++ {
411                                 nfact *= i
412                         }
413
414                         // Test a few different ways to generate a uniform distribution.
415                         p := make([]int, n) // re-usable slice for Shuffle generator
416                         tests := [...]struct {
417                                 name string
418                                 fn   func() int
419                         }{
420                                 {name: "Int32N", fn: func() int { return int(r.Int32N(int32(nfact))) }},
421                                 {name: "int31n", fn: func() int { return int(Int32NForTest(r, int32(nfact))) }},
422                                 {name: "Perm", fn: func() int { return encodePerm(r.Perm(n)) }},
423                                 {name: "Shuffle", fn: func() int {
424                                         // Generate permutation using Shuffle.
425                                         for i := range p {
426                                                 p[i] = i
427                                         }
428                                         r.Shuffle(n, func(i, j int) { p[i], p[j] = p[j], p[i] })
429                                         return encodePerm(p)
430                                 }},
431                         }
432
433                         for _, test := range tests {
434                                 t.Run(test.name, func(t *testing.T) {
435                                         // Gather chi-squared values and check that they follow
436                                         // the expected normal distribution given n!-1 degrees of freedom.
437                                         // See https://en.wikipedia.org/wiki/Pearson%27s_chi-squared_test and
438                                         // https://www.johndcook.com/Beautiful_Testing_ch10.pdf.
439                                         nsamples := 10 * nfact
440                                         if nsamples < 200 {
441                                                 nsamples = 200
442                                         }
443                                         samples := make([]float64, nsamples)
444                                         for i := range samples {
445                                                 // Generate some uniformly distributed values and count their occurrences.
446                                                 const iters = 1000
447                                                 counts := make([]int, nfact)
448                                                 for i := 0; i < iters; i++ {
449                                                         counts[test.fn()]++
450                                                 }
451                                                 // Calculate chi-squared and add to samples.
452                                                 want := iters / float64(nfact)
453                                                 var Ï‡2 float64
454                                                 for _, have := range counts {
455                                                         err := float64(have) - want
456                                                         Ï‡2 += err * err
457                                                 }
458                                                 Ï‡2 /= want
459                                                 samples[i] = Ï‡2
460                                         }
461
462                                         // Check that our samples approximate the appropriate normal distribution.
463                                         dof := float64(nfact - 1)
464                                         expected := &statsResults{mean: dof, stddev: math.Sqrt(2 * dof)}
465                                         errorScale := max(1.0, expected.stddev)
466                                         expected.closeEnough = 0.10 * errorScale
467                                         expected.maxError = 0.08 // TODO: What is the right value here? See issue 21211.
468                                         checkSampleDistribution(t, samples, expected)
469                                 })
470                         }
471                 })
472         }
473 }
474
475 // Benchmarks
476
477 var Sink uint64
478
479 func testRand() *Rand {
480         return New(NewSource(1))
481 }
482
483 func BenchmarkSourceUint64(b *testing.B) {
484         s := NewSource(1).(Source64)
485         var t uint64
486         for n := b.N; n > 0; n-- {
487                 t += s.Uint64()
488         }
489         Sink = uint64(t)
490 }
491
492 func BenchmarkGlobalInt64(b *testing.B) {
493         var t int64
494         for n := b.N; n > 0; n-- {
495                 t += Int64()
496         }
497         Sink = uint64(t)
498 }
499
500 func BenchmarkGlobalInt63Parallel(b *testing.B) {
501         b.RunParallel(func(pb *testing.PB) {
502                 var t int64
503                 for pb.Next() {
504                         t += Int64()
505                 }
506                 atomic.AddUint64(&Sink, uint64(t))
507         })
508 }
509
510 func BenchmarkGlobalUint64(b *testing.B) {
511         var t uint64
512         for n := b.N; n > 0; n-- {
513                 t += Uint64()
514         }
515         Sink = t
516 }
517
518 func BenchmarkGlobalUint64Parallel(b *testing.B) {
519         b.RunParallel(func(pb *testing.PB) {
520                 var t uint64
521                 for pb.Next() {
522                         t += Uint64()
523                 }
524                 atomic.AddUint64(&Sink, t)
525         })
526 }
527
528 func BenchmarkInt64(b *testing.B) {
529         r := testRand()
530         var t int64
531         for n := b.N; n > 0; n-- {
532                 t += r.Int64()
533         }
534         Sink = uint64(t)
535 }
536
537 var AlwaysFalse = false
538
539 func keep[T int | uint | int32 | uint32 | int64 | uint64](x T) T {
540         if AlwaysFalse {
541                 return -x
542         }
543         return x
544 }
545
546 func BenchmarkUint64(b *testing.B) {
547         r := testRand()
548         var t uint64
549         for n := b.N; n > 0; n-- {
550                 t += r.Uint64()
551         }
552         Sink = t
553 }
554
555 func BenchmarkGlobalIntN1000(b *testing.B) {
556         var t int
557         arg := keep(1000)
558         for n := b.N; n > 0; n-- {
559                 t += IntN(arg)
560         }
561         Sink = uint64(t)
562 }
563
564 func BenchmarkIntN1000(b *testing.B) {
565         r := testRand()
566         var t int
567         arg := keep(1000)
568         for n := b.N; n > 0; n-- {
569                 t += r.IntN(arg)
570         }
571         Sink = uint64(t)
572 }
573
574 func BenchmarkInt64N1000(b *testing.B) {
575         r := testRand()
576         var t int64
577         arg := keep(int64(1000))
578         for n := b.N; n > 0; n-- {
579                 t += r.Int64N(arg)
580         }
581         Sink = uint64(t)
582 }
583
584 func BenchmarkInt64N1e8(b *testing.B) {
585         r := testRand()
586         var t int64
587         arg := keep(int64(1e8))
588         for n := b.N; n > 0; n-- {
589                 t += r.Int64N(arg)
590         }
591         Sink = uint64(t)
592 }
593
594 func BenchmarkInt64N1e9(b *testing.B) {
595         r := testRand()
596         var t int64
597         arg := keep(int64(1e9))
598         for n := b.N; n > 0; n-- {
599                 t += r.Int64N(arg)
600         }
601         Sink = uint64(t)
602 }
603
604 func BenchmarkInt64N2e9(b *testing.B) {
605         r := testRand()
606         var t int64
607         arg := keep(int64(2e9))
608         for n := b.N; n > 0; n-- {
609                 t += r.Int64N(arg)
610         }
611         Sink = uint64(t)
612 }
613
614 func BenchmarkInt64N1e18(b *testing.B) {
615         r := testRand()
616         var t int64
617         arg := keep(int64(1e18))
618         for n := b.N; n > 0; n-- {
619                 t += r.Int64N(arg)
620         }
621         Sink = uint64(t)
622 }
623
624 func BenchmarkInt64N2e18(b *testing.B) {
625         r := testRand()
626         var t int64
627         arg := keep(int64(2e18))
628         for n := b.N; n > 0; n-- {
629                 t += r.Int64N(arg)
630         }
631         Sink = uint64(t)
632 }
633
634 func BenchmarkInt64N4e18(b *testing.B) {
635         r := testRand()
636         var t int64
637         arg := keep(int64(4e18))
638         for n := b.N; n > 0; n-- {
639                 t += r.Int64N(arg)
640         }
641         Sink = uint64(t)
642 }
643
644 func BenchmarkInt32N1000(b *testing.B) {
645         r := testRand()
646         var t int32
647         arg := keep(int32(1000))
648         for n := b.N; n > 0; n-- {
649                 t += r.Int32N(arg)
650         }
651         Sink = uint64(t)
652 }
653
654 func BenchmarkInt32N1e8(b *testing.B) {
655         r := testRand()
656         var t int32
657         arg := keep(int32(1e8))
658         for n := b.N; n > 0; n-- {
659                 t += r.Int32N(arg)
660         }
661         Sink = uint64(t)
662 }
663
664 func BenchmarkInt32N1e9(b *testing.B) {
665         r := testRand()
666         var t int32
667         arg := keep(int32(1e9))
668         for n := b.N; n > 0; n-- {
669                 t += r.Int32N(arg)
670         }
671         Sink = uint64(t)
672 }
673
674 func BenchmarkInt32N2e9(b *testing.B) {
675         r := testRand()
676         var t int32
677         arg := keep(int32(2e9))
678         for n := b.N; n > 0; n-- {
679                 t += r.Int32N(arg)
680         }
681         Sink = uint64(t)
682 }
683
684 func BenchmarkFloat32(b *testing.B) {
685         r := testRand()
686         var t float32
687         for n := b.N; n > 0; n-- {
688                 t += r.Float32()
689         }
690         Sink = uint64(t)
691 }
692
693 func BenchmarkFloat64(b *testing.B) {
694         r := testRand()
695         var t float64
696         for n := b.N; n > 0; n-- {
697                 t += r.Float64()
698         }
699         Sink = uint64(t)
700 }
701
702 func BenchmarkExpFloat64(b *testing.B) {
703         r := testRand()
704         var t float64
705         for n := b.N; n > 0; n-- {
706                 t += r.ExpFloat64()
707         }
708         Sink = uint64(t)
709 }
710
711 func BenchmarkNormFloat64(b *testing.B) {
712         r := testRand()
713         var t float64
714         for n := b.N; n > 0; n-- {
715                 t += r.NormFloat64()
716         }
717         Sink = uint64(t)
718 }
719
720 func BenchmarkPerm3(b *testing.B) {
721         r := testRand()
722         var t int
723         for n := b.N; n > 0; n-- {
724                 t += r.Perm(3)[0]
725         }
726         Sink = uint64(t)
727
728 }
729
730 func BenchmarkPerm30(b *testing.B) {
731         r := testRand()
732         var t int
733         for n := b.N; n > 0; n-- {
734                 t += r.Perm(30)[0]
735         }
736         Sink = uint64(t)
737 }
738
739 func BenchmarkPerm30ViaShuffle(b *testing.B) {
740         r := testRand()
741         var t int
742         for n := b.N; n > 0; n-- {
743                 p := make([]int, 30)
744                 for i := range p {
745                         p[i] = i
746                 }
747                 r.Shuffle(30, func(i, j int) { p[i], p[j] = p[j], p[i] })
748                 t += p[0]
749         }
750         Sink = uint64(t)
751 }
752
753 // BenchmarkShuffleOverhead uses a minimal swap function
754 // to measure just the shuffling overhead.
755 func BenchmarkShuffleOverhead(b *testing.B) {
756         r := testRand()
757         for n := b.N; n > 0; n-- {
758                 r.Shuffle(30, func(i, j int) {
759                         if i < 0 || i >= 30 || j < 0 || j >= 30 {
760                                 b.Fatalf("bad swap(%d, %d)", i, j)
761                         }
762                 })
763         }
764 }
765
766 func BenchmarkConcurrent(b *testing.B) {
767         const goroutines = 4
768         var wg sync.WaitGroup
769         wg.Add(goroutines)
770         for i := 0; i < goroutines; i++ {
771                 go func() {
772                         defer wg.Done()
773                         for n := b.N; n > 0; n-- {
774                                 Int64()
775                         }
776                 }()
777         }
778         wg.Wait()
779 }