History

Use single global for flags

Gregory Petrosyan committed 6 years ago

Commit e5424da1 parent 8f0b7cb5 files · +38 −23Browse files

Unified · Split

5 files
  1. combinators.go +1 −1
  2. data.go +2 −2
  3. engine.go +28 −13
  4. shrink.go +6 −6
  5. statemachine.go +1 −1
combinators.go+1−1
@@ -50,7 +50,7 @@ func (g *customGen) value(t *T) value { · 49 unmodified lines
5050 }
5151
5252 func (g *customGen) maybeValue(t *T) value {
53− t = newT(t.tb, t.s, *debug, nil)
53+ t = newT(t.tb, t.s, flags.debug, nil)
5454
5555 defer func() {
5656 if r := recover(); r != nil {
data.go+2−2
@@ -22,8 +22,8 @@ type bitStream interface { · 21 unmodified lines
2222 }
2323
2424 func baseSeed() uint64 {
25− if *startSeed != 0 {
26− return *startSeed
25+ if flags.startSeed != 0 {
26+ return flags.startSeed
2727 }
2828
2929 return uint64(time.Now().UnixNano()) + uint64(atomic.AddUint32(&seedCounter, 1))
engine.go+28−13
@@ -31,14 +31,7 @@ const ( · 30 unmodified lines
3131 )
3232
3333 var (
34− checks = flag.Int("rapid.checks", 100, "rapid: number of checks to perform")
35− steps = flag.Int("rapid.steps", 100, "rapid: number of state machine steps to perform")
36− startSeed = flag.Uint64("rapid.seed", 0, "rapid: PRNG seed to start with (0 to use a random one)")
37− rapidLog = flag.Bool("rapid.log", false, "rapid: eager verbose output to stdout (to aid with unrecoverable test failures)")
38− verbose = flag.Bool("rapid.v", false, "rapid: verbose output")
39− debug = flag.Bool("rapid.debug", false, "rapid: debugging output")
40− debugvis = flag.Bool("rapid.debugvis", false, "rapid: debugging visualization")
41− shrinkTime = flag.Duration("rapid.shrinktime", 30*time.Second, "rapid: maximum time to spend on test case minimization")
34+ flags cmdline
4235
4336 emptyStructType = reflect.TypeOf(struct{}{})
4437 emptyStructValue = reflect.ValueOf(struct{}{})
@@ -49,6 +42,28 @@ var ( · 4 unmodified lines
4942 }
5043 )
5144
45+type cmdline struct {
46+ checks int
47+ steps int
48+ startSeed uint64
49+ rapidLog bool
50+ verbose bool
51+ debug bool
52+ debugvis bool
53+ shrinkTime time.Duration
54+}
55+
56+func init() {
57+ flag.IntVar(&flags.checks, "rapid.checks", 100, "rapid: number of checks to perform")
58+ flag.IntVar(&flags.steps, "rapid.steps", 100, "rapid: number of state machine steps to perform")
59+ flag.Uint64Var(&flags.startSeed, "rapid.seed", 0, "rapid: PRNG seed to start with (0 to use a random one)")
60+ flag.BoolVar(&flags.rapidLog, "rapid.log", false, "rapid: eager verbose output to stdout (to aid with unrecoverable test failures)")
61+ flag.BoolVar(&flags.verbose, "rapid.v", false, "rapid: verbose output")
62+ flag.BoolVar(&flags.debug, "rapid.debug", false, "rapid: debugging output")
63+ flag.BoolVar(&flags.debugvis, "rapid.debugvis", false, "rapid: debugging visualization")
64+ flag.DurationVar(&flags.shrinkTime, "rapid.shrinktime", 30*time.Second, "rapid: maximum time to spend on test case minimization")
65+}
66+
5267 func assert(ok bool) {
5368 if !ok {
5469 panic("assertion failed")
@@ -104,7 +119,7 @@ func checkTB(tb tb, prop func(*T)) { · 49 unmodified lines
104119 dt := time.Since(start)
105120
106121 if err1 == nil && err2 == nil {
107− if valid == *checks {
122+ if valid == flags.checks {
108123 tb.Logf("[rapid] OK, passed %v tests (%v)", valid, dt)
109124 } else {
110125 tb.Errorf("[rapid] only generated %v valid tests from %v total (%v)", valid, valid+invalid, dt)
@@ -140,7 +155,7 @@ func doCheck(tb tb, prop func(*T)) (int, int, uint64, []uint64, *testError, *tes · 29 unmodified lines
140155 }
141156
142157 s := newRandomBitStream(seed, true)
143− t := newT(tb, s, *verbose, nil)
158+ t := newT(tb, s, flags.verbose, nil)
144159 t.Logf("[rapid] trying to reproduce the failure")
145160 err2 := checkOnce(t, prop)
146161 if !sameError(err1, err2) {
@@ -158,12 +173,12 @@ func findBug(tb tb, seed uint64, prop func(*T)) (uint64, int, int, *testError) { · 11 unmodified lines
158173
159174 var (
160175 r = newRandomBitStream(0, false)
161− t = newT(tb, r, *verbose, nil)
176+ t = newT(tb, r, flags.verbose, nil)
162177 valid = 0
163178 invalid = 0
164179 )
165180
166− for valid < *checks && invalid < *checks*invalidChecksMult {
181+ for valid < flags.checks && invalid < flags.checks*invalidChecksMult {
167182 seed += uint64(valid) + uint64(invalid)
168183 r.init(seed)
169184 var start time.Time
@@ -319,7 +334,7 @@ func newT(tb tb, s bitStream, tbLog bool, rawLog *log.Logger, refDraws ...value) · 149 unmodified lines
319334 refDraws: refDraws,
320335 }
321336
322− if rawLog == nil && *rapidLog {
337+ if rawLog == nil && flags.rapidLog {
323338 testName := "rapid test"
324339 if tb != nil {
325340 testName = tb.Name()
shrink.go+6−6
@@ -46,7 +46,7 @@ func shrink(tb tb, rec recordedBits, err *testError, prop func(*T)) ([]uint64, * · 45 unmodified lines
4646
4747 buf, err := s.shrink()
4848
49− if *debugvis {
49+ if flags.debugvis {
5050 name := fmt.Sprintf("vis-%v.html", strings.Replace(tb.Name(), "/", "_", -1))
5151 f, err := os.Create(name)
5252 if err != nil {
@@ -76,7 +76,7 @@ type shrinker struct { · 23 unmodified lines
7676 }
7777
7878 func (s *shrinker) debugf(verbose_ bool, format string, args ...interface{}) {
79− if *debug && (!verbose_ || *verbose) {
79+ if flags.debug && (!verbose_ || flags.verbose) {
8080 s.tb.Helper()
8181 s.tb.Logf("[shrink] "+format, args...)
8282 }
@@ -90,7 +90,7 @@ func (s *shrinker) shrink() (buf []uint64, err *testError) { · 7 unmodified lines
9090 }()
9191
9292 i := 0
93− deadline := time.Now().Add(*shrinkTime)
93+ deadline := time.Now().Add(flags.shrinkTime)
9494 for shrinks := -1; s.shrinks > shrinks && time.Now().Before(deadline); i++ {
9595 shrinks = s.shrinks
9696
@@ -259,7 +259,7 @@ func (s *shrinker) accept(buf []uint64, label string, format string, args ...int · 162 unmodified lines
259259 s.debugf(true, label+": trying to reproduce the failure with a smaller test case: "+format, args...)
260260 s.tries[label]++
261261 s1 := newBufBitStream(buf, false)
262− err1 := checkOnce(newT(s.tb, s1, *debug && *verbose, nil), s.prop)
262+ err1 := checkOnce(newT(s.tb, s1, flags.debug && flags.verbose, nil), s.prop)
263263 if traceback(err1) != traceback(s.err) {
264264 s.cache[bufStr] = struct{}{}
265265 return false
@@ -269,11 +269,11 @@ func (s *shrinker) accept(buf []uint64, label string, format string, args ...int · 3 unmodified lines
269269 s.tries[label]++
270270 s.err = err1
271271 s2 := newBufBitStream(buf, true)
272− err2 := checkOnce(newT(s.tb, s2, *debug && *verbose, nil), s.prop)
272+ err2 := checkOnce(newT(s.tb, s2, flags.debug && flags.verbose, nil), s.prop)
273273 s.rec = s2.recordedBits
274274 s.rec.prune()
275275 assert(compareData(s.rec.data, buf) <= 0)
276− if *debugvis {
276+ if flags.debugvis {
277277 s.visBits = append(s.visBits, s.rec)
278278 }
279279 if !sameError(err1, err2) {
statemachine.go+1−1
@@ -59,7 +59,7 @@ func Run(m StateMachine) func(*T) { · 58 unmodified lines
5959 return func(t *T) {
6060 t.Helper()
6161
62− repeat := newRepeat(0, *steps, maxInt)
62+ repeat := newRepeat(0, flags.steps, maxInt)
6363
6464 sm := newStateMachine(typ)
6565 if sm.init != nil {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t