Skip to content

Instantly share code, notes, and snippets.

@untoldwind
Created June 7, 2021 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save untoldwind/f78910c522f6efb81ac140891016a5da to your computer and use it in GitHub Desktop.
Save untoldwind/f78910c522f6efb81ac140891016a5da to your computer and use it in GitHub Desktop.
Gopter bug 81
module github.com/rciorba/gopterbugreport
go 1.16
require github.com/leanovate/gopter v0.2.10-0.20210503084252-f350002bbbe3
github.com/leanovate/gopter v0.2.10-0.20210503084252-f350002bbbe3 h1:i4SWc+195Dj3aTwGoq/4TBhGHpywYi8nFFDMZURfzew=
github.com/leanovate/gopter v0.2.10-0.20210503084252-f350002bbbe3/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8=
package gopterbugreport
import (
"fmt"
"reflect"
"testing"
"github.com/leanovate/gopter"
"github.com/leanovate/gopter/commands"
"github.com/leanovate/gopter/gen"
)
// The Tests
// simplified SUT, if true we should fail the next PostCondition
type SUT = bool
// simplified state is gonna be just the length of the original state
type baState = int
// the setCommand type.
type setCommand struct {
Index int
Value bool
}
func (cmd setCommand) Run(sut commands.SystemUnderTest) commands.Result {
fmt.Printf("Run got CMD: %v \n", cmd)
if cmd.Index == 12 {
// Intentional "bug" in our SUT, from now on fail the postcondition
return true
}
return sut
}
func (cmd setCommand) NextState(state commands.State) commands.State {
fmt.Printf("NextState got CMD: %v \n", cmd)
st := state.(baState)
if cmd.Index < 0 || cmd.Index >= st {
panic(fmt.Sprintf("Index out of range [0, %v]: %v", st-1, cmd.Index))
}
return st
}
func (setCommand) PreCondition(state commands.State) bool {
return true
}
func (setCommand) PostCondition(state commands.State, result commands.Result) *gopter.PropResult {
sut := result.(SUT)
if sut {
fmt.Printf("We've encountered the fake bug, so we'll fail the PostCondition\n")
return &gopter.PropResult{Status: gopter.PropFalse}
}
return &gopter.PropResult{Status: gopter.PropTrue}
}
func (cmd setCommand) String() string {
return fmt.Sprintf("Set(%v, %v)", cmd.Index, cmd.Value)
}
func makeGenSetCommand(state baState) gopter.Gen {
genCommand := gen.Struct(
reflect.TypeOf(setCommand{}),
map[string]gopter.Gen{
// Despite the declared range we will see out of bounds indexes.
"Index": gen.IntRange(6, state-1),
"Value": gen.Bool(),
},
)
return genCommand
}
var bitarrayCommands = &commands.ProtoCommands{
NewSystemUnderTestFunc: func(initialState commands.State) commands.SystemUnderTest {
return false
},
InitialStateGen: gen.Const(16),
GenCommandFunc: func(state commands.State) gopter.Gen {
st := state.(baState)
return makeGenSetCommand(st)
},
}
func TestIntRangeShrinking(t *testing.T) {
var params *gopter.TestParameters
params = gopter.DefaultTestParameters()
params.Rng.Seed(1) // Just for this example to generate reproducible results
params.Workers = 1
properties := gopter.NewProperties(params)
properties.Property("always equivalent", commands.Prop(bitarrayCommands))
properties.TestingRun(t)
}
NextState got CMD: Set(14, true)
NextState got CMD: Set(14, true)
Run got CMD: Set(14, true)
NextState got CMD: Set(14, true)
NextState got CMD: Set(12, false)
NextState got CMD: Set(12, false)
NextState got CMD: Set(12, false)
NextState got CMD: Set(12, false)
Run got CMD: Set(12, false)
NextState got CMD: Set(12, false)
We've encountered the fake bug, so we'll fail the PostCondition
Run got CMD: Set(12, false)
NextState got CMD: Set(12, false)
We've encountered the fake bug, so we'll fail the PostCondition
NextState got CMD: Set(12, false)
Run got CMD: Set(12, false)
NextState got CMD: Set(12, false)
We've encountered the fake bug, so we'll fail the PostCondition
NextState got CMD: Set(6, false)
Run got CMD: Set(6, false)
NextState got CMD: Set(6, false)
NextState got CMD: Set(9, false)
Run got CMD: Set(9, false)
NextState got CMD: Set(9, false)
NextState got CMD: Set(11, false)
Run got CMD: Set(11, false)
NextState got CMD: Set(11, false)
! always equivalent: Falsified after 2 passed tests.
ARG_0: initialState=16 sequential=[Set(12, false)]
ARG_0_ORIGINAL (1 shrinks): initialState=16 sequential=[Set(12, false)
Set(12, false)]
Elapsed time: 638.271µs
--- FAIL: TestIntRangeShrinking (0.00s)
properties.go:57: failed with initial seed: 1623045649950164756
FAIL
exit status 1
FAIL github.com/rciorba/gopterbugreport 0.002s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment