-
Notifications
You must be signed in to change notification settings - Fork 0
/
cubes_test.go
114 lines (105 loc) · 3.59 KB
/
cubes_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
package hyper
import (
"reflect"
"testing"
)
func centralIsNotInTheSet(set Cubes, central Cube) bool {
for _, cube := range set {
counter := 0
for i, c := range central {
if cube[i] == c {
counter++
}
}
if counter == len(central) {
return false
}
}
return true
}
func TestRescale(t *testing.T) { // Testing panic.
vector := []float64{25.5, 0.01, 210.3, 93.9, 6.6, 9.1, 255.0}
params := Params{0.0, 255.0, 0.25, 10}
rescaled := rescale(vector, params)
got := rescaled
want := []float64{
1, 0.0003921568627450981, 8.24705882352941,
3.6823529411764704, 0.25882352941176473,
0.3568627450980392, 10}
if !reflect.DeepEqual(got, want) {
t.Errorf(`Got %v, want %v.`, got, want)
}
}
func TestCubeSet1(t *testing.T) { // Testing panic.
defer func() { recover() }()
// Intentionally forbiden value for epsPercent.
values := []float64{25.5, 0.01, 210.3, 93.9, 6.6, 9.1, 254.9}
params := Params{0.0, 255.0, 0.51, 10}
_ = CubeSet(values, params)
// Never reaches here if Params panics.
t.Errorf("Params did not panic on epsPercent > 0.5")
}
func TestCubeSet2(t *testing.T) {
params := Params{0.0, 255.0, 0.25, 10}
values := []float64{25.5, 0.01, 210.3, 93.9, 6.6, 9.1, 254.9}
gotCubes := CubeSet(values, params)
gotCentral := CentralCube(values, params)
wantCubes := Cubes{{0, 0, 7, 3, 0, 0, 9}, {1, 0, 7, 3, 0, 0, 9},
{0, 0, 8, 3, 0, 0, 9}, {1, 0, 8, 3, 0, 0, 9}}
wantCentral := Cube{1, 0, 8, 3, 0, 0, 9}
if !reflect.DeepEqual(gotCubes, wantCubes) {
t.Errorf(`Got %v, want %v.`, gotCubes, wantCubes)
}
if !reflect.DeepEqual(gotCentral, wantCentral) {
t.Errorf(`Got %v, want %v.`, gotCentral, wantCentral)
}
if centralIsNotInTheSet(gotCubes, gotCentral) {
t.Errorf(`Central %v is not in the set %v.`, gotCentral, gotCubes)
}
}
// Testing bucket borders.
func TestCubeSet3(t *testing.T) {
params := Params{0.0, 4.0, 0.25, 4}
values := []float64{0.01, 2 * 0.999, 2 * 1.001}
gotCubes := CubeSet(values, params)
gotCentral := CentralCube(values, params)
wantCubes := Cubes{{0, 1, 1}, {0, 2, 1}, {0, 1, 2}, {0, 2, 2}}
wantCentral := Cube{0, 1, 2}
if !reflect.DeepEqual(gotCubes, wantCubes) {
t.Errorf(`Got %v, want %v.`, gotCubes, wantCubes)
}
if !reflect.DeepEqual(gotCentral, wantCentral) {
t.Errorf(`Got %v, want %v.`, gotCentral, wantCentral)
}
if centralIsNotInTheSet(gotCubes, wantCentral) {
t.Errorf(`Central %v is not in the set %v.`, gotCentral, gotCubes)
}
}
// Testing extreme buckets.
func TestCubeSet4(t *testing.T) {
values := []float64{255.0, 0.0, 255.0, 0.0, 255.0, 0.0, 255.0}
params := Params{0.0, 255.0, 0.25, 4}
gotCubes := CubeSet(values, params)
wantCubes := Cubes{{3, 0, 3, 0, 3, 0, 3}}
if !reflect.DeepEqual(gotCubes, wantCubes) {
t.Errorf(`Got %v, want %v.`, gotCubes, wantCubes)
}
}
var vector = []float64{
0, 183, 148, 21, 47, 16, 69, 45, 151, 64, 181}
func TestCubeSet5(t *testing.T) {
params := Params{0.0, 255.0, 0.25, 4}
gotCubes := CubeSet(vector, params)
wantCubes := Cubes{
{0, 2, 2, 0, 0, 0, 0, 0, 2, 0, 2}, {0, 3, 2, 0, 0, 0, 0, 0, 2, 0, 2},
{0, 2, 2, 0, 0, 0, 1, 0, 2, 0, 2}, {0, 3, 2, 0, 0, 0, 1, 0, 2, 0, 2},
{0, 2, 2, 0, 0, 0, 0, 0, 2, 1, 2}, {0, 3, 2, 0, 0, 0, 0, 0, 2, 1, 2},
{0, 2, 2, 0, 0, 0, 1, 0, 2, 1, 2}, {0, 3, 2, 0, 0, 0, 1, 0, 2, 1, 2},
{0, 2, 2, 0, 0, 0, 0, 0, 2, 0, 3}, {0, 3, 2, 0, 0, 0, 0, 0, 2, 0, 3},
{0, 2, 2, 0, 0, 0, 1, 0, 2, 0, 3}, {0, 3, 2, 0, 0, 0, 1, 0, 2, 0, 3},
{0, 2, 2, 0, 0, 0, 0, 0, 2, 1, 3}, {0, 3, 2, 0, 0, 0, 0, 0, 2, 1, 3},
{0, 2, 2, 0, 0, 0, 1, 0, 2, 1, 3}, {0, 3, 2, 0, 0, 0, 1, 0, 2, 1, 3}}
if !reflect.DeepEqual(gotCubes, wantCubes) {
t.Errorf(`Got %v, want %v.`, gotCubes, wantCubes)
}
}