Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f544a67

Browse files
author
Eric Naeseth
committed
Adding some unit tests.
1 parent 5739fb2 commit f544a67

File tree

1 file changed

+209
-0
lines changed

1 file changed

+209
-0
lines changed

test.py

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
"""
4+
Testing code for the FP-growth implementation.
5+
"""
6+
7+
import unittest
8+
import fp_growth
9+
from itertools import izip
10+
from pprint import pprint
11+
12+
class NodeTester(object):
13+
def __init__(self, case, node):
14+
self.case = case
15+
self.node = node
16+
17+
def child(self, item, count=None):
18+
c = self.node.search(item)
19+
self.case.failIf(c is None, 'no child with item %s' % item)
20+
21+
tester = NodeTester(self.case, c)
22+
if count is not None:
23+
tester.count(count)
24+
return tester
25+
26+
def count(self, count):
27+
self.case.assertEqual(self.node.count, count,
28+
'expected count to be %d; instead it was %d' %
29+
(count, self.node.count))
30+
return self
31+
32+
def leaf(self):
33+
self.case.assertTrue(self.node.leaf, 'node must be a leaf')
34+
return self
35+
36+
class TreeTestCase(unittest.TestCase):
37+
def setUp(self):
38+
self.tree = fp_growth.FPTree()
39+
self.root = NodeTester(self, self.tree.root)
40+
41+
def nodes(self, item):
42+
return list(self.tree.nodes(item))
43+
44+
def assertPathsEqual(self, expected, actual):
45+
actual = list(actual)
46+
self.assertEqual(len(expected), len(actual))
47+
48+
for items, path in izip(expected, actual):
49+
self.assertEqual(len(items), len(path))
50+
for item, node in izip(items, path):
51+
self.assertEqual(item, node.item)
52+
53+
class InsertionTests(TreeTestCase):
54+
def testOneBranch(self):
55+
self.tree.add('abc')
56+
self.root.child('a', 1).child('b', 1).child('c', 1)
57+
58+
def testIndependentBranches(self):
59+
self.tree.add('abc')
60+
self.tree.add('def')
61+
62+
self.root.child('a', 1).child('b', 1).child('c', 1)
63+
self.root.child('d', 1).child('e', 1).child('f', 1)
64+
65+
def testCommonPrefix(self):
66+
self.tree.add('abcd')
67+
self.tree.add('abde')
68+
69+
b = self.root.child('a', 2).child('b', 2)
70+
b.child('c', 1).child('d', 1)
71+
b.child('d', 1).child('e', 1)
72+
73+
class RouteTests(TreeTestCase):
74+
def testRoutes(self):
75+
self.tree.add('abc')
76+
self.tree.add('bcd')
77+
self.tree.add('cde')
78+
79+
self.assertEqual(1, len(self.nodes('a')))
80+
self.assertEqual(2, len(self.nodes('b')))
81+
self.assertEqual(3, len(self.nodes('c')))
82+
self.assertEqual(2, len(self.nodes('d')))
83+
self.assertEqual(1, len(self.nodes('e')))
84+
85+
def testRemoveOnly(self):
86+
self.tree.add('ab')
87+
88+
self.assertEqual(1, len(self.nodes('b')))
89+
b = self.root.child('a').child('b').node
90+
self.assertTrue(self.root.child('a').node is b.parent)
91+
b.parent.remove(b)
92+
self.assertTrue(b.parent is None)
93+
self.assertEqual(0, len(self.nodes('b')))
94+
95+
def testNeighbors(self):
96+
self.tree.add('abc')
97+
self.tree.add('bcd')
98+
self.tree.add('cde')
99+
100+
left_c = self.root.child('a').child('b').child('c').node
101+
middle_c = self.root.child('b').child('c').node
102+
right_c = self.root.child('c').node
103+
104+
self.assertTrue(left_c.neighbor is middle_c)
105+
self.assertTrue(middle_c.neighbor is right_c)
106+
107+
def testRemoveMiddle(self):
108+
self.tree.add('abc')
109+
self.tree.add('bcd')
110+
self.tree.add('cde')
111+
112+
left_c = self.root.child('a').child('b').child('c').node
113+
middle_c = self.root.child('b').child('c').node
114+
right_c = self.root.child('c').node
115+
116+
self.assertEqual(3, len(self.nodes('c')))
117+
118+
middle_c.parent.remove(middle_c)
119+
self.assertEqual(2, len(self.nodes('c')))
120+
self.assertTrue(left_c.neighbor is right_c)
121+
122+
def testRemoveEnd(self):
123+
self.tree.add('abc')
124+
self.tree.add('bcd')
125+
self.tree.add('cde')
126+
127+
left_c = self.root.child('a').child('b').child('c').node
128+
middle_c = self.root.child('b').child('c').node
129+
right_c = self.root.child('c').node
130+
131+
self.assertEqual(3, len(self.nodes('c')))
132+
133+
right_c.parent.remove(right_c)
134+
self.assertEqual(2, len(self.nodes('c')))
135+
self.assertTrue(left_c.neighbor is middle_c)
136+
self.assertTrue(middle_c.neighbor is None)
137+
138+
class PrefixPathTests(TreeTestCase):
139+
def testPaths(self):
140+
self.tree.add('abc')
141+
self.tree.add('bcd')
142+
self.tree.add('cde')
143+
144+
self.assertPathsEqual(['abc', 'bc', 'c'], self.tree.prefix_paths('c'))
145+
146+
class RemovalTests(TreeTestCase):
147+
def testLeafRemoval(self):
148+
self.tree.add('abc')
149+
c = self.root.child('a').child('b').child('c').node
150+
b = c.parent
151+
b.remove(c)
152+
self.assertTrue(c.leaf)
153+
154+
def testMiddleRemoval(self):
155+
self.tree.add('abc')
156+
c = self.root.child('a').child('b').child('c').node
157+
b = c.parent
158+
a = b.parent
159+
160+
a.remove(b)
161+
self.failIf(a.leaf, "the 'a' node should not be a leaf")
162+
self.assertTrue(c.parent is a,
163+
"the 'c' node should now be a child of 'a'")
164+
165+
def testMerging(self):
166+
self.tree.add('abc')
167+
self.tree.add('ac')
168+
169+
b = self.root.child('a').child('b').node
170+
a = b.parent
171+
a.remove(b)
172+
self.root.child('a').child('c', 2)
173+
174+
class ConditionalTreeTests(TreeTestCase):
175+
def testGeneration(self):
176+
self.tree.add('abc')
177+
self.tree.add('abd')
178+
self.tree.add('ade')
179+
180+
# Test the tree's structure, just because I'm paranoid.
181+
b = self.root.child('a', 3).child('b', 2)
182+
b.child('c', 1)
183+
b.child('d', 1)
184+
self.root.child('a').child('d', 1).child('e', 1)
185+
186+
paths = list(self.tree.prefix_paths('d'))
187+
ct = fp_growth.conditional_tree_from_paths(paths, 1)
188+
root = NodeTester(self, ct.root)
189+
190+
a = root.child('a', 2)
191+
a.child('b', 1).leaf()
192+
self.assertEqual(1, len(a.node.children))
193+
194+
def testPruning(self):
195+
self.tree.add('abc')
196+
self.tree.add('abd')
197+
self.tree.add('ac')
198+
self.tree.add('abe')
199+
self.tree.add('dc')
200+
201+
paths = list(self.tree.prefix_paths('c'))
202+
ct = fp_growth.conditional_tree_from_paths(paths, 2)
203+
root = NodeTester(self, ct.root)
204+
205+
root.child('a', 2).leaf()
206+
self.assertEqual(1, len(root.node.children))
207+
208+
if __name__ == '__main__':
209+
unittest.main()

0 commit comments

Comments
 (0)