| 1 | // Copyright 2021 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 generator |
| 6 | |
| 7 | import ( |
| 8 | "fmt" |
| 9 | "math/rand/v2" |
| 10 | "os" |
| 11 | "runtime" |
| 12 | "strings" |
| 13 | ) |
| 14 | |
| 15 | const ( |
| 16 | RandCtlNochecks = 0 |
| 17 | RandCtlChecks = 1 << iota |
| 18 | RandCtlCapture |
| 19 | RandCtlPanic |
| 20 | ) |
| 21 | |
| 22 | func NewWrapRand(seed int64, ctl int) *wraprand { |
| 23 | return &wraprand{seed: seed, ctl: ctl, rand: rand.New(rand.NewPCG(0, uint64(seed)))} |
| 24 | } |
| 25 | |
| 26 | type wraprand struct { |
| 27 | f32calls int |
| 28 | f64calls int |
| 29 | intncalls int |
| 30 | seed int64 |
| 31 | tag string |
| 32 | calls []string |
| 33 | ctl int |
| 34 | rand *rand.Rand |
| 35 | } |
| 36 | |
| 37 | func (w *wraprand) captureCall(tag string, val string) { |
| 38 | var call strings.Builder |
| 39 | fmt.Fprintf(&call, "%s: %s\n", tag, val) |
| 40 | pc := make([]uintptr, 10) |
| 41 | n := runtime.Callers(1, pc) |
| 42 | if n == 0 { |
| 43 | panic("why?") |
| 44 | } |
| 45 | pc = pc[:n] // pass only valid pcs to runtime.CallersFrames |
| 46 | frames := runtime.CallersFrames(pc) |
| 47 | for { |
| 48 | frame, more := frames.Next() |
| 49 | if strings.Contains(frame.File, "testing.") { |
| 50 | break |
| 51 | } |
| 52 | fmt.Fprintf(&call, "%s %s:%d\n", frame.Function, frame.File, frame.Line) |
| 53 | if !more { |
| 54 | break |
| 55 | } |
| 56 | |
| 57 | } |
| 58 | w.calls = append(w.calls, call.String()) |
| 59 | } |
| 60 | |
| 61 | func (w *wraprand) Intn(n int64) int64 { |
| 62 | w.intncalls++ |
| 63 | rv := w.rand.Int64N(n) |
| 64 | if w.ctl&RandCtlCapture != 0 { |
| 65 | w.captureCall("Intn", fmt.Sprintf("%d", rv)) |
| 66 | } |
| 67 | return rv |
| 68 | } |
| 69 | |
| 70 | func (w *wraprand) Float32() float32 { |
| 71 | w.f32calls++ |
| 72 | rv := w.rand.Float32() |
| 73 | if w.ctl&RandCtlCapture != 0 { |
| 74 | w.captureCall("Float32", fmt.Sprintf("%f", rv)) |
| 75 | } |
| 76 | return rv |
| 77 | } |
| 78 | |
| 79 | func (w *wraprand) NormFloat64() float64 { |
| 80 | w.f64calls++ |
| 81 | rv := w.rand.NormFloat64() |
| 82 | if w.ctl&RandCtlCapture != 0 { |
| 83 | w.captureCall("NormFloat64", fmt.Sprintf("%f", rv)) |
| 84 | } |
| 85 | return rv |
| 86 | } |
| 87 | |
| 88 | func (w *wraprand) emitCalls(fn string) { |
| 89 | outf, err := os.OpenFile(fn, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o666) |
| 90 | if err != nil { |
| 91 | panic(err) |
| 92 | } |
| 93 | for _, c := range w.calls { |
| 94 | fmt.Fprint(outf, c) |
| 95 | } |
| 96 | outf.Close() |
| 97 | } |
| 98 | |
| 99 | func (w *wraprand) Equal(w2 *wraprand) bool { |
| 100 | return w.f32calls == w2.f32calls && |
| 101 | w.f64calls == w2.f64calls && |
| 102 | w.intncalls == w2.intncalls |
| 103 | } |
| 104 | |
| 105 | func (w *wraprand) Check(w2 *wraprand) { |
| 106 | if w.ctl != 0 && !w.Equal(w2) { |
| 107 | fmt.Fprintf(os.Stderr, "wraprand consistency check failed:\n") |
| 108 | t := "w" |
| 109 | if w.tag != "" { |
| 110 | t = w.tag |
| 111 | } |
| 112 | t2 := "w2" |
| 113 | if w2.tag != "" { |
| 114 | t2 = w2.tag |
| 115 | } |
| 116 | fmt.Fprintf(os.Stderr, " %s: {f32:%d f64:%d i:%d}\n", t, |
| 117 | w.f32calls, w.f64calls, w.intncalls) |
| 118 | fmt.Fprintf(os.Stderr, " %s: {f32:%d f64:%d i:%d}\n", t2, |
| 119 | w2.f32calls, w2.f64calls, w2.intncalls) |
| 120 | if w.ctl&RandCtlCapture != 0 { |
| 121 | f := fmt.Sprintf("/tmp/%s.txt", t) |
| 122 | f2 := fmt.Sprintf("/tmp/%s.txt", t2) |
| 123 | w.emitCalls(f) |
| 124 | w2.emitCalls(f2) |
| 125 | fmt.Fprintf(os.Stderr, "=-= emitted calls to %s, %s\n", f, f2) |
| 126 | } |
| 127 | if w.ctl&RandCtlPanic != 0 { |
| 128 | panic("bad") |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | func (w *wraprand) Checkpoint(tag string) { |
| 134 | if w.ctl&RandCtlCapture != 0 { |
| 135 | w.calls = append(w.calls, "=-=\n"+tag+"\n=-=\n") |
| 136 | } |
| 137 | } |