-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_test.py
More file actions
105 lines (80 loc) · 3.1 KB
/
utils_test.py
File metadata and controls
105 lines (80 loc) · 3.1 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
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
104
105
import unittest
import random
from utils import *
class GraphTest(unittest.TestCase):
def test_basic(self):
G = UndirectedGraph()
self.assertEqual(G.vertices(), [])
vertices = [chr(i) for i in range(ord('a'), ord('z') + 1)]
for ch in vertices:
G.add_vertex(ch)
for vert in G.vertices():
self.assertTrue(vert in vertices)
G.add_edge('a', 'b')
G.add_edge('c', 'a')
G.add_edge('b', 'c')
G.add_edge('b', 'd')
a_neighbors = ['b', 'c']
count = 0
for vert in G.neighbors('a'):
count += 1
self.assertTrue(vert in a_neighbors)
self.assertEqual(count, len(a_neighbors))
def test_big_graph(self):
G = UndirectedGraph()
for i in range(ord('a'), ord('z') + 1):
G.add_vertex(chr(i))
for _ in range(5000):
G.add_vertex(chr(random.randint(ord('a'), ord('z'))))
for _ in range(5000):
rand1 = random.randint(ord('a'), ord('z'))
rand2 = random.randint(ord('a'), ord('z'))
G.add_edge(rand1, rand2)
class MedianTest(unittest.TestCase):
def test_basic(self):
M = MedianFinder()
self.assertEqual(M.med(), None)
M.add(1)
self.assertEqual(M.med(), 1)
nums = [1]
for _ in range(100):
nums.append(random.randint(-100, 100))
M.add(nums[-1])
nums.sort()
self.assertEqual(M.med(), nums[(len(nums)-1) // 2])
nums.append(random.randint(-100, 100))
M.add(nums[-1])
nums.sort()
self.assertEqual(M.med(), nums[(len(nums)-1) // 2])
class LocationTest(unittest.TestCase):
def test_dist(self):
place1 = Location(37.789216, -122.401476) # Montgomery Bart
place2 = Location(37.784020, -122.408071) # Powell Bart
self.assertTrue(Location.dist(place1, place2) < 1)
place1 = Location(37.871692, -122.259381) # Campanile
place2 = Location(37.778971, -122.419160) # SF City Hall
self.assertTrue(Location.dist(place1, place2) > 17)
self.assertTrue(Location.dist(place1, place2) < 18)
place1 = Location(37.778971, -122.419160) # SF City Hall
place2 = Location(38.897675, -77.036592) # White House
self.assertTrue(Location.dist(place1, place2) > 3900)
self.assertTrue(Location.dist(place1, place2) < 4000)
def test_avg(self):
locs = [Location(0, 0), Location(1, 2), Location(2, 4)]
self.assertEqual(Location(1, 2), Location.avg(locs))
class CounterTest(unittest.TestCase):
def test_basic(self):
counter = Counter()
items = []
for _ in range(10):
items.append('a')
for _ in range(20):
items.append('b')
counter.add_counts(items)
self.assertEqual(10, counter.get_count('a'))
self.assertEqual(20, counter.get_count('b'))
counter.add_counts(items)
self.assertEqual(20, counter.get_count('a'))
self.assertEqual(40, counter.get_count('b'))
if __name__ == '__main__':
unittest.main()