-
Notifications
You must be signed in to change notification settings - Fork 10
/
hot_keys_test.go
56 lines (50 loc) · 1.21 KB
/
hot_keys_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"container/heap"
"testing"
)
func stringsEqual(first []string, second []string) bool {
if len(first) != len(second) {
return false
}
for i := range first {
if first[i] != second[i] {
return false
}
}
return true
}
func TestHotKeys(t *testing.T) {
h := NewHotKeyPool()
h.Add([]string{"foo", "foo", "baz", "baz", "bar", "baz"})
top_keys := h.GetTopKeys()
expected_keys := []Key{
Key{"baz", 3},
Key{"foo", 2},
Key{"bar", 1},
}
for _, key := range expected_keys {
popped_key := heap.Pop(top_keys).(*Key)
if key.Name != popped_key.Name {
t.Errorf("Expected top key %v, got %v\n", key.Name, popped_key.Name)
}
if key.Hits != popped_key.Hits {
t.Errorf("Expected key %s to have %d hits, got %vdn",
key.Name, key.Hits, popped_key.Hits)
}
}
}
func TestHotKeysClone(t *testing.T) {
h := NewHotKeyPool()
h.Add([]string{"foo", "foo", "bar", "baz", "baz", "baz"})
rotated := h.Rotate()
// Validate old top keys
top_keys := rotated.GetTopKeys()
expected_keys := []string{"baz", "foo", "bar"}
for _, key := range expected_keys {
popped_key := heap.Pop(top_keys).(*Key)
if key != popped_key.Name {
t.Errorf("Expected top key %v, got %v\n", key, popped_key)
}
}
}