forked from satojkovic/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_binary_search.py
More file actions
34 lines (29 loc) · 1.19 KB
/
Copy pathtest_binary_search.py
File metadata and controls
34 lines (29 loc) · 1.19 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
import unittest
from binary_search import *
from nose.tools import eq_
class TestBinarySearch(unittest.TestCase):
def setUp(self):
self.data = [1, 3, 4, 13, 40, 193]
self.data2 = [-1, 10, 29, 67, 998]
self.data3 = [1, 1, 1]
def test_binary_search_r(self):
eq_(binary_search_r(self.data, 40, 0, len(self.data) - 1), 4)
eq_(binary_search_r(self.data, 1, 0, len(self.data) - 1), 0)
eq_(binary_search_r(self.data, 5, 0, len(self.data) - 1), -1)
eq_(binary_search_r(self.data, 76, 0, len(self.data) - 1), -1)
eq_(binary_search_r(self.data, 1000, 0, len(self.data) - 1), -1)
eq_(binary_search_r(self.data, 0, 0, len(self.data) - 1), -1)
eq_(binary_search_r(self.data3, 1, 0, len(self.data3) - 1), 1)
eq_(binary_search_r(self.data3, 2, 0, len(self.data3) - 1), -1)
def test_search_2d_mat(self):
mat = [
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
eq_(search_2d_mat(mat, 5), True)
eq_(search_2d_mat(mat, 20), False)
if __name__ == "__main__":
unittest.main()