Browse Source

clist: add simple property tests (#6791)

Adds a simple property test to the `clist` package. This test uses the [rapid](https://github.com/flyingmutant/rapid) library and works by modeling the internal clist as a simple array.

Follow up from this mornings workshop with the Regen team.
pull/6796/head
William Banfield 3 years ago
committed by GitHub
parent
commit
e801328128
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 75 additions and 0 deletions
  1. +1
    -0
      go.mod
  2. +2
    -0
      go.sum
  3. +72
    -0
      internal/libs/clist/clist_property_test.go

+ 1
- 0
go.mod View File

@ -39,4 +39,5 @@ require (
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4
google.golang.org/grpc v1.39.0
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b // indirect
pgregory.net/rapid v0.4.7 // indirect
)

+ 2
- 0
go.sum View File

@ -1437,6 +1437,8 @@ mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b h1:DxJ5nJdkhDlLok9K6qO+5290kphD
mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b/go.mod h1:2odslEg/xrtNQqCYg2/jCoyKnw3vv5biOc3JnIcYfL4=
mvdan.cc/unparam v0.0.0-20210104141923-aac4ce9116a7 h1:HT3e4Krq+IE44tiN36RvVEb6tvqeIdtsVSsxmNPqlFU=
mvdan.cc/unparam v0.0.0-20210104141923-aac4ce9116a7/go.mod h1:hBpJkZE8H/sb+VRFvw2+rBpHNsTBcvSpk61hr8mzXZE=
pgregory.net/rapid v0.4.7 h1:MTNRktPuv5FNqOO151TM9mDTa+XHcX6ypYeISDVD14g=
pgregory.net/rapid v0.4.7/go.mod h1:UYpPVyjFHzYBGHIxLFoupi8vwk6rXNzRY9OMvVxFIOU=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=


+ 72
- 0
internal/libs/clist/clist_property_test.go View File

@ -0,0 +1,72 @@
package clist_test
import (
"testing"
"github.com/stretchr/testify/require"
"pgregory.net/rapid"
"github.com/tendermint/tendermint/internal/libs/clist"
)
func TestCListProperties(t *testing.T) {
rapid.Check(t, rapid.Run(&clistModel{}))
}
// clistModel is used by the rapid state machine testing framework.
// clistModel contains both the clist that is being tested and a slice of *clist.CElements
// that will be used to model the expected clist behavior.
type clistModel struct {
clist *clist.CList
model []*clist.CElement
}
// Init is a method used by the rapid state machine testing library.
// Init is called when the test starts to initialize the data that will be used
// in the state machine test.
func (m *clistModel) Init(t *rapid.T) {
m.clist = clist.New()
m.model = []*clist.CElement{}
}
// PushBack defines an action that will be randomly selected across by the rapid state
// machines testing library. Every call to PushBack calls PushBack on the clist and
// performs a similar action on the model data.
func (m *clistModel) PushBack(t *rapid.T) {
value := rapid.String().Draw(t, "value").(string)
el := m.clist.PushBack(value)
m.model = append(m.model, el)
}
// Remove defines an action that will be randomly selected across by the rapid state
// machine testing library. Every call to Remove selects an element from the model
// and calls Remove on the CList with that element. The same element is removed from
// the model to keep the objects in sync.
func (m *clistModel) Remove(t *rapid.T) {
if len(m.model) == 0 {
return
}
ix := rapid.IntRange(0, len(m.model)-1).Draw(t, "index").(int)
value := m.model[ix]
m.model = append(m.model[:ix], m.model[ix+1:]...)
m.clist.Remove(value)
}
// Check is a method required by the rapid state machine testing library.
// Check is run after each action and is used to verify that the state of the object,
// in this case a clist.CList matches the state of the objec.
func (m *clistModel) Check(t *rapid.T) {
require.Equal(t, len(m.model), m.clist.Len())
if len(m.model) == 0 {
return
}
require.Equal(t, m.model[0], m.clist.Front())
require.Equal(t, m.model[len(m.model)-1], m.clist.Back())
iter := m.clist.Front()
for _, val := range m.model {
require.Equal(t, val, iter)
iter = iter.Next()
}
}

Loading…
Cancel
Save