-
-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathbinary_search_test.exs
More file actions
43 lines (34 loc) · 1.25 KB
/
binary_search_test.exs
File metadata and controls
43 lines (34 loc) · 1.25 KB
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
defmodule Algorithms.Search.BinarySearchTest do
alias Algorithms.Search.BinarySearch
use ExUnit.Case
describe "search/2 - example test cases" do
test "finds element in the middle of the list" do
assert BinarySearch.search([1, 3, 5, 7, 9], 5) == 2
end
test "finds element at the beginning of the list" do
assert BinarySearch.search([1, 3, 5, 7, 9], 1) == 0
end
test "finds element at the end of the list" do
assert BinarySearch.search([1, 3, 5, 7, 9], 9) == 4
end
test "returns -1 when element is not in the list" do
assert BinarySearch.search([1, 3, 5, 7, 9], 4) == -1
end
test "works with an empty list" do
assert BinarySearch.search([], 1) == -1
end
test "works with a single-element list" do
assert BinarySearch.search([1], 1) == 0
assert BinarySearch.search([1], 2) == -1
end
test "works with a large sorted list" do
list = Enum.to_list(1..1000)
assert BinarySearch.search(list, 500) == 499
assert BinarySearch.search(list, 1001) == -1
end
test "handles duplicate elements" do
assert BinarySearch.search([1, 2, 2, 3, 4, 5, 5, 6], 2) in [1, 2]
assert BinarySearch.search([1, 2, 2, 3, 4, 5, 5, 6], 5) in [5, 6]
end
end
end