forked from sjwhitworth/golearn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
111 lines (102 loc) · 3.65 KB
/
util_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
package base
import (
. "github.com/smartystreets/goconvey/convey"
"testing"
)
func TestClassDistributionAfterSplit(t *testing.T) {
Convey("Given the PlayTennis dataset", t, func() {
inst, err := ParseCSVToInstances("../examples/datasets/tennis.csv", true)
So(err, ShouldEqual, nil)
Convey("Splitting on Sunny should give the right result...", func() {
result := GetClassDistributionAfterSplit(inst, inst.AllAttributes()[0])
So(result["sunny"]["no"], ShouldEqual, 3)
So(result["sunny"]["yes"], ShouldEqual, 2)
So(result["overcast"]["yes"], ShouldEqual, 4)
So(result["rainy"]["yes"], ShouldEqual, 3)
So(result["rainy"]["no"], ShouldEqual, 2)
})
})
}
func TestPackAndUnpack(t *testing.T) {
Convey("Given some uint64", t, func() {
x := uint64(0xDEADBEEF)
Convey("When the integer is packed", func() {
packed := PackU64ToBytes(x)
Convey("And then unpacked", func() {
unpacked := UnpackBytesToU64(packed)
Convey("The unpacked version should be the same", func() {
So(x, ShouldEqual, unpacked)
})
})
})
})
Convey("Given another uint64", t, func() {
x := uint64(1)
Convey("When the integer is packed", func() {
packed := PackU64ToBytes(x)
Convey("And then unpacked", func() {
unpacked := UnpackBytesToU64(packed)
Convey("The unpacked version should be the same", func() {
So(x, ShouldEqual, unpacked)
})
})
})
})
}
func TestPackAndUnpackFloat(t *testing.T) {
Convey("Given some float", t, func() {
x := 1.2011
Convey("When the float gets packed", func() {
packed := PackFloatToBytes(x)
Convey("And then unpacked", func() {
unpacked := UnpackBytesToFloat(packed)
Convey("The unpacked version should be the same", func() {
So(unpacked, ShouldEqual, x)
})
})
})
})
}
func TestStrictlyCompatable(t *testing.T) {
Convey("Given two datasets...", t, func() {
Convey("Given two identical datasets", func() {
// Violates the requirement that both CategoricalAttributes
// must have values in the same order
d1, err := ParseCSVToInstances("../examples/datasets/exam.csv", true)
So(err, ShouldEqual, nil)
d2, err := ParseCSVToInstances("../examples/datasets/exams.csv", true)
So(err, ShouldEqual, nil)
So(CheckStrictlyCompatible(d1, d2), ShouldEqual, true)
})
Convey("Given two identical datasets (apart from sorting)", func() {
// Violates the requirement that both CategoricalAttributes
// must have values in the same order
d1, err := ParseCSVToInstances("../examples/datasets/iris_sorted_asc.csv", true)
So(err, ShouldEqual, nil)
d2, err := ParseCSVToInstances("../examples/datasets/iris_sorted_desc.csv", true)
So(err, ShouldEqual, nil)
So(CheckStrictlyCompatible(d1, d2), ShouldEqual, false)
})
Convey("Given two different datasets...", func() {
// Violates verything
d1, err := ParseCSVToInstances("../examples/datasets/tennis.csv", true)
So(err, ShouldEqual, nil)
d2, err := ParseCSVToInstances("../examples/datasets/iris_sorted_desc.csv", true)
So(err, ShouldEqual, nil)
So(CheckStrictlyCompatible(d1, d2), ShouldEqual, false)
})
})
}
func TestCategoricalEquality(t *testing.T) {
Convey("Given two outwardly identical class Attributes...", t, func() {
d1, err := ParseCSVToInstances("../examples/datasets/iris_sorted_asc.csv", true)
So(err, ShouldEqual, nil)
d2, err := ParseCSVToInstances("../examples/datasets/iris_sorted_desc.csv", true)
So(err, ShouldEqual, nil)
c1 := d1.AllClassAttributes()[0]
c2 := d2.AllClassAttributes()[0]
So(c1.GetName(), ShouldEqual, c2.GetName())
So(c1.Equals(c2), ShouldBeFalse)
So(c2.Equals(c1), ShouldBeFalse) // Violates the fact that Attributes must appear in the same order
})
}