diff --git a/base/bag_test.go b/base/bag_test.go index f5366ae..485d79c 100644 --- a/base/bag_test.go +++ b/base/bag_test.go @@ -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) } } diff --git a/ensemble/randomforest_test.go b/ensemble/randomforest_test.go index 762125d..3828e8c 100644 --- a/ensemble/randomforest_test.go +++ b/ensemble/randomforest_test.go @@ -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) diff --git a/filters/binning_test.go b/filters/binning_test.go index f655717..dfa2518 100644 --- a/filters/binning_test.go +++ b/filters/binning_test.go @@ -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 diff --git a/filters/chimerge_test.go b/filters/chimerge_test.go index 72ae5be..677d5aa 100644 --- a/filters/chimerge_test.go +++ b/filters/chimerge_test.go @@ -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) } } diff --git a/meta/bagging_test.go b/meta/bagging_test.go index 651f4f8..9443cf7 100644 --- a/meta/bagging_test.go +++ b/meta/bagging_test.go @@ -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) diff --git a/neural/layered_test.go b/neural/layered_test.go index e9c52f6..98443c3 100644 --- a/neural/layered_test.go +++ b/neural/layered_test.go @@ -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 } diff --git a/trees/tree_test.go b/trees/tree_test.go index 1912177..d20089f 100644 --- a/trees/tree_test.go +++ b/trees/tree_test.go @@ -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