package console import ( "fmt" "testing" ) func TestTopWithOther(t *testing.T) { m := map[string]int64{"_other": 5} for i := 0; i < 12; i++ { m[fmt.Sprintf("C%02d", i)] = int64(100 - i) // C00 largest, C11 smallest } got := topWithOther(m, 10) if len(got) != 11 { t.Fatalf("len = %d, want 10 + _other: %v", len(got), got) } if got["_other"] != 5+89+90 { // C10=90, C11=89, plus the existing 5 t.Fatalf("_other = %d", got["_other"]) } if _, ok := got["C10"]; ok { t.Fatal("the 11th largest survived") } if len(topWithOther(nil, 10)) != 0 || topWithOther(nil, 10) == nil { t.Fatal("nil in must be an empty, non-nil map out") } }