Debian Codegraph @

mem/count_pair_test.go

raw
1 package mem
2
3 import (
4 "fmt"
5 "testing"
6 )
7
8 var (
9 countPairSizes = [...]int{
10 1, 2, 4, 8, 10, 16, 32,
11 }
12 )
13
14 func TestCountPair(t *testing.T) {
15 for _, size := range countPairSizes {
16 makeInput := func(values ...byte) []byte {
17 input := make([]byte, size*len(values))
18 for i := range values {
19 input[i*size] = values[i]
20 }
21 return input
22 }
23
24 t.Run(fmt.Sprintf("N=%d", size), func(t *testing.T) {
25 tests := []struct {
26 scenario string
27 input []byte
28 count int
29 }{
30 {
31 scenario: "empty input",
32 input: nil,
33 count: 0,
34 },
35
36 {
37 scenario: "input with only one item",
38 input: makeInput(1),
39 count: 0,
40 },
41
42 {
43 scenario: "input with two non-equal items",
44 input: makeInput(1, 2),
45 count: 0,
46 },
47
48 {
49 scenario: "input with two equal items",
50 input: makeInput(1, 1),
51 count: 1,
52 },
53
54 {
55 scenario: "input with two equal items in the middle",
56 input: makeInput(0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9),
57 count: 1,
58 },
59
60 {
61 scenario: "input with two equal items at the end",
62 input: makeInput(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9),
63 count: 1,
64 },
65
66 {
67 scenario: "input with many equal items at the beginning of a long sequence",
68 input: makeInput(
69 0, 0, 0, 0, 0, 0, 0, 0, 0,
70 0, 1, 2, 3, 4, 5, 6, 7, 8,
71 0, 1, 2, 3, 4, 5, 6, 7, 8,
72 0, 1, 2, 3, 4, 5, 6, 7, 8,
73 0, 1, 2, 3, 4, 5, 6, 7, 8,
74 0, 1, 2, 3, 4, 5, 6, 7, 8,
75 0, 1, 2, 3, 4, 5, 6, 7, 8,
76 0, 1, 2, 3, 4, 5, 6, 7, 8,
77 0, 1, 2, 3, 4, 5, 6, 7, 8,
78 0, 1, 2, 3, 4, 5, 6, 7, 8,
79 0, 1, 2, 3, 4, 5, 6, 7, 8,
80 0, 1, 2, 3, 4, 5, 6, 7, 8,
81 0, 1, 2, 3, 4, 5, 6, 7, 8,
82 0, 1, 2, 3, 4, 5, 6, 7, 8,
83 0, 1, 2, 3, 4, 5, 6, 7, 8,
84 0, 1, 2, 3, 4, 5, 6, 7, 8,
85 ),
86 count: 9,
87 },
88
89 {
90 scenario: "input with many equal items in the middle of a long sequence",
91 input: makeInput(
92 0, 1, 2, 3, 4, 5, 6, 7, 8,
93 0, 1, 2, 3, 4, 5, 6, 7, 8,
94 0, 1, 2, 3, 4, 5, 6, 7, 8,
95 0, 1, 2, 3, 4, 5, 6, 7, 8,
96 0, 1, 2, 3, 4, 5, 6, 7, 8,
97 0, 1, 2, 3, 4, 5, 6, 7, 8,
98 0, 0, 0, 0, 0, 0, 0, 0, 0,
99 0, 1, 2, 3, 4, 5, 6, 7, 8,
100 0, 1, 2, 3, 4, 5, 6, 7, 8,
101 0, 1, 2, 3, 4, 5, 6, 7, 8,
102 0, 1, 2, 3, 4, 5, 6, 7, 8,
103 0, 1, 2, 3, 4, 5, 6, 7, 8,
104 0, 1, 2, 3, 4, 5, 6, 7, 8,
105 0, 1, 2, 3, 4, 5, 6, 7, 8,
106 0, 1, 2, 3, 4, 5, 6, 7, 8,
107 0, 1, 2, 3, 4, 5, 6, 7, 8,
108 ),
109 count: 9,
110 },
111
112 {
113 scenario: "input with many equal items in a long sequence",
114 input: makeInput(
115 0, 1, 2, 3, 4, 5, 6, 7, 0,
116 0, 1, 2, 3, 4, 5, 6, 7, 0,
117 0, 1, 2, 3, 4, 5, 6, 7, 0,
118 0, 1, 2, 3, 4, 5, 6, 7, 0,
119 0, 1, 2, 3, 4, 5, 6, 7, 0,
120 0, 1, 2, 3, 4, 5, 6, 7, 0,
121 0, 1, 2, 3, 4, 5, 6, 7, 0,
122 0, 1, 2, 3, 4, 5, 6, 7, 0,
123 0, 1, 2, 3, 4, 5, 6, 7, 0,
124 0, 1, 2, 3, 4, 5, 6, 7, 0,
125 0, 1, 2, 3, 4, 5, 6, 7, 0,
126 0, 1, 2, 3, 4, 5, 6, 7, 0,
127 0, 1, 2, 3, 4, 5, 6, 7, 0,
128 0, 1, 2, 3, 4, 5, 6, 7, 0,
129 0, 1, 2, 3, 4, 5, 6, 7, 0,
130 0, 1, 2, 3, 4, 5, 6, 7, 0,
131 ),
132 count: 15,
133 },
134 }
135
136 for _, test := range tests {
137 t.Run(test.scenario, func(t *testing.T) {
138 n := CountPair(test.input, size)
139
140 if n != test.count {
141 t.Errorf("expected=%d found=%d", test.count, n)
142 }
143 })
144 }
145 })
146 }
147 }
148
149 func BenchmarkCountPair(b *testing.B) {
150 for _, size := range countPairSizes {
151 input := make([]byte, 16*1024)
152 for i := range input {
153 input[i] = byte(i)
154 }
155
156 if size%len(input) != 0 {
157 input = input[:(len(input)/size)*size]
158 }
159
160 b.Run(fmt.Sprintf("N=%d", size), func(b *testing.B) {
161 b.SetBytes(int64(len(input)))
162
163 for i := 0; i < b.N; i++ {
164 n := CountPair(input, size)
165 if n != 0 {
166 b.Fatal("unexpected result:", n)
167 }
168 }
169 })
170 }
171 }