Download this file
| 1 | package console |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestTopWithOther(t *testing.T) { |
| 9 | m := map[string]int64{"_other": 5} |
| 10 | for i := 0; i < 12; i++ { |
| 11 | m[fmt.Sprintf("C%02d", i)] = int64(100 - i) // C00 largest, C11 smallest |
| 12 | } |
| 13 | got := topWithOther(m, 10) |
| 14 | if len(got) != 11 { |
| 15 | t.Fatalf("len = %d, want 10 + _other: %v", len(got), got) |
| 16 | } |
| 17 | if got["_other"] != 5+89+90 { // C10=90, C11=89, plus the existing 5 |
| 18 | t.Fatalf("_other = %d", got["_other"]) |
| 19 | } |
| 20 | if _, ok := got["C10"]; ok { |
| 21 | t.Fatal("the 11th largest survived") |
| 22 | } |
| 23 | if len(topWithOther(nil, 10)) != 0 || topWithOther(nil, 10) == nil { |
| 24 | t.Fatal("nil in must be an empty, non-nil map out") |
| 25 | } |
| 26 | } |
| 27 | |