aboutsummaryrefslogtreecommitdiff
path: root/immutable_test.go
diff options
context:
space:
mode:
authorBen Johnson <benbjohnson@yahoo.com>2022-12-02 12:42:42 -0700
committerGitHub <noreply@github.com>2022-12-02 12:42:42 -0700
commit3bb6ffff486f57ae47181b3a6d2f47423b84181a (patch)
treeff82f4ab470e22f304c67c0894016ad0f4879e29 /immutable_test.go
parentMerge pull request #24 from banks/patch-1 (diff)
parentAllow lists to contain non-comparable elements (diff)
downloadpds-3bb6ffff486f57ae47181b3a6d2f47423b84181a.tar.gz
pds-3bb6ffff486f57ae47181b3a6d2f47423b84181a.tar.xz
Merge pull request #28 from BarrensZeppelin/master
Allow lists to contain non-comparable elements
Diffstat (limited to 'immutable_test.go')
-rw-r--r--immutable_test.go41
1 files changed, 41 insertions, 0 deletions
diff --git a/immutable_test.go b/immutable_test.go
index c4b0297..6a0ee22 100644
--- a/immutable_test.go
+++ b/immutable_test.go
@@ -180,6 +180,47 @@ func TestList(t *testing.T) {
}
})
+ t.Run("TestSliceFreesReferences", func(t *testing.T) {
+ /* Test that the leaf node in a sliced list contains zero'ed entries at
+ * the correct positions. To do this we directly access the internal
+ * tree structure of the list.
+ */
+ l := NewList[*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] {
+ switch n := n.(type) {
+ case *listBranchNode[*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]: return n
+ default: panic("Unexpected case")
+ }
+ }
+
+ leaf := findLeaf(sl.root)
+ if leaf.occupied != 0b1100 {
+ t.Errorf("Expected occupied to be 1100, was %032b", leaf.occupied)
+ }
+
+ for i := 0; i < listNodeSize; i++ {
+ if 2 <= i && i < 4 {
+ if leaf.children[i] != &ints[i] {
+ t.Errorf("Position %v does not contain the right pointer?", i)
+ }
+ } else if leaf.children[i] != nil {
+ t.Errorf("Expected position %v to be cleared, was %v", i, leaf.children[i])
+ }
+ }
+ })
+
RunRandom(t, "Random", func(t *testing.T, rand *rand.Rand) {
l := NewTList()
for i := 0; i < 100000; i++ {