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