mal-go/list/list_test.go
ajet cfbddb3f39
All checks were successful
Go / test (push) Successful in 4s
test First()
2025-11-04 17:19:45 -10:00

81 lines
2.2 KiB
Go

package list
import (
"github.com/stretchr/testify/assert"
"pgregory.net/rapid"
"testing"
)
/*
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 TestListConj(t *testing.T) {
var l IList
assert.Equal(t, "()", Empty().String(), "should insert at head")
l = New(5)
assert.Equal(t, "(5)", l.String(), "should insert at head")
l = Empty().Conj(4)
assert.Equal(t, "(4)", l.String(), "should insert at head")
}
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 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")
assert.Equal(t, "()", l.Rest().Rest().Rest().String(), "should return rest sublist")
assert.Equal(t, Empty(), Empty().Rest(), "should return rest sublist")
assert.Equal(t, Empty(), Empty().Rest().Rest(), "should return rest sublist")
}
*/
func intListGen() *rapid.Generator[IList] {
return rapid.Custom(func(t *rapid.T) IList {
myList := Empty()
t.Repeat(map[string]func(*rapid.T){
"conj": func(t *rapid.T) {
myList = myList.Conj(rapid.Int().Draw(t, "el"))
},
})
return myList
})
}
// generative tests
func TestConjIncreasesLength(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
myList := intListGen().Draw(t, "myList")
assert.Equal(t, myList.Len()+1, myList.Conj(42).Len())
})
}
func TestRestDecreasesLengthForNonEmptyList(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
myList := intListGen().Filter(func(l IList) bool {
return !l.IsEmpty()
}).Draw(t, "myList")
assert.Equal(t, myList.Len()-1, myList.Rest().Len())
})
}
func TestFirstIsNilWhenEmpty(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
myList := intListGen().Draw(t, "myList")
assert.Equal(t, myList.IsEmpty(), myList.First() == nil)
})
}