1
0
mirror of https://github.com/sjwhitworth/golearn.git synced 2025-04-26 13:49:14 +08:00

Favor idiomatic t.Fatalf over panic for test failures

This commit is contained in:
Amit Kumar Gupta 2014-08-22 08:07:55 +00:00
parent 45545d6ebd
commit 695aec6eb6
7 changed files with 27 additions and 27 deletions

View File

@ -35,7 +35,7 @@ func TestBAGSimple(t *testing.T) {
} else if name == "2" {
attrSpecs[2] = a
} else {
panic(name)
t.Fatalf("Unexpected attribute name '%s'", name)
}
}
@ -102,7 +102,7 @@ func TestBAG(t *testing.T) {
} else if name == "2" {
attrSpecs[2] = a
} else {
panic(name)
t.Fatalf("Unexpected attribute name '%s'", name)
}
}

View File

@ -10,7 +10,7 @@ import (
func TestRandomForest1(testEnv *testing.T) {
inst, err := base.ParseCSVToInstances("../examples/datasets/iris_headers.csv", true)
if err != nil {
panic(err)
testEnv.Fatal("Unable to parse CSV to instances: %s", err.Error())
}
filt := filters.NewChiMergeFilter(inst, 0.90)

View File

@ -11,12 +11,12 @@ func TestBinning(t *testing.T) {
// Read the data
inst1, err := base.ParseCSVToInstances("../examples/datasets/iris_headers.csv", true)
if err != nil {
panic(err)
t.Fatal("Unable to parse CSV to instances: %s", err.Error())
}
inst2, err := base.ParseCSVToInstances("../examples/datasets/iris_binned.csv", true)
if err != nil {
panic(err)
t.Fatal("Unable to parse CSV to instances: %s", err.Error())
}
//
// Construct the binning filter

View File

@ -9,7 +9,7 @@ import (
func TestChiMFreqTable(testEnv *testing.T) {
inst, err := base.ParseCSVToInstances("../examples/datasets/chim.csv", true)
if err != nil {
panic(err)
testEnv.Fatal("Unable to parse CSV to instances: %s", err.Error())
}
freq := ChiMBuildFrequencyTable(inst.AllAttributes()[0], inst)
@ -28,7 +28,7 @@ func TestChiMFreqTable(testEnv *testing.T) {
func TestChiClassCounter(testEnv *testing.T) {
inst, err := base.ParseCSVToInstances("../examples/datasets/chim.csv", true)
if err != nil {
panic(err)
testEnv.Fatal("Unable to parse CSV to instances: %s", err.Error())
}
freq := ChiMBuildFrequencyTable(inst.AllAttributes()[0], inst)
classes := chiCountClasses(freq)
@ -46,7 +46,7 @@ func TestChiClassCounter(testEnv *testing.T) {
func TestStatisticValues(testEnv *testing.T) {
inst, err := base.ParseCSVToInstances("../examples/datasets/chim.csv", true)
if err != nil {
panic(err)
testEnv.Fatal("Unable to parse CSV to instances: %s", err.Error())
}
freq := ChiMBuildFrequencyTable(inst.AllAttributes()[0], inst)
chiVal := chiComputeStatistic(freq[5], freq[6])
@ -76,11 +76,9 @@ func TestChiSquareDistValues(testEnv *testing.T) {
}
func TestChiMerge1(testEnv *testing.T) {
// Read the data
inst, err := base.ParseCSVToInstances("../examples/datasets/chim.csv", true)
if err != nil {
panic(err)
testEnv.Fatal("Unable to parse CSV to instances: %s", err.Error())
}
_, rows := inst.Size()
@ -105,7 +103,7 @@ func TestChiMerge2(testEnv *testing.T) {
// Randy Kerber, ChiMerge: Discretisation of Numeric Attributes, 1992
inst, err := base.ParseCSVToInstances("../examples/datasets/iris_headers.csv", true)
if err != nil {
panic(err)
testEnv.Fatal("Unable to parse CSV to instances: %s", err.Error())
}
// Sort the instances
@ -113,7 +111,7 @@ func TestChiMerge2(testEnv *testing.T) {
sortAttrSpecs := base.ResolveAttributes(inst, allAttrs)[0:1]
instSorted, err := base.Sort(inst, base.Ascending, sortAttrSpecs)
if err != nil {
panic(err)
testEnv.Fatalf("Sort failed: %s", err.Error())
}
// Perform Chi-Merge
@ -179,7 +177,7 @@ func TestChiMerge4(testEnv *testing.T) {
// Randy Kerber, ChiMerge: Discretisation of Numeric Attributes, 1992
inst, err := base.ParseCSVToInstances("../examples/datasets/iris_headers.csv", true)
if err != nil {
panic(err)
testEnv.Fatal("Unable to parse CSV to instances: %s", err.Error())
}
filt := NewChiMergeFilter(inst, 0.90)
@ -191,7 +189,9 @@ func TestChiMerge4(testEnv *testing.T) {
if len(clsAttrs) != 1 {
testEnv.Fatalf("%d != %d", len(clsAttrs), 1)
}
if clsAttrs[0].GetName() != "Species" {
panic("Class Attribute wrong!")
firstClassAttributeName := clsAttrs[0].GetName()
expectedClassAttributeName := "Species"
if firstClassAttributeName != expectedClassAttributeName {
testEnv.Fatalf("Expected class attribute '%s'; actual class attribute '%s'", expectedClassAttributeName, firstClassAttributeName)
}
}

View File

@ -13,7 +13,7 @@ import (
func BenchmarkBaggingRandomForestFit(testEnv *testing.B) {
inst, err := base.ParseCSVToInstances("../examples/datasets/iris_headers.csv", true)
if err != nil {
panic(err)
testEnv.Fatal("Unable to parse CSV to instances: %s", err.Error())
}
rand.Seed(time.Now().UnixNano())
@ -38,7 +38,7 @@ func BenchmarkBaggingRandomForestFit(testEnv *testing.B) {
func BenchmarkBaggingRandomForestPredict(testEnv *testing.B) {
inst, err := base.ParseCSVToInstances("../examples/datasets/iris_headers.csv", true)
if err != nil {
panic(err)
testEnv.Fatal("Unable to parse CSV to instances: %s", err.Error())
}
rand.Seed(time.Now().UnixNano())
@ -64,7 +64,7 @@ func BenchmarkBaggingRandomForestPredict(testEnv *testing.B) {
func TestRandomForest1(testEnv *testing.T) {
inst, err := base.ParseCSVToInstances("../examples/datasets/iris_headers.csv", true)
if err != nil {
panic(err)
testEnv.Fatal("Unable to parse CSV to instances: %s", err.Error())
}
trainData, testData := base.InstancesTrainTestSplit(inst, 0.6)

View File

@ -140,7 +140,7 @@ func TestLayeredXOR(t *testing.T) {
for _, a := range pred.AllAttributes() {
af, ok := a.(*base.FloatAttribute)
if !ok {
panic("All of these should be FloatAttributes!")
t.Fatalf("Expected all attributes to be FloatAttributes; actually some were not")
}
af.Precision = 1
}

View File

@ -11,7 +11,7 @@ import (
func TestRandomTree(testEnv *testing.T) {
inst, err := base.ParseCSVToInstances("../examples/datasets/iris_headers.csv", true)
if err != nil {
panic(err)
testEnv.Fatal("Unable to parse CSV to instances: %s", err.Error())
}
filt := filters.NewChiMergeFilter(inst, 0.90)
@ -30,7 +30,7 @@ func TestRandomTree(testEnv *testing.T) {
func TestRandomTreeClassification(testEnv *testing.T) {
inst, err := base.ParseCSVToInstances("../examples/datasets/iris_headers.csv", true)
if err != nil {
panic(err)
testEnv.Fatal("Unable to parse CSV to instances: %s", err.Error())
}
trainData, testData := base.InstancesTrainTestSplit(inst, 0.6)
@ -55,7 +55,7 @@ func TestRandomTreeClassification(testEnv *testing.T) {
func TestRandomTreeClassification2(testEnv *testing.T) {
inst, err := base.ParseCSVToInstances("../examples/datasets/iris_headers.csv", true)
if err != nil {
panic(err)
testEnv.Fatal("Unable to parse CSV to instances: %s", err.Error())
}
trainData, testData := base.InstancesTrainTestSplit(inst, 0.4)
@ -78,7 +78,7 @@ func TestRandomTreeClassification2(testEnv *testing.T) {
func TestPruning(testEnv *testing.T) {
inst, err := base.ParseCSVToInstances("../examples/datasets/iris_headers.csv", true)
if err != nil {
panic(err)
testEnv.Fatal("Unable to parse CSV to instances: %s", err.Error())
}
trainData, testData := base.InstancesTrainTestSplit(inst, 0.6)
@ -120,7 +120,7 @@ func TestInformationGain(testEnv *testing.T) {
func TestID3Inference(testEnv *testing.T) {
inst, err := base.ParseCSVToInstances("../examples/datasets/tennis.csv", true)
if err != nil {
panic(err)
testEnv.Fatal("Unable to parse CSV to instances: %s", err.Error())
}
// Build the decision tree
@ -170,7 +170,7 @@ func TestID3Inference(testEnv *testing.T) {
func TestID3Classification(testEnv *testing.T) {
inst, err := base.ParseCSVToInstances("../examples/datasets/iris_headers.csv", true)
if err != nil {
panic(err)
testEnv.Fatal("Unable to parse CSV to instances: %s", err.Error())
}
filt := filters.NewBinningFilter(inst, 10)
@ -194,7 +194,7 @@ func TestID3Classification(testEnv *testing.T) {
func TestID3(testEnv *testing.T) {
inst, err := base.ParseCSVToInstances("../examples/datasets/tennis.csv", true)
if err != nil {
panic(err)
testEnv.Fatal("Unable to parse CSV to instances: %s", err.Error())
}
// Build the decision tree