diff options
Diffstat (limited to 'tests/pds.go')
-rw-r--r-- | tests/pds.go | 172 |
1 files changed, 86 insertions, 86 deletions
diff --git a/tests/pds.go b/tests/pds.go index 049edae..33ef400 100644 --- a/tests/pds.go +++ b/tests/pds.go @@ -15,50 +15,50 @@ import ( -func test_NewList() { - g.TestStart("NewList[]()") +func test_NewVector() { + g.TestStart("NewVector[]()") - g.Testing("a brand new list starts empty", func() { - g.TAssertEqual(NewList[int]().Len(), 0) + g.Testing("a brand new vector starts empty", func() { + g.TAssertEqual(NewVector[int]().Len(), 0) }) } -func TestList(t *testing.T) { +func TestVector(t *testing.T) { t.Run("Shallow", func(t *testing.T) { - list := NewList[string]() - list = list.Append("foo") - if v := list.Get(0); v != "foo" { + vector := NewVector[string]() + vector = vector.Append("foo") + if v := vector.Get(0); v != "foo" { t.Fatalf("unexpected value: %v", v) } - other := list.Append("bar") + other := vector.Append("bar") if v := other.Get(0); v != "foo" { t.Fatalf("unexpected value: %v", v) } else if v := other.Get(1); v != "bar" { t.Fatalf("unexpected value: %v", v) } - if v := list.Len(); v != 1 { + if v := vector.Len(); v != 1 { t.Fatalf("unexpected value: %v", v) } }) t.Run("Deep", func(t *testing.T) { - list := NewList[int]() + vector := NewVector[int]() var array []int const n = 1000 // FIXME: 100000 was too slow for i := 0; i < n; i++ { - list = list.Append(i) + vector = vector.Append(i) array = append(array, i) } - if got, exp := len(array), list.Len(); got != exp { - t.Fatalf("List.Len()=%d, exp %d", got, exp) + if got, exp := len(array), vector.Len(); got != exp { + t.Fatalf("Vector.Len()=%d, exp %d", got, exp) } for j := range array { - if got, exp := list.Get(j), array[j]; got != exp { + if got, exp := vector.Get(j), array[j]; got != exp { t.Fatalf( - "%d. List.Get(%d)=%d, exp %d", + "%d. Vector.Get(%d)=%d, exp %d", len(array), j, got, @@ -69,18 +69,18 @@ func TestList(t *testing.T) { }) t.Run("Set", func(t *testing.T) { - list := NewList[string]() - list = list.Append("foo") - list = list.Append("bar") + vector := NewVector[string]() + vector = vector.Append("foo") + vector = vector.Append("bar") - if v := list.Get(0); v != "foo" { + if v := vector.Get(0); v != "foo" { t.Fatalf("unexpected value: %v", v) } - list = list.Set(0, "baz") - if v := list.Get(0); v != "baz" { + vector = vector.Set(0, "baz") + if v := vector.Get(0); v != "baz" { t.Fatalf("unexpected value: %v", v) - } else if v := list.Get(1); v != "bar" { + } else if v := vector.Get(1); v != "bar" { t.Fatalf("unexpected value: %v", v) } }) @@ -89,11 +89,11 @@ func TestList(t *testing.T) { var r string func() { defer func() { r = recover().(string) }() - l := NewList[string]() + l := NewVector[string]() l = l.Append("foo") l.Get(-1) }() - if r != `immutable.List.Get: index -1 out of bounds` { + if r != `immutable.Vector.Get: index -1 out of bounds` { t.Fatalf("unexpected panic: %q", r) } }) @@ -102,11 +102,11 @@ func TestList(t *testing.T) { var r string func() { defer func() { r = recover().(string) }() - l := NewList[string]() + l := NewVector[string]() l = l.Append("foo") l.Get(1) }() - if r != `immutable.List.Get: index 1 out of bounds` { + if r != `immutable.Vector.Get: index 1 out of bounds` { t.Fatalf("unexpected panic: %q", r) } }) @@ -115,11 +115,11 @@ func TestList(t *testing.T) { var r string func() { defer func() { r = recover().(string) }() - l := NewList[string]() + l := NewVector[string]() l = l.Append("foo") l.Set(1, "bar") }() - if r != `immutable.List.Set: index 1 out of bounds` { + if r != `immutable.Vector.Set: index 1 out of bounds` { t.Fatalf("unexpected panic: %q", r) } }) @@ -128,11 +128,11 @@ func TestList(t *testing.T) { var r string func() { defer func() { r = recover().(string) }() - l := NewList[string]() + l := NewVector[string]() l = l.Append("foo") l.Slice(2, 3) }() - if r != `immutable.List.Slice: start index 2 out of bounds` { + if r != `immutable.Vector.Slice: start index 2 out of bounds` { t.Fatalf("unexpected panic: %q", r) } }) @@ -141,11 +141,11 @@ func TestList(t *testing.T) { var r string func() { defer func() { r = recover().(string) }() - l := NewList[string]() + l := NewVector[string]() l = l.Append("foo") l.Slice(1, 3) }() - if r != `immutable.List.Slice: end index 3 out of bounds` { + if r != `immutable.Vector.Slice: end index 3 out of bounds` { t.Fatalf("unexpected panic: %q", r) } }) @@ -154,25 +154,25 @@ func TestList(t *testing.T) { var r string func() { defer func() { r = recover().(string) }() - l := NewList[string]() + l := NewVector[string]() l = l.Append("foo") l = l.Append("bar") l.Slice(2, 1) }() - if r != `immutable.List.Slice: invalid slice index: [2:1]` { + if r != `immutable.Vector.Slice: invalid slice index: [2:1]` { t.Fatalf("unexpected panic: %q", r) } }) t.Run("SliceBeginning", func(t *testing.T) { - l := NewList[string]() + l := NewVector[string]() l = l.Append("foo") l = l.Append("bar") l = l.Slice(1, 2) if got, exp := l.Len(), 1; got != exp { - t.Fatalf("List.Len()=%d, exp %d", got, exp) + t.Fatalf("Vector.Len()=%d, exp %d", got, exp) } else if got, exp := l.Get(0), "bar"; got != exp { - t.Fatalf("List.Get(0)=%v, exp %v", got, exp) + t.Fatalf("Vector.Get(0)=%v, exp %v", got, exp) } }) @@ -180,35 +180,35 @@ func TestList(t *testing.T) { var r string func() { defer func() { r = recover().(string) }() - l := NewList[string]() + l := NewVector[string]() l = l.Append("foo") l.Iterator().Seek(-1) }() - if r != `immutable.ListIterator.Seek: index -1 out of bounds` { + if r != `immutable.VectorIterator.Seek: index -1 out of bounds` { t.Fatalf("unexpected panic: %q", r) } }) t.Run("TestSliceFreesReferences", func(t *testing.T) { - // Test that the leaf node in a sliced list contains zero'ed + // Test that the leaf node in a sliced vector contains zero'ed // entries at the correct positions. To do this we directly - // access the internal tree structure of the list. - l := NewList[*int]() + // access the internal tree structure of the vector. + l := NewVector[*int]() var ints [5]int for i := 0; i < 5; i++ { l = l.Append(&ints[i]) } sl := l.Slice(2, 4) - var findLeaf func(listNode[*int]) *listLeafNode[*int] - findLeaf = func(n listNode[*int]) *listLeafNode[*int] { + var findLeaf func(vectorNode[*int]) *vectorLeafNode[*int] + findLeaf = func(n vectorNode[*int]) *vectorLeafNode[*int] { switch n := n.(type) { - case *listBranchNode[*int]: + case *vectorBranchNode[*int]: if n.children[0] == nil { t.Fatal("Failed to find leaf node due to nil child") } return findLeaf(n.children[0]) - case *listLeafNode[*int]: + case *vectorLeafNode[*int]: return n default: panic("Unexpected case") @@ -220,7 +220,7 @@ func TestList(t *testing.T) { t.Errorf("Expected occupied to be 1100, was %032b", leaf.occupied) } - for i := 0; i < listNodeSize; i++ { + for i := 0; i < vectorNodeSize; i++ { if 2 <= i && i < 4 { if leaf.children[i] != &ints[i] { t.Errorf("Position %v does not contain the right pointer?", i) @@ -232,12 +232,12 @@ func TestList(t *testing.T) { }) t.Run("AppendImmutable", func(t *testing.T) { - outer_l := NewList[int]() + outer_l := NewVector[int]() for N := 0; N < 1_000; N++ { l1 := outer_l.Append(0) outer_l.Append(1) if actual := l1.Get(N); actual != 0 { - t.Fatalf("Append mutates list with %d elements. Got %d instead of 0", N, actual) + t.Fatalf("Append mutates vector with %d elements. Got %d instead of 0", N, actual) } outer_l = outer_l.Append(0) @@ -246,7 +246,7 @@ func TestList(t *testing.T) { RunRandom(t, "Random", func(t *testing.T, rand *rand.Rand) { const n = 100 // FIXME: 100000 was too slow - l := newTList() + l := newTVector() for i := 0; i < n; i++ { rnd := rand.Intn(70) switch { @@ -269,29 +269,29 @@ func TestList(t *testing.T) { }) } -// TList represents a list that operates on a standard Go slice & immutable -// list. -type tList struct { - im, prev *List[int] - builder *ListBuilder[int] +// TVector represents a vector that operates on a standard Go slice & immutable +// vector. +type tVector struct { + im, prev *Vector[int] + builder *VectorBuilder[int] std []int } -// newTList returns a new instance of tList. -func newTList() *tList { - return &tList{ - im: NewList[int](), - builder: NewListBuilder[int](), +// newTVector returns a new instance of tVector. +func newTVector() *tVector { + return &tVector{ + im: NewVector[int](), + builder: NewVectorBuilder[int](), } } -// Len returns the size of the list. -func (l *tList) Len() int { +// Len returns the size of the vector. +func (l *tVector) Len() int { return len(l.std) } // ChooseIndex returns a randomly chosen, valid index from the standard slice. -func (l *tList) ChooseIndex(rand *rand.Rand) int { +func (l *tVector) ChooseIndex(rand *rand.Rand) int { if len(l.std) == 0 { return -1 } @@ -299,7 +299,7 @@ func (l *tList) ChooseIndex(rand *rand.Rand) int { } // ChooseSliceIndices returns randomly chosen, valid indices for slicing. -func (l *tList) ChooseSliceIndices(rand *rand.Rand) (start, end int) { +func (l *tVector) ChooseSliceIndices(rand *rand.Rand) (start, end int) { if len(l.std) == 0 { return 0, 0 } @@ -308,40 +308,40 @@ func (l *tList) ChooseSliceIndices(rand *rand.Rand) (start, end int) { return start, end } -// Append adds v to the end of slice and List. -func (l *tList) Append(v int) { +// Append adds v to the end of slice and Vector. +func (l *tVector) Append(v int) { l.prev = l.im l.im = l.im.Append(v) l.builder.Append(v) l.std = append(l.std, v) } -// Prepend adds v to the beginning of the slice and List. -func (l *tList) Prepend(v int) { +// Prepend adds v to the beginning of the slice and Vector. +func (l *tVector) Prepend(v int) { l.prev = l.im l.im = l.im.Prepend(v) l.builder.Prepend(v) l.std = append([]int{v}, l.std...) } -// Set updates the value at index i to v in the slice and List. -func (l *tList) Set(i, v int) { +// Set updates the value at index i to v in the slice and Vector. +func (l *tVector) Set(i, v int) { l.prev = l.im l.im = l.im.Set(i, v) l.builder.Set(i, v) l.std[i] = v } -// Slice contracts the slice and List to the range of start/end indices. -func (l *tList) Slice(start, end int) { +// Slice contracts the slice and Vector to the range of start/end indices. +func (l *tVector) Slice(start, end int) { l.prev = l.im l.im = l.im.Slice(start, end) l.builder.Slice(start, end) l.std = l.std[start:end] } -// Validate returns an error if the slice and List are different. -func (l *tList) Validate() error { +// Validate returns an error if the slice and Vector are different. +func (l *tVector) Validate() error { if got, exp := l.im.Len(), len(l.std); got != exp { return fmt.Errorf("Len()=%v, expected %d", got, exp) } else if got, exp := l.builder.Len(), len(l.std); got != exp { @@ -352,7 +352,7 @@ func (l *tList) Validate() error { if got, exp := l.im.Get(i), l.std[i]; got != exp { return fmt.Errorf("Get(%d)=%v, expected %v", i, got, exp) } else if got, exp := l.builder.Get(i), l.std[i]; got != exp { - return fmt.Errorf("Builder.List/Get(%d)=%v, expected %v", i, got, exp) + return fmt.Errorf("Builder.Vector/Get(%d)=%v, expected %v", i, got, exp) } } @@ -370,37 +370,37 @@ func (l *tList) Validate() error { return nil } -func (l *tList) validateForwardIterator(typ string, itr *ListIterator[int]) error { +func (l *tVector) validateForwardIterator(typ string, itr *VectorIterator[int]) error { for i := range l.std { if j, v := itr.Next(); i != j || l.std[i] != v { - return fmt.Errorf("ListIterator.Next()=<%v,%v>, expected <%v,%v> [%s]", j, v, i, l.std[i], typ) + return fmt.Errorf("VectorIterator.Next()=<%v,%v>, expected <%v,%v> [%s]", j, v, i, l.std[i], typ) } done := i == len(l.std)-1 if v := itr.Done(); v != done { - return fmt.Errorf("ListIterator.Done()=%v, expected %v [%s]", v, done, typ) + return fmt.Errorf("VectorIterator.Done()=%v, expected %v [%s]", v, done, typ) } } if i, v := itr.Next(); i != -1 || v != 0 { - return fmt.Errorf("ListIterator.Next()=<%v,%v>, expected DONE [%s]", i, v, typ) + return fmt.Errorf("VectorIterator.Next()=<%v,%v>, expected DONE [%s]", i, v, typ) } return nil } -func (l *tList) validateBackwardIterator(typ string, itr *ListIterator[int]) error { +func (l *tVector) validateBackwardIterator(typ string, itr *VectorIterator[int]) error { itr.Last() for i := len(l.std) - 1; i >= 0; i-- { if j, v := itr.Prev(); i != j || l.std[i] != v { - return fmt.Errorf("ListIterator.Prev()=<%v,%v>, expected <%v,%v> [%s]", j, v, i, l.std[i], typ) + return fmt.Errorf("VectorIterator.Prev()=<%v,%v>, expected <%v,%v> [%s]", j, v, i, l.std[i], typ) } done := i == 0 if v := itr.Done(); v != done { - return fmt.Errorf("ListIterator.Done()=%v, expected %v [%s]", v, done, typ) + return fmt.Errorf("VectorIterator.Done()=%v, expected %v [%s]", v, done, typ) } } if i, v := itr.Prev(); i != -1 || v != 0 { - return fmt.Errorf("ListIterator.Prev()=<%v,%v>, expected DONE [%s]", i, v, typ) + return fmt.Errorf("VectorIterator.Prev()=<%v,%v>, expected DONE [%s]", i, v, typ) } return nil } @@ -2030,10 +2030,10 @@ func TestSortedSetBuilder(t *testing.T) { func MainTest() { - test_NewList() + test_NewVector() tests := []testing.InternalTest{ - { "TestList", TestList }, + { "TestVector", TestVector }, { "TestInternal_mapNode_Overwrite", TestInternal_mapNode_Overwrite }, { "TestInternal_mapArrayNode", TestInternal_mapArrayNode }, { "TestInternal_mapValueNode", TestInternal_mapValueNode }, |