Skip to content

Commit

Permalink
test: 🚨 add tests for red_black_tree
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbarsukov committed Oct 13, 2024
1 parent d295822 commit 3e63f33
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions test/red_black_tree_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ defmodule RedBlackTreeTest do
alias RedBlackTree.Node
doctest RedBlackTree

setup do
tree = RedBlackTree.new()
{:ok, tree: tree}
end

test "initializing a red black tree" do
assert %RedBlackTree{} == RedBlackTree.new()
assert %RedBlackTree{} == RedBlackTree.empty()
assert 0 == RedBlackTree.new().size
assert 0 == RedBlackTree.empty().size

assert [{1, 1}, {2, 2}, {:c, :c}] == RedBlackTree.to_list(RedBlackTree.new([1, 2, :c]))
assert [{1, 1}, {2, 2}, {:c, :c}] == RedBlackTree.to_list(RedBlackTree.new([1, 2, :c]))
Expand Down Expand Up @@ -100,6 +107,45 @@ defmodule RedBlackTreeTest do
RedBlackTree.reduce_nodes(unchanged_tree, %{}, depth_aggregator)
end

test "insert and get", %{tree: tree} do
tree = RedBlackTree.insert(tree, :a, 1)
assert RedBlackTree.get(tree, :a) == 1
assert RedBlackTree.get(tree, :b) == nil
end

test "insert and member?", %{tree: tree} do
tree = RedBlackTree.insert(tree, :a, 1)
assert RedBlackTree.member?(tree, :a) == true
assert RedBlackTree.member?(tree, :b) == false
end

test "pop", %{tree: tree} do
tree = RedBlackTree.insert(tree, :a, 1)
{value, new_tree} = RedBlackTree.pop(tree, :a)
assert value == 1
assert RedBlackTree.member?(new_tree, :a) == false
end

test "fold_left", %{tree: tree} do
tree = RedBlackTree.insert(tree, :a, 1) |> RedBlackTree.insert(:b, 2)
result = RedBlackTree.fold_left(RedBlackTree.to_list(tree), 0, fn acc, {_, v} -> acc + v end)
assert result == 3
end

test "fold_right", %{tree: tree} do
tree = RedBlackTree.insert(tree, :a, 1) |> RedBlackTree.insert(:b, 2)
result = RedBlackTree.fold_right(RedBlackTree.to_list(tree), 0, fn {_, v}, acc -> acc + v end)
assert result == 3
end

test "Enumerable implementation", %{tree: tree} do
tree = RedBlackTree.insert(tree, :a, 1)
tree = RedBlackTree.insert(tree, :b, 2)
assert Enum.count(tree) == 2
assert Enum.member?(tree, :a) == true
assert Enum.member?(tree, :c) == false
end

test "reduce_nodes" do
initial_tree = RedBlackTree.new(d: 1, b: 2, f: 3, g: 4, c: 5, a: 6, e: 7)

Expand Down

0 comments on commit 3e63f33

Please sign in to comment.