forked from phuang07/python_algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_binary_search.py
More file actions
51 lines (37 loc) · 1.03 KB
/
Copy pathtest_binary_search.py
File metadata and controls
51 lines (37 loc) · 1.03 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
44
45
46
47
48
49
50
51
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
test_binary_search
----------------------------------
Tests for `python_algorithms.binary_search` module.
"""
import random
import unittest
from python_algorithms.basic import binary_search as bs
class TestBinarySearch(unittest.TestCase):
def setUp(self):
self.size = 100
self.seq = list(range(self.size))
def test_random_location(self):
n = random.randrange(0, self.size)
k = bs.search(self.seq, n)
self.assertEqual(n, k)
def test_first_location(self):
n = 0
k = bs.search(self.seq, n)
self.assertEqual(n, k)
def test_last_location(self):
n = self.size - 1
k = bs.search(self.seq, n)
self.assertEqual(n, k)
def test_absence(self):
n = 100
k = bs.search(self.seq, n)
self.assertEqual(k, -1)
def test_empty(self):
k = bs.search([], 0)
self.assertEqual(k, -1)
def tearDown(self):
pass
if __name__ == '__main__':
unittest.main()