Source file
src/math/rand/v2/regress_test.go
1
2
3
4
5
6
7
8
9
10
11
12 package rand_test
13
14 import (
15 "bytes"
16 "flag"
17 "fmt"
18 "go/format"
19 "io"
20 . "math/rand/v2"
21 "os"
22 "reflect"
23 "strings"
24 "testing"
25 )
26
27 var update = flag.Bool("update", false, "update golden results for regression test")
28
29 func TestRegress(t *testing.T) {
30 var int32s = []int32{1, 10, 32, 1 << 20, 1<<20 + 1, 1000000000, 1 << 30, 1<<31 - 2, 1<<31 - 1}
31 var uint32s = []uint32{1, 10, 32, 1 << 20, 1<<20 + 1, 1000000000, 1 << 30, 1<<31 - 2, 1<<31 - 1, 1<<32 - 2, 1<<32 - 1}
32 var int64s = []int64{1, 10, 32, 1 << 20, 1<<20 + 1, 1000000000, 1 << 30, 1<<31 - 2, 1<<31 - 1, 1000000000000000000, 1 << 60, 1<<63 - 2, 1<<63 - 1}
33 var uint64s = []uint64{1, 10, 32, 1 << 20, 1<<20 + 1, 1000000000, 1 << 30, 1<<31 - 2, 1<<31 - 1, 1000000000000000000, 1 << 60, 1<<63 - 2, 1<<63 - 1, 1<<64 - 2, 1<<64 - 1}
34 var permSizes = []int{0, 1, 5, 8, 9, 10, 16}
35
36 n := reflect.TypeOf(New(NewPCG(1, 2))).NumMethod()
37 p := 0
38 var buf bytes.Buffer
39 if *update {
40 fmt.Fprintf(&buf, "var regressGolden = []any{\n")
41 }
42 for i := 0; i < n; i++ {
43 if *update && i > 0 {
44 fmt.Fprintf(&buf, "\n")
45 }
46 r := New(NewPCG(1, 2))
47 rv := reflect.ValueOf(r)
48 m := rv.Type().Method(i)
49 mv := rv.Method(i)
50 mt := mv.Type()
51 if mt.NumOut() == 0 {
52 continue
53 }
54 for repeat := 0; repeat < 20; repeat++ {
55 var args []reflect.Value
56 var argstr string
57 if mt.NumIn() == 1 {
58 var x any
59 switch mt.In(0).Kind() {
60 default:
61 t.Fatalf("unexpected argument type for r.%s", m.Name)
62
63 case reflect.Int:
64 if m.Name == "Perm" {
65 x = permSizes[repeat%len(permSizes)]
66 break
67 }
68 big := int64s[repeat%len(int64s)]
69 if int64(int(big)) != big {
70
71
72
73 r.Int64N(big)
74 if *update {
75 t.Fatalf("must run -update on 64-bit machine")
76 }
77 p++
78 continue
79 }
80 x = int(big)
81
82 case reflect.Uint:
83 if m.Name == "Uint" {
84 continue
85 }
86 big := uint64s[repeat%len(uint64s)]
87 if uint64(uint(big)) != big {
88 r.Uint64N(big)
89 if *update {
90 t.Fatalf("must run -update on 64-bit machine")
91 }
92 p++
93 continue
94 }
95 x = uint(big)
96
97 case reflect.Int32:
98 x = int32s[repeat%len(int32s)]
99
100 case reflect.Int64:
101 x = int64s[repeat%len(int64s)]
102
103 case reflect.Uint32:
104 x = uint32s[repeat%len(uint32s)]
105
106 case reflect.Uint64:
107 x = uint64s[repeat%len(uint64s)]
108 }
109 argstr = fmt.Sprint(x)
110 args = append(args, reflect.ValueOf(x))
111 }
112
113 var out any
114 out = mv.Call(args)[0].Interface()
115 if m.Name == "Int" || m.Name == "IntN" {
116 out = int64(out.(int))
117 }
118 if m.Name == "Uint" || m.Name == "UintN" {
119 out = uint64(out.(uint))
120 }
121 if *update {
122 var val string
123 big := int64(1 << 60)
124 if int64(int(big)) != big && (m.Name == "Int" || m.Name == "IntN" || m.Name == "Uint" || m.Name == "UintN") {
125
126 val = "truncated"
127 } else if reflect.TypeOf(out).Kind() == reflect.Slice {
128 val = fmt.Sprintf("%#v", out)
129 } else {
130 val = fmt.Sprintf("%T(%v)", out, out)
131 }
132 fmt.Fprintf(&buf, "\t%s, // %s(%s)\n", val, m.Name, argstr)
133 } else if p >= len(regressGolden) {
134 t.Errorf("r.%s(%s) = %v, missing golden value", m.Name, argstr, out)
135 } else {
136 want := regressGolden[p]
137 if m.Name == "Int" {
138 want = int64(int(uint(want.(int64)) << 1 >> 1))
139 }
140 if m.Name == "Uint" {
141 want = uint64(uint(want.(uint64)))
142 }
143 if !reflect.DeepEqual(out, want) {
144 t.Errorf("r.%s(%s) = %v, want %v", m.Name, argstr, out, want)
145 }
146 }
147 p++
148 }
149 }
150 if *update {
151 replace(t, "regress_test.go", buf.Bytes())
152 }
153 }
154
155 func TestUpdateExample(t *testing.T) {
156 if !*update {
157 t.Skip("-update not given")
158 }
159
160 oldStdout := os.Stdout
161 defer func() {
162 os.Stdout = oldStdout
163 }()
164
165 r, w, err := os.Pipe()
166 if err != nil {
167 t.Fatal(err)
168 }
169 defer r.Close()
170 defer w.Close()
171
172 go func() {
173 os.Stdout = w
174 Example_rand()
175 os.Stdout = oldStdout
176 w.Close()
177 }()
178 out, err := io.ReadAll(r)
179 if err != nil {
180 t.Fatal(err)
181 }
182
183 var buf bytes.Buffer
184 fmt.Fprintf(&buf, "\t// Output:\n")
185 for _, line := range strings.Split(string(out), "\n") {
186 if line != "" {
187 fmt.Fprintf(&buf, "\t// %s\n", line)
188 }
189 }
190
191 replace(t, "example_test.go", buf.Bytes())
192
193
194 fmt.Printf("UPDATED; ignore non-zero exit status\n")
195 os.Exit(1)
196 }
197
198
199
200
201
202
203
204
205
206
207
208 func replace(t *testing.T, file string, new []byte) {
209 first, _, _ := bytes.Cut(new, []byte("\n"))
210 first = append(append([]byte("\n"), first...), '\n')
211 data, err := os.ReadFile(file)
212 if err != nil {
213 t.Fatal(err)
214 }
215 i := bytes.Index(data, first)
216 if i < 0 {
217 t.Fatalf("cannot find %q in %s", first, file)
218 }
219 j := bytes.Index(data[i+1:], []byte("\n}\n"))
220 if j < 0 {
221 t.Fatalf("cannot find end in %s", file)
222 }
223 data = append(append(data[:i+1:i+1], new...), data[i+1+j+1:]...)
224 data, err = format.Source(data)
225 if err != nil {
226 t.Fatal(err)
227 }
228 if err := os.WriteFile(file, data, 0666); err != nil {
229 t.Fatal(err)
230 }
231 }
232
233 var regressGolden = []any{
234 float64(0.5931317151369719),
235 float64(0.0680034588807843),
236 float64(0.036496967459790364),
237 float64(2.460335459645379),
238 float64(1.5792300208419903),
239 float64(0.9149501499404387),
240 float64(0.43463410545541104),
241 float64(0.5513632046504593),
242 float64(0.7426404617374481),
243 float64(1.2334925132631804),
244 float64(0.892529142200442),
245 float64(0.21508763681487764),
246 float64(1.0208588200798545),
247 float64(0.7650739736831382),
248 float64(0.7772788529257701),
249 float64(1.102732861281323),
250 float64(0.6982243043885805),
251 float64(0.4981788638202421),
252 float64(0.15806532306947937),
253 float64(0.9419163802459202),
254
255 float32(0.95955694),
256 float32(0.8076733),
257 float32(0.8135684),
258 float32(0.92872405),
259 float32(0.97472525),
260 float32(0.5485458),
261 float32(0.97740936),
262 float32(0.042272687),
263 float32(0.99663067),
264 float32(0.035181105),
265 float32(0.45059562),
266 float32(0.86597633),
267 float32(0.8954844),
268 float32(0.090798736),
269 float32(0.46218646),
270 float32(0.5955118),
271 float32(0.08985227),
272 float32(0.19820237),
273 float32(0.7443699),
274 float32(0.56461),
275
276 float64(0.6764556596678251),
277 float64(0.4613862177205994),
278 float64(0.5085473976760264),
279 float64(0.4297927436037299),
280 float64(0.797802349388613),
281 float64(0.3883664855410056),
282 float64(0.8192750264193612),
283 float64(0.3381816951746133),
284 float64(0.9730458047755973),
285 float64(0.281449117585586),
286 float64(0.6047654075331631),
287 float64(0.9278107175107462),
288 float64(0.16387541502137226),
289 float64(0.7263900707339023),
290 float64(0.6974917552729882),
291 float64(0.7640946923790318),
292 float64(0.7188183661358182),
293 float64(0.5856191500346635),
294 float64(0.9549597149363428),
295 float64(0.5168804691962643),
296
297 int64(4969059760275911952),
298 int64(2147869220224756844),
299 int64(5246770554000605320),
300 int64(5471241176507662746),
301 int64(4321634407747778896),
302 int64(760102831717374652),
303 int64(9221744211007427193),
304 int64(8289669384274456462),
305 int64(2449715415482412441),
306 int64(3389241988064777392),
307 int64(2986830195847294191),
308 int64(8204908297817606218),
309 int64(8134976985547166651),
310 int64(2240328155279531677),
311 int64(7311121042813227358),
312 int64(5231057920893523323),
313 int64(4257872588489500903),
314 int64(158397175702351138),
315 int64(1350674201389090105),
316 int64(6093522341581845358),
317
318 int32(1652216515),
319 int32(1323786710),
320 int32(1684546306),
321 int32(1710678126),
322 int32(503104460),
323 int32(88487615),
324 int32(1073552320),
325 int32(965044529),
326 int32(285184408),
327 int32(394559696),
328 int32(1421454622),
329 int32(955177040),
330 int32(2020777787),
331 int32(260808523),
332 int32(851126509),
333 int32(1682717115),
334 int32(1569423431),
335 int32(1092181682),
336 int32(157239171),
337 int32(709379364),
338
339 int32(0),
340 int32(6),
341 int32(8),
342 int32(704922),
343 int32(245656),
344 int32(41205257),
345 int32(43831929),
346 int32(965044528),
347 int32(285184408),
348 int32(0),
349 int32(6),
350 int32(10),
351 int32(283579),
352 int32(127348),
353 int32(396336665),
354 int32(911873403),
355 int32(1569423430),
356 int32(1092181681),
357 int32(0),
358 int32(3),
359
360 int64(4969059760275911952),
361 int64(2147869220224756844),
362 int64(5246770554000605320),
363 int64(5471241176507662746),
364 int64(4321634407747778896),
365 int64(760102831717374652),
366 int64(9221744211007427193),
367 int64(8289669384274456462),
368 int64(2449715415482412441),
369 int64(3389241988064777392),
370 int64(2986830195847294191),
371 int64(8204908297817606218),
372 int64(8134976985547166651),
373 int64(2240328155279531677),
374 int64(7311121042813227358),
375 int64(5231057920893523323),
376 int64(4257872588489500903),
377 int64(158397175702351138),
378 int64(1350674201389090105),
379 int64(6093522341581845358),
380
381 int64(0),
382 int64(6),
383 int64(8),
384 int64(704922),
385 int64(245656),
386 int64(41205257),
387 int64(43831929),
388 int64(965044528),
389 int64(285184408),
390 int64(183731176326946086),
391 int64(680987186633600239),
392 int64(4102454148908803108),
393 int64(8679174511200971228),
394 int64(0),
395 int64(3),
396 int64(27),
397 int64(665831),
398 int64(533292),
399 int64(73220195),
400 int64(686060398),
401
402 int64(0),
403 int64(6),
404 int64(8),
405 int64(704922),
406 int64(245656),
407 int64(41205257),
408 int64(43831929),
409 int64(965044528),
410 int64(285184408),
411 int64(183731176326946086),
412 int64(680987186633600239),
413 int64(4102454148908803108),
414 int64(8679174511200971228),
415 int64(0),
416 int64(3),
417 int64(27),
418 int64(665831),
419 int64(533292),
420 int64(73220195),
421 int64(686060398),
422
423 float64(0.37944549835531083),
424 float64(0.07473804659119399),
425 float64(0.20006841200604142),
426 float64(-1.1253144115495104),
427 float64(-0.4005883316435388),
428 float64(-3.0853771402394736),
429 float64(1.932330243076978),
430 float64(1.726131393719264),
431 float64(-0.11707238034168332),
432 float64(-0.9303318111676635),
433 float64(-0.04750789419852852),
434 float64(0.22248301107582735),
435 float64(-1.83630520614272),
436 float64(0.7259521217919809),
437 float64(0.8806882871913041),
438 float64(-1.5022903484270484),
439 float64(0.5972577266810571),
440 float64(1.5631937339973658),
441 float64(-0.3841235370075905),
442 float64(-0.2967295854430667),
443
444 []int{},
445 []int{0},
446 []int{1, 4, 2, 0, 3},
447 []int{4, 3, 6, 1, 5, 2, 7, 0},
448 []int{6, 5, 1, 8, 7, 2, 0, 3, 4},
449 []int{9, 4, 2, 5, 6, 8, 1, 7, 0, 3},
450 []int{5, 9, 3, 1, 4, 2, 10, 7, 15, 11, 0, 14, 13, 8, 6, 12},
451 []int{},
452 []int{0},
453 []int{4, 2, 1, 3, 0},
454 []int{0, 2, 3, 1, 5, 4, 6, 7},
455 []int{2, 0, 8, 3, 4, 7, 6, 5, 1},
456 []int{0, 6, 5, 3, 8, 4, 1, 2, 9, 7},
457 []int{9, 14, 4, 11, 13, 8, 0, 6, 2, 12, 3, 7, 1, 10, 5, 15},
458 []int{},
459 []int{0},
460 []int{2, 4, 0, 3, 1},
461 []int{3, 2, 1, 0, 7, 5, 4, 6},
462 []int{1, 3, 4, 5, 0, 2, 7, 8, 6},
463 []int{1, 8, 4, 7, 2, 6, 5, 9, 0, 3},
464
465 uint64(14192431797130687760),
466 uint64(11371241257079532652),
467 uint64(14470142590855381128),
468 uint64(14694613213362438554),
469 uint64(4321634407747778896),
470 uint64(760102831717374652),
471 uint64(9221744211007427193),
472 uint64(8289669384274456462),
473 uint64(2449715415482412441),
474 uint64(3389241988064777392),
475 uint64(12210202232702069999),
476 uint64(8204908297817606218),
477 uint64(17358349022401942459),
478 uint64(2240328155279531677),
479 uint64(7311121042813227358),
480 uint64(14454429957748299131),
481 uint64(13481244625344276711),
482 uint64(9381769212557126946),
483 uint64(1350674201389090105),
484 uint64(6093522341581845358),
485
486 uint32(3304433030),
487 uint32(2647573421),
488 uint32(3369092613),
489 uint32(3421356252),
490 uint32(1006208920),
491 uint32(176975231),
492 uint32(2147104640),
493 uint32(1930089058),
494 uint32(570368816),
495 uint32(789119393),
496 uint32(2842909244),
497 uint32(1910354080),
498 uint32(4041555575),
499 uint32(521617046),
500 uint32(1702253018),
501 uint32(3365434230),
502 uint32(3138846863),
503 uint32(2184363364),
504 uint32(314478343),
505 uint32(1418758728),
506
507 uint32(0),
508 uint32(6),
509 uint32(8),
510 uint32(704922),
511 uint32(245656),
512 uint32(41205257),
513 uint32(43831929),
514 uint32(965044528),
515 uint32(285184408),
516 uint32(789119393),
517 uint32(2842909244),
518 uint32(0),
519 uint32(9),
520 uint32(29),
521 uint32(266590),
522 uint32(821640),
523 uint32(730819735),
524 uint32(522841378),
525 uint32(157239171),
526 uint32(709379364),
527
528 uint64(14192431797130687760),
529 uint64(11371241257079532652),
530 uint64(14470142590855381128),
531 uint64(14694613213362438554),
532 uint64(4321634407747778896),
533 uint64(760102831717374652),
534 uint64(9221744211007427193),
535 uint64(8289669384274456462),
536 uint64(2449715415482412441),
537 uint64(3389241988064777392),
538 uint64(12210202232702069999),
539 uint64(8204908297817606218),
540 uint64(17358349022401942459),
541 uint64(2240328155279531677),
542 uint64(7311121042813227358),
543 uint64(14454429957748299131),
544 uint64(13481244625344276711),
545 uint64(9381769212557126946),
546 uint64(1350674201389090105),
547 uint64(6093522341581845358),
548
549 uint64(0),
550 uint64(6),
551 uint64(8),
552 uint64(704922),
553 uint64(245656),
554 uint64(41205257),
555 uint64(43831929),
556 uint64(965044528),
557 uint64(285184408),
558 uint64(183731176326946086),
559 uint64(680987186633600239),
560 uint64(4102454148908803108),
561 uint64(8679174511200971228),
562 uint64(2240328155279531676),
563 uint64(7311121042813227357),
564 uint64(0),
565 uint64(7),
566 uint64(2),
567 uint64(312633),
568 uint64(346376),
569
570 uint64(0),
571 uint64(6),
572 uint64(8),
573 uint64(704922),
574 uint64(245656),
575 uint64(41205257),
576 uint64(43831929),
577 uint64(965044528),
578 uint64(285184408),
579 uint64(183731176326946086),
580 uint64(680987186633600239),
581 uint64(4102454148908803108),
582 uint64(8679174511200971228),
583 uint64(2240328155279531676),
584 uint64(7311121042813227357),
585 uint64(0),
586 uint64(7),
587 uint64(2),
588 uint64(312633),
589 uint64(346376),
590 }
591
View as plain text