1
exercism fetch elixir binary-search-tree

binary_search_tree_test.exs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
if !System.get_env("EXERCISM_TEST_EXAMPLES") do
  Code.load_file("binary_search_tree.exs", __DIR__)
end

ExUnit.start
ExUnit.configure exclude: :pending, trace: true

defmodule BinarySearchTreeTest do
  use ExUnit.Case

  test "retains data" do
    assert BinarySearchTree.new(4).data == 4
  end

  @tag :pending
  test "inserting lower number" do
    root =
      BinarySearchTree.new(4)
      |> BinarySearchTree.insert(2)

    assert root.data == 4
    assert root.left.data == 2
  end

  @tag :pending
  test "inserting same number" do
    root =
      BinarySearchTree.new(4)
      |> BinarySearchTree.insert(4)

    assert root.data == 4
    assert root.left.data == 4
  end

  @tag :pending
  test "inserting higher number" do
    root =
      BinarySearchTree.new(4)
      |> BinarySearchTree.insert(5)

    assert root.data == 4
    assert root.right.data == 5
  end

  @tag :pending
  test "complex tree" do
    root =
      BinarySearchTree.new(4)
      |> BinarySearchTree.insert(2)
      |> BinarySearchTree.insert(6)
      |> BinarySearchTree.insert(1)
      |> BinarySearchTree.insert(3)
      |> BinarySearchTree.insert(7)
      |> BinarySearchTree.insert(5)

    assert root.data == 4
    assert root.left.data == 2
    assert root.left.left.data == 1
    assert root.left.right.data == 3
    assert root.right.data == 6
    assert root.right.left.data == 5
    assert root.right.right.data == 7
  end

  @tag :pending
  test "iterating one element" do
    root = BinarySearchTree.new(4)

    assert [4] == BinarySearchTree.in_order(root)
  end

  @tag :pending
  test "iterating over smaller element" do
    root =
      BinarySearchTree.new(4)
      |> BinarySearchTree.insert(2)

    assert [2, 4] == BinarySearchTree.in_order(root)
  end

  @tag :pending
  test "iterating over larger element" do
    root =
      BinarySearchTree.new(4)
      |> BinarySearchTree.insert(5)

    assert [4, 5] == BinarySearchTree.in_order(root)
  end

  @tag :pending
  test "iterating over complex tree" do
    root =
      BinarySearchTree.new(4)
      |> BinarySearchTree.insert(2)
      |> BinarySearchTree.insert(1)
      |> BinarySearchTree.insert(3)
      |> BinarySearchTree.insert(6)
      |> BinarySearchTree.insert(7)
      |> BinarySearchTree.insert(5)

    assert [1, 2, 3, 4, 5, 6, 7] == BinarySearchTree.in_order(root)
  end
end