Skip to content

Commit

Permalink
Convert some tests to goconvey, and improve assertions along the way
Browse files Browse the repository at this point in the history
  • Loading branch information
amitkgupta committed Aug 22, 2014
1 parent 1809a8b commit 9f67f73
Show file tree
Hide file tree
Showing 15 changed files with 377 additions and 412 deletions.
9 changes: 4 additions & 5 deletions base/bag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
)

func TestBAGSimple(t *testing.T) {

Convey("Given certain bit data", t, func() {
// Generate said bits
bVals := [][]byte{
Expand All @@ -28,14 +27,14 @@ func TestBAGSimple(t *testing.T) {
attrSpecs := make([]AttributeSpec, 3)
for _, a := range attrSpecsUnordered {
name := a.GetAttribute().GetName()
So(name, ShouldBeIn, []string{"0", "1", "2"})

if name == "0" {
attrSpecs[0] = a
} else if name == "1" {
attrSpecs[1] = a
} else if name == "2" {
attrSpecs[2] = a
} else {
t.Fatalf("Unexpected attribute name '%s'", name)
}
}

Expand Down Expand Up @@ -95,14 +94,14 @@ func TestBAG(t *testing.T) {
attrSpecs := make([]AttributeSpec, 3)
for _, a := range attrSpecsUnordered {
name := a.GetAttribute().GetName()
So(name, ShouldBeIn, []string{"0", "1", "2"})

if name == "0" {
attrSpecs[0] = a
} else if name == "1" {
attrSpecs[1] = a
} else if name == "2" {
attrSpecs[2] = a
} else {
t.Fatalf("Unexpected attribute name '%s'", name)
}
}

Expand Down
134 changes: 60 additions & 74 deletions base/lazy_sort_test.go
Original file line number Diff line number Diff line change
@@ -1,86 +1,72 @@
package base

import (
. "github.com/smartystreets/goconvey/convey"
"testing"
)

func TestLazySortDesc(t *testing.T) {
inst1, err := ParseCSVToInstances("../examples/datasets/iris_headers.csv", true)
if err != nil {
t.Error(err)
return
}
inst2, err := ParseCSVToInstances("../examples/datasets/iris_sorted_desc.csv", true)
if err != nil {
t.Error(err)
return
}

as1 := ResolveAllAttributes(inst1)
as2 := ResolveAllAttributes(inst2)

if isSortedDesc(inst1, as1[0]) {
t.Error("Can't test descending sort order")
}
if !isSortedDesc(inst2, as2[0]) {
t.Error("Reference data not sorted in descending order!")
}

inst, err := LazySort(inst1, Descending, as1[0:len(as1)-1])
if err != nil {
t.Error(err)
}
if !isSortedDesc(inst, as1[0]) {
t.Error("Instances are not sorted in descending order")
t.Error(inst1)
}
if !inst2.Equal(inst) {
t.Error("Instances don't match")
t.Error(inst)
t.Error(inst2)
}
Convey("Given data that's not already sorted descending", t, func() {
unsorted, err := ParseCSVToInstances("../examples/datasets/iris_headers.csv", true)
So(err, ShouldBeNil)

as1 := ResolveAllAttributes(unsorted)
So(isSortedDesc(unsorted, as1[0]), ShouldBeFalse)

Convey("Given reference data that's alredy sorted descending", func() {
sortedDescending, err := ParseCSVToInstances("../examples/datasets/iris_sorted_desc.csv", true)
So(err, ShouldBeNil)

as2 := ResolveAllAttributes(sortedDescending)
So(isSortedDesc(sortedDescending, as2[0]), ShouldBeTrue)

Convey("LazySorting Descending", func() {
result, err := LazySort(unsorted, Descending, as1[0:len(as1)-1])
So(err, ShouldBeNil)

Convey("Result should be sorted descending", func() {
So(isSortedDesc(result, as1[0]), ShouldBeTrue)
})

Convey("Result should match the reference", func() {
So(sortedDescending.Equal(result), ShouldBeTrue)
})
})
})
})
}

func TestLazySortAsc(t *testing.T) {
inst, err := ParseCSVToInstances("../examples/datasets/iris_headers.csv", true)
as1 := ResolveAllAttributes(inst)
if isSortedAsc(inst, as1[0]) {
t.Error("Can't test ascending sort on something ascending already")
}
if err != nil {
t.Error(err)
return
}
insts, err := LazySort(inst, Ascending, as1)
if err != nil {
t.Error(err)
return
}
if !isSortedAsc(insts, as1[0]) {
t.Error("Instances are not sorted in ascending order")
t.Error(insts)
}

inst2, err := ParseCSVToInstances("../examples/datasets/iris_sorted_asc.csv", true)
if err != nil {
t.Error(err)
return
}
as2 := ResolveAllAttributes(inst2)
if !isSortedAsc(inst2, as2[0]) {
t.Error("This file should be sorted in ascending order")
}

if !inst2.Equal(insts) {
t.Error("Instances don't match")
t.Error(inst)
t.Error(inst2)
}

rowStr := insts.RowString(0)
ref := "4.30 3.00 1.10 0.10 Iris-setosa"
if rowStr != ref {
t.Fatalf("'%s' != '%s'", rowStr, ref)
}
Convey("Given data that's not already sorted ascending", t, func() {
unsorted, err := ParseCSVToInstances("../examples/datasets/iris_headers.csv", true)
So(err, ShouldBeNil)

as1 := ResolveAllAttributes(unsorted)
So(isSortedAsc(unsorted, as1[0]), ShouldBeFalse)

Convey("Given reference data that's alredy sorted ascending", func() {
sortedAscending, err := ParseCSVToInstances("../examples/datasets/iris_sorted_asc.csv", true)
So(err, ShouldBeNil)

as2 := ResolveAllAttributes(sortedAscending)
So(isSortedAsc(sortedAscending, as2[0]), ShouldBeTrue)

Convey("LazySorting Ascending", func() {
result, err := LazySort(unsorted, Ascending, as1[0:len(as1)-1])
So(err, ShouldBeNil)

Convey("Result should be sorted descending", func() {
So(isSortedAsc(result, as1[0]), ShouldBeTrue)
})

Convey("Result should match the reference", func() {
So(sortedAscending.Equal(result), ShouldBeTrue)
})

Convey("First element of Result should equal known value", func() {
So(result.RowString(0), ShouldEqual, "4.30 3.00 1.10 0.10 Iris-setosa")
})
})
})
})
}
118 changes: 58 additions & 60 deletions base/sort_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package base

import "testing"
import (
. "github.com/smartystreets/goconvey/convey"
"testing"
)

func isSortedAsc(inst FixedDataGrid, attr AttributeSpec) bool {
valPrev := 0.0
Expand Down Expand Up @@ -33,72 +36,67 @@ func isSortedDesc(inst FixedDataGrid, attr AttributeSpec) bool {
}

func TestSortDesc(t *testing.T) {
inst1, err := ParseCSVToInstances("../examples/datasets/iris_headers.csv", true)
if err != nil {
t.Error(err)
return
}
inst2, err := ParseCSVToInstances("../examples/datasets/iris_sorted_desc.csv", true)
if err != nil {
t.Error(err)
return
}
Convey("Given data that's not already sorted descending", t, func() {
unsorted, err := ParseCSVToInstances("../examples/datasets/iris_headers.csv", true)
So(err, ShouldBeNil)

as1 := ResolveAllAttributes(inst1)
as2 := ResolveAllAttributes(inst2)
as1 := ResolveAllAttributes(unsorted)
So(isSortedDesc(unsorted, as1[0]), ShouldBeFalse)

if isSortedDesc(inst1, as1[0]) {
t.Error("Can't test descending sort order")
}
if !isSortedDesc(inst2, as2[0]) {
t.Error("Reference data not sorted in descending order!")
}
Convey("Given reference data that's alredy sorted descending", func() {
sortedDescending, err := ParseCSVToInstances("../examples/datasets/iris_sorted_desc.csv", true)
So(err, ShouldBeNil)

Sort(inst1, Descending, as1[0:len(as1)-1])
if err != nil {
t.Error(err)
}
if !isSortedDesc(inst1, as1[0]) {
t.Error("Instances are not sorted in descending order")
t.Error(inst1)
}
if !inst2.Equal(inst1) {
t.Error("Instances don't match")
t.Error(inst1)
t.Error(inst2)
}
as2 := ResolveAllAttributes(sortedDescending)
So(isSortedDesc(sortedDescending, as2[0]), ShouldBeTrue)

Convey("Sorting Descending", func() {
result, err := Sort(unsorted, Descending, as1[0:len(as1)-1])
So(err, ShouldBeNil)

Convey("Result should be sorted descending", func() {
So(isSortedDesc(result, as1[0]), ShouldBeTrue)
})

Convey("Result should match the reference", func() {
So(sortedDescending.Equal(result), ShouldBeTrue)
})
})
})
})
}

func TestSortAsc(t *testing.T) {
inst, err := ParseCSVToInstances("../examples/datasets/iris_headers.csv", true)
as1 := ResolveAllAttributes(inst)
if isSortedAsc(inst, as1[0]) {
t.Error("Can't test ascending sort on something ascending already")
}
if err != nil {
t.Error(err)
return
}
Sort(inst, Ascending, as1[0:1])
if !isSortedAsc(inst, as1[0]) {
t.Error("Instances are not sorted in ascending order")
t.Error(inst)
}
Convey("Given data that's not already sorted ascending", t, func() {
unsorted, err := ParseCSVToInstances("../examples/datasets/iris_headers.csv", true)
So(err, ShouldBeNil)

inst2, err := ParseCSVToInstances("../examples/datasets/iris_sorted_asc.csv", true)
if err != nil {
t.Error(err)
return
}
as2 := ResolveAllAttributes(inst2)
if !isSortedAsc(inst2, as2[0]) {
t.Error("This file should be sorted in ascending order")
}
as1 := ResolveAllAttributes(unsorted)
So(isSortedAsc(unsorted, as1[0]), ShouldBeFalse)

if !inst2.Equal(inst) {
t.Error("Instances don't match")
t.Error(inst)
t.Error(inst2)
}
Convey("Given reference data that's alredy sorted ascending", func() {
sortedAscending, err := ParseCSVToInstances("../examples/datasets/iris_sorted_asc.csv", true)
So(err, ShouldBeNil)

as2 := ResolveAllAttributes(sortedAscending)
So(isSortedAsc(sortedAscending, as2[0]), ShouldBeTrue)

Convey("Sorting Ascending", func() {
result, err := Sort(unsorted, Ascending, as1[0:len(as1)-1])
So(err, ShouldBeNil)

Convey("Result should be sorted descending", func() {
So(isSortedAsc(result, as1[0]), ShouldBeTrue)
})

Convey("Result should match the reference", func() {
So(sortedAscending.Equal(result), ShouldBeTrue)
})

Convey("First element of Result should equal known value", func() {
So(result.RowString(0), ShouldEqual, "4.30 3.00 1.10 0.10 Iris-setosa")
})
})
})
})
}
Loading

0 comments on commit 9f67f73

Please sign in to comment.