-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_histograms.py
107 lines (87 loc) · 3.53 KB
/
test_histograms.py
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
106
107
#!python
from __future__ import division, print_function
from histograms import Dictogram, Listogram
import unittest
class DictogramTest(unittest.TestCase):
# test fixtures
text_str = 'one fish two fish red fish blue fish'
text_list = text_str.split()
hist_list = [('one', 1), ('fish', 4), ('two', 1), ('red', 1), ('blue', 1)]
hist_dict = {'one': 1, 'fish': 4, 'two': 1, 'red': 1, 'blue': 1}
def test_items(self):
hist_dict = Dictogram(self.text_list)
assert len(hist_dict) == 5
assert hist_dict == self.hist_dict
self.assertItemsEqual(hist_dict, self.hist_dict)
hist_list = hist_dict.items()
assert len(hist_list) == 5
self.assertItemsEqual(hist_list, self.hist_list)
def test_count(self):
hist_dict = Dictogram(self.text_list)
assert hist_dict.types == 5
assert hist_dict.tokens == 8
assert hist_dict.count('one') == 1
assert hist_dict.count('two') == 1
assert hist_dict.count('red') == 1
assert hist_dict.count('blue') == 1
assert hist_dict.count('fish') == 4
assert hist_dict.count('food') == 0
def test_update(self):
hist_dict = Dictogram(self.text_list)
hist_dict.update(['two', 'blue', 'fish', 'food'])
assert hist_dict.types == 6
assert hist_dict.tokens == 12
assert hist_dict.count('one') == 1
assert hist_dict.count('two') == 2
assert hist_dict.count('red') == 1
assert hist_dict.count('blue') == 2
assert hist_dict.count('fish') == 5
assert hist_dict.count('food') == 1
def test_contains(self):
hist_dict = Dictogram(self.text_list)
for word in self.text_list:
assert word in hist_dict
for word in ['fishy', 'food']:
assert word not in hist_dict
class ListogramTest(unittest.TestCase):
# test fixtures
text_str = 'one fish two fish red fish blue fish'
text_list = text_str.split()
hist_list = [('one', 1), ('fish', 4), ('two', 1), ('red', 1), ('blue', 1)]
hist_dict = {'one': 1, 'fish': 4, 'two': 1, 'red': 1, 'blue': 1}
def test_items(self):
hist_list = Listogram(self.text_list)
assert len(hist_list) == 5
self.assertItemsEqual(hist_list, self.hist_list)
hist_dict = dict(hist_list)
assert len(hist_dict) == 5
self.assertItemsEqual(hist_dict.items(), self.hist_list)
def test_count(self):
hist_list = Listogram(self.text_list)
assert hist_list.types == 5
assert hist_list.tokens == 8
assert hist_list.count('one') == 1
assert hist_list.count('two') == 1
assert hist_list.count('red') == 1
assert hist_list.count('blue') == 1
assert hist_list.count('fish') == 4
assert hist_list.count('food') == 0
def test_update(self):
hist_list = Listogram(self.text_list)
hist_list.update(['two', 'blue', 'fish', 'food'])
assert hist_list.types == 6
assert hist_list.tokens == 12
assert hist_list.count('one') == 1
assert hist_list.count('two') == 2
assert hist_list.count('red') == 1
assert hist_list.count('blue') == 2
assert hist_list.count('fish') == 5
assert hist_list.count('food') == 1
def test_contains(self):
hist_list = Listogram(self.text_list)
for word in self.text_list:
assert word in hist_list
for word in ['fishy', 'food']:
assert word not in hist_list
if __name__ == '__main__':
unittest.main()