From 3e63f335df97e3eacce11514b85dc0536a58c18e Mon Sep 17 00:00:00 2001 From: maxbarsukov Date: Sun, 13 Oct 2024 20:57:31 +0300 Subject: [PATCH] test: :rotating_light: add tests for red_black_tree --- test/red_black_tree_test.exs | 46 ++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/test/red_black_tree_test.exs b/test/red_black_tree_test.exs index e256b3b..93a33fb 100644 --- a/test/red_black_tree_test.exs +++ b/test/red_black_tree_test.exs @@ -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])) @@ -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)