|
| 1 | +package binarysearchtree_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "strconv" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/fgm/container" |
| 8 | + bst "github.com/fgm/container/binarysearchtree" |
| 9 | +) |
| 10 | + |
| 11 | +func TestIntrinsic_nil(t *testing.T) { |
| 12 | + var tree *bst.Intrinsic[int] |
| 13 | + tree.WalkInOrder(bst.P) |
| 14 | + tree.WalkPostOrder(bst.P) |
| 15 | + tree.WalkPreOrder(bst.P) |
| 16 | + tree.Upsert(nil) |
| 17 | + tree.Delete(nil) |
| 18 | + |
| 19 | + tree = &bst.Intrinsic[int]{} |
| 20 | + tree.WalkInOrder(bst.P) |
| 21 | + tree.WalkPostOrder(bst.P) |
| 22 | + tree.WalkPreOrder(bst.P) |
| 23 | + // Output: |
| 24 | +} |
| 25 | + |
| 26 | +func TestIntrinsic_Upsert(t *testing.T) { |
| 27 | + tree := bst.Simple() |
| 28 | + actual := tree.Upsert(&bst.One) |
| 29 | + if len(actual) != 1 { |
| 30 | + t.Fatalf("expected overwriting upsert to return one value, got %v", actual) |
| 31 | + } |
| 32 | + if *actual[0] != bst.One { |
| 33 | + t.Fatalf("expected overwriting upsert to return %d, got %d", bst.One, *actual[0]) |
| 34 | + } |
| 35 | + |
| 36 | + actual = tree.Upsert(&bst.Six) |
| 37 | + if len(actual) != 1 { |
| 38 | + t.Fatalf("expected non-overwriting upsert to return one value, got %v", actual) |
| 39 | + } |
| 40 | + if actual[0] != nil { |
| 41 | + t.Fatalf("expected non-overwriting upsert to return one nil, got %v", actual[0]) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestIntrinsic_IndexOf(t *testing.T) { |
| 46 | + tree := bst.Simple() |
| 47 | + checks := [...]struct { |
| 48 | + input int |
| 49 | + expectedOK bool |
| 50 | + expectedIndex int |
| 51 | + }{ |
| 52 | + {bst.One, true, 0}, |
| 53 | + {bst.Two, true, 1}, |
| 54 | + {bst.Three, true, 2}, |
| 55 | + {bst.Four, true, 3}, |
| 56 | + {bst.Five, true, 4}, |
| 57 | + {bst.Six, false, 0}, |
| 58 | + } |
| 59 | + for _, check := range checks { |
| 60 | + t.Run(strconv.Itoa(check.input), func(t *testing.T) { |
| 61 | + actualIndex, actualOK := tree.IndexOf(&check.input) |
| 62 | + if actualOK != check.expectedOK { |
| 63 | + t.Fatalf("%d found: %t but expected %t", check.input, actualOK, check.expectedOK) |
| 64 | + } |
| 65 | + if actualIndex != check.expectedIndex { |
| 66 | + t.Fatalf("%d at index %d but expected %d", check.input, actualIndex, check.expectedIndex) |
| 67 | + } |
| 68 | + }) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestIntrinsic_Len(t *testing.T) { |
| 73 | + si := bst.Simple().(container.Enumerable[int]).Elements() |
| 74 | + hf := bst.HalfFull().(container.Enumerable[int]).Elements() |
| 75 | + |
| 76 | + checks := [...]struct { |
| 77 | + name string |
| 78 | + input []int |
| 79 | + deletions []int |
| 80 | + expected int |
| 81 | + }{ |
| 82 | + {"empty", nil, nil, 0}, |
| 83 | + {"simple", si, nil, 5}, |
| 84 | + {"half-full", hf, nil, 6}, |
| 85 | + {"overwrite element", append(si, bst.Three), nil, 5}, |
| 86 | + {"delete nonexistent", si, []int{bst.Six}, 5}, |
| 87 | + {"delete existing childless", si, []int{bst.One}, 4}, |
| 88 | + {"delete existing with 1 left child", si, []int{bst.Two}, 4}, |
| 89 | + {"delete existing with 1 right child", si, []int{bst.Four}, 4}, |
| 90 | + {"delete existing with 2 children", hf, []int{bst.Three}, 5}, |
| 91 | + } |
| 92 | + for _, check := range checks { |
| 93 | + t.Run(check.name, func(t *testing.T) { |
| 94 | + tree := bst.Intrinsic[int]{} |
| 95 | + // In these loops, e is always the same variable: without cloning, |
| 96 | + // each iteration reuses the same pointer, overwriting the tree. |
| 97 | + for _, e := range check.input { |
| 98 | + clone := e |
| 99 | + tree.Upsert(&clone) |
| 100 | + } |
| 101 | + for _, e := range check.deletions { |
| 102 | + clone := e |
| 103 | + tree.Delete(&clone) |
| 104 | + } |
| 105 | + if tree.Len() != check.expected { |
| 106 | + t.Fatalf("Found len %d, but expected %d", tree.Len(), check.expected) |
| 107 | + } |
| 108 | + }) |
| 109 | + } |
| 110 | +} |
0 commit comments