Skip to content

Commit 5ccd132

Browse files
committed
Fixed test case to accomodate unordered lists
git-svn-id: https://python-cluster.svn.sourceforge.net/svnroot/python-cluster/trunk@2 57eab859-f816-0410-af72-e61ffa1cc713
1 parent 7209277 commit 5ccd132

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

clusterTests.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,27 @@
1919
from difflib import SequenceMatcher
2020
import unittest
2121

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+
2243
class HClusterSmallListTestCase(unittest.TestCase):
2344
" Test for Bug #1516204 "
2445

@@ -118,14 +139,17 @@ def testUniformLength(self):
118139
self.assertRaises(ValueError, KMeansClustering, data)
119140

120141
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)]
123145
cl = KMeansClustering(data)
124146
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))
129153

130154
def testClustering(self):
131155
"Basic clustering test"

0 commit comments

Comments
 (0)