add PBT infra

This commit is contained in:
2025-11-04 14:49:36 -10:00
parent 645fd64752
commit c044f452b5
5 changed files with 42 additions and 5 deletions
+9 -1
View File
@@ -5,13 +5,13 @@ import (
"strings"
)
type IList interface {
Conj(data any) IList
First() any
Rest() IList
String() string
IsEmpty() bool
Len() int
}
type EmptyList struct{}
@@ -121,3 +121,11 @@ func (this *List) String() string {
func (this *EmptyList) String() string {
return "()"
}
func (this *EmptyList) Len() int {
return 0
}
func (this *List) Len() int {
return 1 + this.Rest().Len()
}
+29 -4
View File
@@ -2,14 +2,21 @@ package list
import (
"github.com/stretchr/testify/assert"
"pgregory.net/rapid"
"testing"
)
func TestReadString(t *testing.T) {
func TestListLen(t *testing.T) {
assert.Equal(t, 0, Empty().Len(), "should insert at head")
assert.Equal(t, 1, Empty().Conj(1).Len(), "should insert at head")
assert.Equal(t, 2, Empty().Conj(1).Conj(2).Len(), "should insert at head")
}
func TestListString(t *testing.T) {
assert.Equal(t, "()", Empty().String(), "should insert at head")
}
func TestReadConj(t *testing.T) {
func TestListConj(t *testing.T) {
var l IList
assert.Equal(t, "()", Empty().String(), "should insert at head")
l = New(5)
@@ -18,13 +25,13 @@ func TestReadConj(t *testing.T) {
assert.Equal(t, "(4)", l.String(), "should insert at head")
}
func TestReadFirst(t *testing.T) {
func TestListFirst(t *testing.T) {
l := Empty().Conj(5).Conj(6).Conj(7)
assert.Equal(t, 7, l.First(), "should return first element")
assert.Equal(t, nil, Empty().First(), "should return nil")
}
func TestReadRest(t *testing.T) {
func TestListRest(t *testing.T) {
l := Empty().Conj(5).Conj(6).Conj(7)
assert.Equal(t, "(6 5)", l.Rest().String(), "should return rest sublist")
assert.Equal(t, "(5)", l.Rest().Rest().String(), "should return rest sublist")
@@ -32,3 +39,21 @@ func TestReadRest(t *testing.T) {
assert.Equal(t, Empty(), Empty().Rest(), "should return rest sublist")
assert.Equal(t, Empty(), Rest(Empty().Rest()), "should return rest sublist")
}
type listGen struct{}
// generative tests
func TestConjIncreasesLen(t *testing.T) {
l := Empty()
expectedSized := 0
rapid.Check(t, func(t *rapid.T) {
t.Repeat(map[string]func(*rapid.T){
"conj": func(t *rapid.T) {
expectedSized += 0
},
"": func(t *rapid.T) {
assert.Equal(t, l.Len(), expectedSized, "must be equal")
},
})
})
}