-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter_test.go
83 lines (69 loc) · 2.11 KB
/
counter_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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package godino
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewCounter(t *testing.T) {
c := NewCounter([]string{"foo", "bar", "baz", "foo", "foo", "baz"})
expected := counterElements[string]{{"foo", 3}, {"bar", 1}, {"baz", 2}}
assert.Equal(t, expected, c.Elements())
}
func TestCounterAdd(t *testing.T) {
c := NewCounter([]string{"foo", "bar", "baz", "foo", "foo", "baz"})
c.Add("bar")
assert.Equal(t, 2, c.counts["bar"])
}
func TestGet(t *testing.T) {
c := NewCounter([]string{"foo", "bar", "baz", "foo", "foo", "baz"})
assert.Equal(t, 3, c.Get("foo"))
}
func TestMostCommon(t *testing.T) {
c := NewCounter([]string{"foo", "bar", "baz", "foo", "foo", "baz"})
t.Run("get all elements sorted by most common", func(t *testing.T) {
mostCommon := c.MostCommon(-1)
expected := counterElements[string]{
{"foo", 3},
{"baz", 2},
{"bar", 1},
}
assert.Equal(t, expected, mostCommon)
})
t.Run("get n most common elements", func(t *testing.T) {
mostCommon := c.MostCommon(2)
expected := counterElements[string]{
{"foo", 3},
{"baz", 2},
}
assert.Equal(t, expected, mostCommon)
})
t.Run("ties are broken by element first inserted", func(t *testing.T) {
c := NewCounter([]string{"foo", "bar", "baz", "baz"})
mostCommon := c.MostCommon(1)
expected := counterElements[string]{{"baz", 2}}
assert.Equal(t, expected, mostCommon)
c.Add("foo")
mostCommon2 := c.MostCommon(1)
expected2 := counterElements[string]{{"foo", 2}}
assert.Equal(t, expected2, mostCommon2)
})
}
func TestSubtract(t *testing.T) {
c := NewCounter([]string{"foo", "bar", "baz", "foo", "foo", "baz"})
assert.Equal(t, 3, c.Get("foo"))
c.Subtract("foo")
assert.Equal(t, 2, c.Get("foo"))
}
func TestTotal(t *testing.T) {
c := NewCounter([]string{"foo", "bar", "baz", "foo", "foo", "baz"})
assert.Equal(t, 6, c.Total())
}
func TestUpdate(t *testing.T) {
c := NewCounter([]int{1, 1, 1, 2, 3, 3})
arr1 := []int{2, 3, 3, 4, 5}
arr2 := []int{4, 5, 5, 6}
c.Update(arr1, arr2)
expected := counterElements[int]{
{1, 3}, {2, 2}, {3, 4}, {4, 2}, {5, 3}, {6, 1},
}
assert.Equal(t, expected, c.Elements())
}