|
19 | 19 | from difflib import SequenceMatcher |
20 | 20 | import unittest |
21 | 21 |
|
| 22 | +def compare_list(x, y): |
| 23 | + """ |
| 24 | + Compare lists by content. Ordering does not matter. |
| 25 | + Returns True if both lists contain the same items (and are of identical |
| 26 | + length) |
| 27 | + """ |
| 28 | + |
| 29 | + cmpx = [set(cluster) for cluster in x] |
| 30 | + cmpy = [set(cluster) for cluster in y] |
| 31 | + |
| 32 | + all_ok = True |
| 33 | + |
| 34 | + for cset in cmpx: |
| 35 | + all_ok &= cset in cmpy |
| 36 | + |
| 37 | + for cset in cmpy: |
| 38 | + all_ok &= cset in cmpx |
| 39 | + |
| 40 | + return all_ok |
| 41 | + |
| 42 | + |
22 | 43 | class HClusterSmallListTestCase(unittest.TestCase): |
23 | 44 | " Test for Bug #1516204 " |
24 | 45 |
|
@@ -118,14 +139,17 @@ def testUniformLength(self): |
118 | 139 | self.assertRaises(ValueError, KMeansClustering, data) |
119 | 140 |
|
120 | 141 | def testPointDoubling(self): |
121 | | - "test for bug ?" |
122 | | - data = [ (0,0), (7,3), (2,6), (3,5), (3,6), (0,0), (8,1), (3,4), (8,3), (9,2), (2,5), (9,3) ] |
| 142 | + "test for bug #1604868" |
| 143 | + data = [ (18,13), (15, 12), (17,12), (18,12), (19,12), (16,11), (18, 11), |
| 144 | + (19,10), (0,0), (1, 4), (1,2), (2,3), (4,1), (4,3), (5,2), (6,1)] |
123 | 145 | cl = KMeansClustering(data) |
124 | 146 | clusters = cl.getclusters(2) |
125 | | - self.assertEqual( |
126 | | - cl.getclusters(2), |
127 | | - [[(0, 0), (2, 6), (3, 6), (2, 5), (3, 5), (3, 4), (1, 5)], |
128 | | - [(7, 3), (9, 2), (9, 3), (8, 1), (8, 3)]]) |
| 147 | + expected = [[(18,13), (15, 12), (17,12), (18,12), (19,12), (16,11), (18, 11), (19,10)], |
| 148 | + [(0,0), (1, 4), (1,2), (2,3), (4,1), (5,2), (6,1), (4,3)]] |
| 149 | + self.assertTrue( compare_list( |
| 150 | + clusters, |
| 151 | + expected ), |
| 152 | + "Elements differ!\n%s\n%s" % (clusters, expected)) |
129 | 153 |
|
130 | 154 | def testClustering(self): |
131 | 155 | "Basic clustering test" |
|
0 commit comments