forked from sohlich/go-dbscan
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcluster_test.go
98 lines (82 loc) · 2 KB
/
cluster_test.go
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (c) Roman Atachiants and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
package dbscan
import (
"fmt"
"log"
"math"
"testing"
"github.com/stretchr/testify/assert"
)
type SimplePoint struct {
position float64
}
func (s SimplePoint) DistanceTo(c Point) float64 {
distance := math.Abs(c.(SimplePoint).position - s.position)
return distance
}
func (s SimplePoint) Name() string {
return fmt.Sprint(s.position)
}
func TestPutAll(t *testing.T) {
testMap := make(map[string]Point)
clusterList := []Point{
SimplePoint{10},
SimplePoint{12},
}
merge(testMap, clusterList...)
mapSize := len(testMap)
if mapSize != 2 {
t.Errorf("Map does not contain expected size 2 but was %d", mapSize)
}
}
//Test find neighbour function
func TestFindNeighbours(t *testing.T) {
log.Println("Executing TestFindNeighbours")
clusterList := []Point{
SimplePoint{0},
SimplePoint{1},
SimplePoint{-1},
SimplePoint{1.5},
SimplePoint{-0.5},
}
eps := 1.01
neighbours := findNeighbours(clusterList[0], clusterList, eps)
assert.Equal(t, 3, len(neighbours))
}
func TestExpandCluster(t *testing.T) {
log.Println("Executing TestExpandCluster")
expected := 4
clusterList := []Point{
SimplePoint{0},
SimplePoint{1},
SimplePoint{2},
SimplePoint{2.1},
SimplePoint{5},
}
eps := 1.0
minPts := 3
visitMap := make(map[string]bool)
cluster := make([]Point, 0)
cluster = expandCluster(cluster, clusterList, visitMap, minPts, eps)
assert.Equal(t, expected, len(cluster))
}
func TestCluster(t *testing.T) {
clusters := Cluster(2, 1.0,
SimplePoint{1},
SimplePoint{0.5},
SimplePoint{0},
SimplePoint{5},
SimplePoint{4.5},
SimplePoint{4})
assert.Equal(t, 2, len(clusters))
if 2 == len(clusters) {
assert.Equal(t, 3, len(clusters[0]))
assert.Equal(t, 3, len(clusters[1]))
}
}
func TestClusterNoData(t *testing.T) {
log.Println("Executing TestClusterNoData")
clusters := Cluster(3, 1.0)
assert.Equal(t, 0, len(clusters))
}