-
Notifications
You must be signed in to change notification settings - Fork 145
/
tests.py
64 lines (48 loc) · 2.15 KB
/
tests.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
import pytest
from kmeans import (
point_avg,
distance,
generate_k,
assign_points,
update_centers
)
class TestKMeans(object):
def pytest_funcarg__two_dimensional_points(self, request):
return ((0, 10), (5, 15))
def pytest_funcarg__three_dimensional_points(self, request):
return ((1, 2, 6), (2, 3, 8), (4, 5, 3), (6, 7, 0))
def pytest_funcarg__data_set(self, request):
return ((0, 1), (1, 2), (1, 3), (10, 9), (11, 8), (9, 7))
def pytest_funcarg__assignments(self, request):
return [0, 0, 0, 1, 1, 1]
def pytest_funcarg__centers(self, request):
return [[.67, 2.0], [10.0, 8.0]]
def test_point_avg(self, two_dimensional_points, three_dimensional_points):
assert point_avg(two_dimensional_points) == [2.5, 12.5]
assert point_avg(three_dimensional_points) == [3.25, 4.25, 4.25]
def test_distance(self, two_dimensional_points, three_dimensional_points):
assert distance(*two_dimensional_points) > 7.06
assert distance(*two_dimensional_points) < 7.08
def test_generate_k_two_dimensions(self, two_dimensional_points):
for i in xrange(1000):
centers = generate_k(two_dimensional_points, 10)
for point in centers:
assert 0 < point[0] < 10
assert 5 < point[1] < 15
def test_generate_k_three_dimensions(self, three_dimensional_points):
for i in xrange(1000):
centers = generate_k(three_dimensional_points, 10)
for point in centers:
assert 1 < point[0] < 6
assert 2 < point[1] < 7
assert 0 < point[1] < 8
def test_update_centers(self, data_set, assignments):
centers = update_centers(data_set, assignments)
assert len(centers) == 2
rounded_centers = []
for point in centers:
rounded_centers.append([float('%.2f' % point[0]), float('%.2f' % point[1])])
for point in [[.67, 2.0], [10.0, 8.0]]:
assert point in rounded_centers
def test_assign_points(self, data_set, centers, assignments):
assert assign_points(data_set, centers) == assignments