|
| 1 | +from algorithms.searching.binary_search import ( |
| 2 | + binary_search, |
| 3 | + lower_bound, |
| 4 | + upper_bound, |
| 5 | +) |
| 6 | +from data_structures.array.dynamic_array import DynamicArray |
| 7 | +from data_structures.array.static_array import StaticArray |
| 8 | + |
| 9 | +# ── binary_search ──────────────────────────────────────────────────── |
| 10 | + |
| 11 | + |
| 12 | +def test_found_in_odd_length(): |
| 13 | + assert binary_search([1, 3, 5, 7, 9], 5) == 2 |
| 14 | + |
| 15 | + |
| 16 | +def test_found_in_even_length(): |
| 17 | + assert binary_search([2, 4, 6, 8], 6) == 2 |
| 18 | + |
| 19 | + |
| 20 | +def test_found_first_element(): |
| 21 | + assert binary_search([1, 2, 3], 1) == 0 |
| 22 | + |
| 23 | + |
| 24 | +def test_found_last_element(): |
| 25 | + assert binary_search([1, 2, 3], 3) == 2 |
| 26 | + |
| 27 | + |
| 28 | +def test_not_found(): |
| 29 | + assert binary_search([1, 2, 3, 4, 5], 42) == -1 |
| 30 | + |
| 31 | + |
| 32 | +def test_empty_list(): |
| 33 | + assert binary_search([], 1) == -1 |
| 34 | + |
| 35 | + |
| 36 | +def test_single_element_found(): |
| 37 | + assert binary_search([7], 7) == 0 |
| 38 | + |
| 39 | + |
| 40 | +def test_single_element_not_found(): |
| 41 | + assert binary_search([7], 3) == -1 |
| 42 | + |
| 43 | + |
| 44 | +def test_duplicates_returns_one_of_them(): |
| 45 | + result = binary_search([1, 3, 3, 3, 5], 3) |
| 46 | + assert result in (1, 2, 3) |
| 47 | + |
| 48 | + |
| 49 | +def test_strings(): |
| 50 | + assert binary_search(["a", "b", "c", "d"], "c") == 2 |
| 51 | + |
| 52 | + |
| 53 | +def test_large_list(): |
| 54 | + arr = list(range(0, 10000, 2)) # [0, 2, 4, ..., 9998] |
| 55 | + assert binary_search(arr, 4444) == 2222 |
| 56 | + assert binary_search(arr, 4445) == -1 |
| 57 | + |
| 58 | + |
| 59 | +# ── lower_bound ────────────────────────────────────────────────────── |
| 60 | + |
| 61 | + |
| 62 | +def test_lower_bound_exact(): |
| 63 | + assert lower_bound([1, 3, 5, 7], 3) == 1 |
| 64 | + |
| 65 | + |
| 66 | +def test_lower_bound_between(): |
| 67 | + assert lower_bound([1, 3, 5, 7], 4) == 2 |
| 68 | + |
| 69 | + |
| 70 | +def test_lower_bound_less_than_all(): |
| 71 | + assert lower_bound([2, 4, 6], 0) == 0 |
| 72 | + |
| 73 | + |
| 74 | +def test_lower_bound_greater_than_all(): |
| 75 | + assert lower_bound([2, 4, 6], 10) == 3 |
| 76 | + |
| 77 | + |
| 78 | +def test_lower_bound_duplicates(): |
| 79 | + assert lower_bound([1, 3, 3, 3, 5], 3) == 1 |
| 80 | + |
| 81 | + |
| 82 | +def test_lower_bound_empty(): |
| 83 | + assert lower_bound([], 1) == 0 |
| 84 | + |
| 85 | + |
| 86 | +# ── upper_bound ────────────────────────────────────────────────────── |
| 87 | + |
| 88 | + |
| 89 | +def test_upper_bound_exact(): |
| 90 | + assert upper_bound([1, 3, 5, 7], 3) == 2 |
| 91 | + |
| 92 | + |
| 93 | +def test_upper_bound_between(): |
| 94 | + assert upper_bound([1, 3, 5, 7], 4) == 2 |
| 95 | + |
| 96 | + |
| 97 | +def test_upper_bound_less_than_all(): |
| 98 | + assert upper_bound([2, 4, 6], 0) == 0 |
| 99 | + |
| 100 | + |
| 101 | +def test_upper_bound_greater_than_all(): |
| 102 | + assert upper_bound([2, 4, 6], 10) == 3 |
| 103 | + |
| 104 | + |
| 105 | +def test_upper_bound_duplicates(): |
| 106 | + assert upper_bound([1, 3, 3, 3, 5], 3) == 4 |
| 107 | + |
| 108 | + |
| 109 | +def test_upper_bound_empty(): |
| 110 | + assert upper_bound([], 1) == 0 |
| 111 | + |
| 112 | + |
| 113 | +# ── StaticArray ────────────────────────────────────────────────────── |
| 114 | + |
| 115 | + |
| 116 | +def test_binary_search_static_array(): |
| 117 | + assert binary_search(StaticArray([1, 3, 5, 7, 9]), 5) == 2 |
| 118 | + assert binary_search(StaticArray([1, 3, 5, 7, 9]), 42) == -1 |
| 119 | + |
| 120 | + |
| 121 | +def test_lower_bound_static_array(): |
| 122 | + assert lower_bound(StaticArray([1, 3, 5, 7]), 4) == 2 |
| 123 | + |
| 124 | + |
| 125 | +def test_upper_bound_static_array(): |
| 126 | + assert upper_bound(StaticArray([1, 3, 3, 3, 5]), 3) == 4 |
| 127 | + |
| 128 | + |
| 129 | +# ── DynamicArray ───────────────────────────────────────────────────── |
| 130 | + |
| 131 | + |
| 132 | +def test_binary_search_dynamic_array(): |
| 133 | + assert binary_search(DynamicArray([1, 3, 5, 7, 9]), 5) == 2 |
| 134 | + assert binary_search(DynamicArray([1, 3, 5, 7, 9]), 42) == -1 |
| 135 | + |
| 136 | + |
| 137 | +def test_lower_bound_dynamic_array(): |
| 138 | + assert lower_bound(DynamicArray([1, 3, 5, 7]), 4) == 2 |
| 139 | + |
| 140 | + |
| 141 | +def test_upper_bound_dynamic_array(): |
| 142 | + assert upper_bound(DynamicArray([1, 3, 3, 3, 5]), 3) == 4 |
0 commit comments