Completed
Branch master (5cc02c)
by Andrei
01:30
created

pyclustering.utils.tests.Test   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 37
Duplicated Lines 0 %
Metric Value
dl 0
loc 37
rs 10
wmc 17
1
"""!
2
3
Unit-tests for utils module.
4
5
Copyright (C) 2015    Andrei Novikov ([email protected])
6
7
pyclustering is free software: you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation, either version 3 of the License, or
10
(at your option) any later version.
11
12
pyclustering is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
GNU General Public License for more details.
16
17
You should have received a copy of the GNU General Public License
18
along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20
"""
21
22
import unittest;
23
24
from pyclustering.utils import euclidean_distance;
25
from pyclustering.utils import average_neighbor_distance;
26
27
class Test(unittest.TestCase):
28
29
    def testEuclideanDistance(self):
30
        point1 = [1, 2];
31
        point2 = [1, 3];
32
        point3 = [4, 6];
33
        
34
        # Tests for euclidean_distance
35
        assert euclidean_distance(point1, point2) == 1;
36
        assert euclidean_distance(point1, point1) == 0;
37
        assert euclidean_distance(point1, point3) == 5;
38
39
    
40
    def testFloatEuclideanDistance(self):
41
        assert euclidean_distance(0.5, 1.5) == 1;
42
        assert self.float_comparasion(euclidean_distance(1.6, 1.4), 0.2);
43
        assert self.float_comparasion(euclidean_distance(4.23, 2.14), 2.09);
44
    
45
    
46
    def testAverageNeighborFourDistance(self):
47
        points = [[0.0, 0.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0]];
48
        
49
        assert average_neighbor_distance(points, 1) == 1.0;
50
        assert average_neighbor_distance(points, 2) == 1.0;
51
        assert self.float_comparasion(average_neighbor_distance(points, 3), 1.1381);
52
53
54
    def testAverageNeighborFourDistanceNegativeValues(self):
55
        points = [[0.0, 0.0], [0.0, -1.0], [-1.0, -1.0], [-1.0, 0.0]];
56
        
57
        assert average_neighbor_distance(points, 1) == 1.0;
58
        assert average_neighbor_distance(points, 2) == 1.0;
59
        assert self.float_comparasion(average_neighbor_distance(points, 3), 1.1381);
60
    
61
    
62
    def float_comparasion(self, float1, float2, eps = 0.0001):
63
        return ( (float1 + eps) > float2 and (float1 - eps) < float2 );
64
        
65
        
66
67
if __name__ == "__main__":
68
    unittest.main();