-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClosestPairTest.py
79 lines (54 loc) · 2.26 KB
/
ClosestPairTest.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
'''
Created on Nov 3, 2012
@author: naomiaro
'''
import unittest
import math
import ClosestPair as cp
class Test(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def testEclideanDistance(self):
p = (3, 4)
q = (5, 6)
a = math.sqrt(8)
d = cp.euclideanDistance(p, q)
self.assertEqual(d, a)
def testFindClosestPair(self):
P = [(1,1), (3,3), (4,6), (3,5), (4, 3.5)]
pair = cp.findClosestPair(P)
self.assertTrue((3,3) in pair)
self.assertTrue((4, 3.5) in pair)
def testClosestSplitPair(self):
P = [(1,1), (3,3), (11,5), (45,47), (4,6), (3,5), (4, 3.5), (65, 3), (33, 22), (3, 7), (3, 11), (4, 14), (4, 4), (4, 16)]
Px = sorted(P, key=lambda tup: tup[0])
Py = sorted(P, key=lambda tup: tup[1])
pair = cp.closestSplitPair(Px, Py, 2)
self.assertTrue((4,4) in pair)
self.assertTrue((4, 3.5) in pair)
def testClosestPairSplit(self):
P = [(1,1), (3,3), (11,5), (45,47), (4,6), (3,5), (4, 3.5), (65, 3), (33, 22), (3, 7), (3, 11), (4, 14), (4, 4), (4, 16)]
Px = sorted(P, key=lambda tup: tup[0])
Py = sorted(P, key=lambda tup: tup[1])
pair = cp.closestPair(Px, Py)
self.assertTrue((4,4) in pair)
self.assertTrue((4, 3.5) in pair)
def testClosestPairLeft(self):
P = [(4, 3.5), (1,1), (3,3), (11,5), (4, 4), (45,47), (4,6), (3,5), (65, 3), (33, 22), (3, 7), (3, 11), (4, 14), (4, 16)]
Px = sorted(P, key=lambda tup: tup[0])
Py = sorted(P, key=lambda tup: tup[1])
pair = cp.closestPair(Px, Py)
self.assertTrue((4,4) in pair)
self.assertTrue((4, 3.5) in pair)
def testClosestPairRight(self):
P = [(1,1), (3,3), (11,5), (45,47), (4,6), (3,5), (65, 3), (33, 22), (3, 7), (3, 11), (4, 14), (4, 4), (4, 16), (4, 3.5)]
Px = sorted(P, key=lambda tup: tup[0])
Py = sorted(P, key=lambda tup: tup[1])
pair = cp.closestPair(Px, Py)
self.assertTrue((4,4) in pair)
self.assertTrue((4, 3.5) in pair)
if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testName']
unittest.main()