PBT all the things
All checks were successful
Go / test (push) Successful in 5s

This commit is contained in:
Adam Jeniski 2025-11-05 04:50:07 -10:00
parent a6f8502fbb
commit ba00edc4db

View File

@ -9,6 +9,7 @@ import (
"pgregory.net/rapid" "pgregory.net/rapid"
) )
// intListGen returns a generator for either empty list or random ints
func intListGen() *rapid.Generator[IList] { func intListGen() *rapid.Generator[IList] {
return rapid.OneOf(rapid.Custom(func(t *rapid.T) IList { return rapid.OneOf(rapid.Custom(func(t *rapid.T) IList {
myList := Empty() myList := Empty()
@ -21,22 +22,20 @@ func intListGen() *rapid.Generator[IList] {
}), rapid.Just(Empty())) }), rapid.Just(Empty()))
} }
func TestConjIncreasesLength(t *testing.T) { // descSortedIntListGen returns a generator for list of at least several descending ints
rapid.Check(t, func(t *rapid.T) { func descSortedIntListGen() *rapid.Generator[IList] {
myList := intListGen().Draw(t, "myList") return rapid.Custom(func(t *rapid.T) IList {
newElem := rapid.Int().Draw(t, "newElem") myList := New(0)
assert.Equal(t, myList.Len()+1, myList.Conj(newElem).Len(), "conj increases length") i := 1
t.Repeat(map[string]func(*rapid.T){
"conj": func(t *rapid.T) {
myList = myList.Conj(i)
i += 1
},
}) })
} return myList
}).Filter(func(l IList) bool {
func TestRestDecreasesLength(t *testing.T) { return l.Len() > 1
rapid.Check(t, func(t *rapid.T) {
myList := intListGen().Draw(t, "myList")
if myList.IsEmpty() {
assert.Equal(t, myList.Len(), myList.Rest().Len(), "rest does not change length for empty list")
} else {
assert.Equal(t, myList.Len()-1, myList.Rest().Len(), "rest decreases length of non-empty list")
}
}) })
} }
@ -49,6 +48,32 @@ func TestFirstIsNilOnlyWhenEmpty(t *testing.T) {
}) })
} }
func TestConj(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
myList := intListGen().Draw(t, "myList")
newElem := rapid.Int().Draw(t, "newElem")
myList2 := myList.Conj(newElem)
assert.Equal(t, myList.Len()+1, myList2.Len(), "conj increases length")
assert.Equal(t, newElem, myList2.First(), "first after conj returns inserted number")
})
}
func TestRest(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
myList := intListGen().Draw(t, "myList")
if myList.IsEmpty() {
assert.Equal(t, myList.Len(), myList.Rest().Len(), "rest does not change length for empty list")
} else {
assert.Equal(t, myList.Len()-1, myList.Rest().Len(), "rest decreases length of non-empty list")
}
})
rapid.Check(t, func(t *rapid.T) {
myList := descSortedIntListGen().Draw(t, "myList")
assert.GreaterOrEqual(t, myList.First(), myList.Rest().First(), "rest decreases value in list head")
})
}
func TestStringifyIntList(t *testing.T) { func TestStringifyIntList(t *testing.T) {
assert.Equal(t, "(1 2 3)", New(3).Conj(2).Conj(1).String()) assert.Equal(t, "(1 2 3)", New(3).Conj(2).Conj(1).String())
@ -56,7 +81,7 @@ func TestStringifyIntList(t *testing.T) {
myList := intListGen().Draw(t, "myList") myList := intListGen().Draw(t, "myList")
s := myList.String() s := myList.String()
r := regexp.MustCompile(`^\([\d\s-]*\)$`) r := regexp.MustCompile(`^\([\d\s-]*\)$`)
assert.Equal(t, true, r.Match([]byte(s)), s + " looks like a stringfied list") assert.Equal(t, true, r.Match([]byte(s)), s+" looks like a stringified list")
if !myList.IsEmpty() { if !myList.IsEmpty() {
assert.Equal(t, myList.Len(), strings.Count(s, " ")+1, "number of spaces in string should match count of elements") assert.Equal(t, myList.Len(), strings.Count(s, " ")+1, "number of spaces in string should match count of elements")
} else { } else {