forked from sspencer/colorart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
counted_set_test.go
124 lines (101 loc) · 2.19 KB
/
counted_set_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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package colorart
import "testing"
var (
zero = rgb{0, 100, 200}
one = rgb{1, 11, 111}
two = rgb{2, 22, 222}
)
func TestAddPixel(t *testing.T) {
s := NewCountedSet(10)
s.AddPixel(pixel{0.2, 0.4, 0.6, 1})
entries := s.SortedSet()
e := entries[0]
str := e.String()
answer := "336699: 1"
if str != answer {
t.Errorf("String conversion should be %s, not %s", answer, str)
}
}
func TestCount(t *testing.T) {
s := NewCountedSet(10)
s[one]++
if s.Count(one) != 1 {
t.Error("Count should be 1")
}
s[one]++
if s.Count(one) != 2 {
t.Error("Count should be 2")
}
s[one]++
if s.Count(one) != 3 {
t.Error("Count should be 3")
}
if len(s) != 1 {
// Only "one" and "two" should be in Set
t.Error("len: incorrect size after Add")
}
}
func TestMultiCount(t *testing.T) {
s := NewCountedSet(10)
s[one]++
s[two]++
s[two]++
if s.Count(one) != 1 {
t.Error("Add: incorrect 'one' count")
}
if s.Count(two) != 2 {
t.Error("Add: incorrect 'two' count")
}
if s.Count(zero) != 0 {
t.Error("Add: incorrect 'zero' count")
}
if len(s) != 2 {
// Only "one" and "two" should be in Set
t.Error("len: incorrect size after Add")
}
}
func TestSortedSet(t *testing.T) {
s := NewCountedSet(10)
s[one]++
s[two]++
s[two]++
entries := s.SortedSet()
if len(entries) != 2 {
t.Error("Sorted set returned incorrect number of elements")
}
e := entries[0]
if e.Color != two && e.Count != 2 {
t.Error("First sorted entry (two) is incorrect")
}
e = entries[1]
if e.Color != one && e.Count != 1 {
t.Error("First sorted entry (two) is incorrect")
}
}
func TestAddCount(t *testing.T) {
s := NewCountedSet(10)
s[one]++
if s.Count(one) != 1 {
t.Error("Incorrect count before AddCount")
}
// does not add new count to old ... replaces old with new
s.AddCount(one, 108)
if s.Count(one) != 109 {
t.Error("Incorrect count after AddCount(1)")
}
s.AddCount(two, 23456)
if s.Count(two) != 23456 {
t.Error("Incorrect count after AddCount(2)")
}
}
func TestString(t *testing.T) {
s := NewCountedSet(10)
s[one]++
entries := s.SortedSet()
e := entries[0]
str := e.String()
answer := "010b6f: 1"
if str != answer {
t.Errorf("String conversion should be %s, not %s", answer, str)
}
}