-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdbscan_test.go
128 lines (112 loc) · 2.75 KB
/
dbscan_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package dbscan
import (
"fmt"
"log"
"math"
"testing"
)
type SimpleClusterable struct {
position float64
}
func (s SimpleClusterable) Distance(c interface{}) float64 {
distance := math.Abs(c.(SimpleClusterable).position - s.position)
return distance
}
func (s SimpleClusterable) GetID() string {
return fmt.Sprint(s.position)
}
func TestPutAll(t *testing.T) {
testMap := make(map[string]Clusterable)
clusterList := []Clusterable{
SimpleClusterable{10},
SimpleClusterable{12},
}
putAll(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 := []Clusterable{
SimpleClusterable{0},
SimpleClusterable{1},
SimpleClusterable{-1},
SimpleClusterable{1.5},
SimpleClusterable{-0.5},
}
eps := 1.0
neighbours := findNeighbours(clusterList[0], clusterList, eps)
assertEquals(t, 3, len(neighbours))
}
func TestMerge(t *testing.T) {
log.Println("Executing TestMerge")
expected := 6
one := []Clusterable{
SimpleClusterable{0},
SimpleClusterable{1},
SimpleClusterable{2.1},
SimpleClusterable{2.2},
SimpleClusterable{2.3},
}
two := []Clusterable{
one[0],
one[1],
SimpleClusterable{2.4},
}
output := merge(one, two)
assertEquals(t, expected, len(output))
}
func TestExpandCluster(t *testing.T) {
log.Println("Executing TestExpandCluster")
expected := 4
clusterList := []Clusterable{
SimpleClusterable{0},
SimpleClusterable{1},
SimpleClusterable{2},
SimpleClusterable{2.1},
SimpleClusterable{5},
}
eps := 1.0
minPts := 3
visitMap := make(map[string]bool)
cluster := make(Cluster, 0)
cluster = expandCluster(cluster, clusterList, visitMap, minPts, eps)
assertEquals(t, expected, len(cluster))
}
func TestClusterize(t *testing.T) {
log.Println("Executing TestClusterize")
clusterList := []Clusterable{
SimpleClusterable{1},
SimpleClusterable{0.5},
SimpleClusterable{0},
SimpleClusterable{5},
SimpleClusterable{4.5},
SimpleClusterable{4},
}
eps := 1.0
minPts := 2
clusters := Clusterize(clusterList, minPts, eps)
assertEquals(t, 2, len(clusters))
if 2 == len(clusters) {
assertEquals(t, 3, len(clusters[0]))
assertEquals(t, 3, len(clusters[1]))
}
}
func TestClusterizeNoData(t *testing.T) {
log.Println("Executing TestClusterizeNoData")
clusterList := []Clusterable{}
eps := 1.0
minPts := 3
clusters := Clusterize(clusterList, minPts, eps)
assertEquals(t, 0, len(clusters))
}
//Assert function. If the expected value not equals result, function
//returns error.
func assertEquals(t *testing.T, expected, result int) {
if expected != result {
t.Errorf("Expected %d but got %d", expected, result)
}
}