2014-05-09 18:21:31 +01:00
|
|
|
package base
|
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
2014-08-02 16:22:14 +01:00
|
|
|
func isSortedAsc(inst FixedDataGrid, attr AttributeSpec) bool {
|
2014-05-09 18:21:31 +01:00
|
|
|
valPrev := 0.0
|
2014-08-02 16:22:14 +01:00
|
|
|
_, rows := inst.Size()
|
|
|
|
for i := 0; i < rows; i++ {
|
|
|
|
cur := UnpackBytesToFloat(inst.Get(attr, i))
|
2014-05-09 18:21:31 +01:00
|
|
|
if i > 0 {
|
|
|
|
if valPrev > cur {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
valPrev = cur
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-08-02 16:22:14 +01:00
|
|
|
func isSortedDesc(inst FixedDataGrid, attr AttributeSpec) bool {
|
2014-05-09 18:21:31 +01:00
|
|
|
valPrev := 0.0
|
2014-08-02 16:22:14 +01:00
|
|
|
_, rows := inst.Size()
|
|
|
|
for i := 0; i < rows; i++ {
|
|
|
|
cur := UnpackBytesToFloat(inst.Get(attr, i))
|
2014-05-09 18:21:31 +01:00
|
|
|
if i > 0 {
|
|
|
|
if valPrev < cur {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
valPrev = cur
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-08-22 08:13:19 +00:00
|
|
|
func TestSortDesc(t *testing.T) {
|
2014-05-09 18:21:31 +01:00
|
|
|
inst1, err := ParseCSVToInstances("../examples/datasets/iris_headers.csv", true)
|
|
|
|
if err != nil {
|
2014-08-22 08:13:19 +00:00
|
|
|
t.Error(err)
|
2014-05-09 18:21:31 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
inst2, err := ParseCSVToInstances("../examples/datasets/iris_sorted_desc.csv", true)
|
|
|
|
if err != nil {
|
2014-08-22 08:13:19 +00:00
|
|
|
t.Error(err)
|
2014-05-09 18:21:31 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-08-03 12:31:26 +01:00
|
|
|
as1 := ResolveAllAttributes(inst1)
|
|
|
|
as2 := ResolveAllAttributes(inst2)
|
2014-08-02 16:22:14 +01:00
|
|
|
|
|
|
|
if isSortedDesc(inst1, as1[0]) {
|
2014-08-22 08:13:19 +00:00
|
|
|
t.Error("Can't test descending sort order")
|
2014-05-09 18:21:31 +01:00
|
|
|
}
|
2014-08-02 16:22:14 +01:00
|
|
|
if !isSortedDesc(inst2, as2[0]) {
|
2014-08-22 08:13:19 +00:00
|
|
|
t.Error("Reference data not sorted in descending order!")
|
2014-05-09 18:21:31 +01:00
|
|
|
}
|
2014-08-02 16:22:14 +01:00
|
|
|
|
|
|
|
Sort(inst1, Descending, as1[0:len(as1)-1])
|
|
|
|
if err != nil {
|
2014-08-22 08:13:19 +00:00
|
|
|
t.Error(err)
|
2014-08-02 16:22:14 +01:00
|
|
|
}
|
|
|
|
if !isSortedDesc(inst1, as1[0]) {
|
2014-08-22 08:13:19 +00:00
|
|
|
t.Error("Instances are not sorted in descending order")
|
|
|
|
t.Error(inst1)
|
2014-05-09 18:21:31 +01:00
|
|
|
}
|
|
|
|
if !inst2.Equal(inst1) {
|
2014-08-22 08:13:19 +00:00
|
|
|
t.Error("Instances don't match")
|
|
|
|
t.Error(inst1)
|
|
|
|
t.Error(inst2)
|
2014-05-09 18:21:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-22 08:13:19 +00:00
|
|
|
func TestSortAsc(t *testing.T) {
|
2014-05-09 18:21:31 +01:00
|
|
|
inst, err := ParseCSVToInstances("../examples/datasets/iris_headers.csv", true)
|
2014-08-03 12:31:26 +01:00
|
|
|
as1 := ResolveAllAttributes(inst)
|
2014-08-02 16:22:14 +01:00
|
|
|
if isSortedAsc(inst, as1[0]) {
|
2014-08-22 08:13:19 +00:00
|
|
|
t.Error("Can't test ascending sort on something ascending already")
|
2014-05-09 18:21:31 +01:00
|
|
|
}
|
|
|
|
if err != nil {
|
2014-08-22 08:13:19 +00:00
|
|
|
t.Error(err)
|
2014-05-09 18:21:31 +01:00
|
|
|
return
|
|
|
|
}
|
2014-08-02 16:22:14 +01:00
|
|
|
Sort(inst, Ascending, as1[0:1])
|
|
|
|
if !isSortedAsc(inst, as1[0]) {
|
2014-08-22 08:13:19 +00:00
|
|
|
t.Error("Instances are not sorted in ascending order")
|
|
|
|
t.Error(inst)
|
2014-05-09 18:21:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inst2, err := ParseCSVToInstances("../examples/datasets/iris_sorted_asc.csv", true)
|
|
|
|
if err != nil {
|
2014-08-22 08:13:19 +00:00
|
|
|
t.Error(err)
|
2014-05-09 18:21:31 +01:00
|
|
|
return
|
|
|
|
}
|
2014-08-03 12:31:26 +01:00
|
|
|
as2 := ResolveAllAttributes(inst2)
|
2014-08-02 16:22:14 +01:00
|
|
|
if !isSortedAsc(inst2, as2[0]) {
|
2014-08-22 08:13:19 +00:00
|
|
|
t.Error("This file should be sorted in ascending order")
|
2014-05-09 18:21:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if !inst2.Equal(inst) {
|
2014-08-22 08:13:19 +00:00
|
|
|
t.Error("Instances don't match")
|
|
|
|
t.Error(inst)
|
|
|
|
t.Error(inst2)
|
2014-05-09 18:21:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|