Example #1
0
// TestStringTable
func TestStringTable(test *testing.T) {
	test_strings := []string{"a", "b", "c", "d", "e"}

	table := db.NewStringTable()

	// Map every string twice.  During the first cycle, the string
	// should not be found.  During the second cycle it should be
	// found.  Unmap should return the original value during both
	// cycles.
	for i := 0; i < 2; i++ {
		for _, s := range test_strings {
			m, ok := table.Encode(s)
			u := table.Decode(m)
			if u != s {
				test.Errorf(`"%s" mapped back to "%s" during cycle %d`, s, u, i)
			}
			if ok && i == 0 {
				test.Errorf(`"%s" found during fill cycle`, s)
			}
			if !ok && i == 1 {
				test.Errorf(`"%s" not found during verify cycle`, s)
			}
		}
	}
}
Example #2
0
func featureFactory(s string) db.DataFrameFeature {
	var feature db.Feature
	switch s[0] {
	case 'C':
		feature = db.NewCategoricalFeature(db.NewStringTable())
	default:
		feature = db.NewNumericFeature(nil)
	}
	return db.NewDataFrameFeature(s, feature)
}
Example #3
0
func TestCategoricalFeature(t *testing.T) {
	// Assign to Feature to ensure CategoricalFeature implements Feature
	cf := db.NewCategoricalFeature(db.NewStringTable())

	testStrings := []string{"alpha", "beta", "delta", "beta", "alpha"}

	for _, s := range testStrings {
		cf.AddFromString(s)
	}

	check := func(index int, expected string) {
		actual := cf.Value(index).(string)
		altActual := cf.Decode(cf.NumericValue(index)).(string)
		if actual != altActual {
			t.Errorf("Value(%d) returned %v; Decode(NumericValue(%d) returned %v", index, actual, index, altActual)
		}
		if actual != expected {
			t.Errorf("Get(%d) got %v; expecting %v", index, actual, expected)
		}
	}

	for i, s := range testStrings {
		check(i, s)
	}

	if cf.Len() != len(testStrings) {
		t.Errorf("Length() returned %d; expecting %d", cf.Len(), 5)
	}

	ordercheck := func(index int, expected string) {
		orderedString := cf.Decode(cf.NumericValue(cf.InOrder(index)))
		if orderedString != expected {
			t.Errorf("Get(%d) got %s; expecting %s", index, orderedString, expected)
		}
	}

	cf.Prepare()

	ordercheck(0, "alpha")
	ordercheck(1, "alpha")
	ordercheck(2, "beta")
	ordercheck(3, "beta")
	ordercheck(4, "delta")

	if n := cf.Categories(); n != 3 {
		t.Errorf("Expected %d categories; got %d\n", 3, n)
	}
}