Debian Codegraph @

mem/index_pair_test.go

raw
1 package mem
2
3 import (
4 "fmt"
5 "testing"
6 )
7
8 var (
9 indexPairSizes = [...]int{
10 1, 2, 4, 8, 10, 16, 32,
11 }
12 )
13
14 func TestIndexPair(t *testing.T) {
15 for _, size := range indexPairSizes {
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 index int
29 }{
30 {
31 scenario: "empty input",
32 input: nil,
33 index: -1,
34 },
35
36 {
37 scenario: "input with only one item",
38 input: makeInput(1),
39 index: -1,
40 },
41
42 {
43 scenario: "input with two non-equal items",
44 input: makeInput(1, 2),
45 index: -1,
46 },
47
48 {
49 scenario: "input with two equal items",
50 input: makeInput(1, 1),
51 index: 0,
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 index: 5,
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 index: 9,
64 },
65
66 {
67 scenario: "input with two equal items at the beginning of a long sequence",
68 input: makeInput(
69 0, 0, 2, 3, 4, 5, 6, 7, 8,
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 index: 0,
87 },
88
89 {
90 scenario: "input with two 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, 1, 2, 3, 4, 5, 6, 7, 7, // pair
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 index: 61,
110 },
111
112 {
113 scenario: "input with two equal items at the end of a long sequence",
114 input: makeInput(
115 0, 1, 2, 3, 4, 5, 6, 7, 8,
116 0, 1, 2, 3, 4, 5, 6, 7, 8,
117 0, 1, 2, 3, 4, 5, 6, 7, 8,
118 0, 1, 2, 3, 4, 5, 6, 7, 8,
119 0, 1, 2, 3, 4, 5, 6, 7, 8,
120 0, 1, 2, 3, 4, 5, 6, 7, 8,
121 0, 1, 2, 3, 4, 5, 6, 7, 8,
122 0, 1, 2, 3, 4, 5, 6, 7, 8,
123 0, 1, 2, 3, 4, 5, 6, 7, 8,
124 0, 1, 2, 3, 4, 5, 6, 7, 8,
125 0, 1, 2, 3, 4, 5, 6, 7, 8,
126 0, 1, 2, 3, 4, 5, 6, 7, 8,
127 0, 1, 2, 3, 4, 5, 6, 7, 8,
128 0, 1, 2, 3, 4, 5, 6, 7, 8,
129 0, 1, 2, 3, 4, 5, 6, 7, 8,
130 0, 1, 2, 3, 4, 5, 6, 7, 7, // pair
131 ),
132 index: 142,
133 },
134 }
135
136 for _, test := range tests {
137 t.Run(test.scenario, func(t *testing.T) {
138 i := test.index * size
139 j := IndexPair(test.input, size)
140
141 if i < 0 {
142 i = -1
143 }
144
145 if i != j {
146 t.Errorf("expected=%d found=%d", i, j)
147 }
148 })
149 }
150 })
151 }
152 }
153
154 func BenchmarkIndexPair(b *testing.B) {
155 for _, size := range indexPairSizes {
156 input := make([]byte, 16*1024)
157 for i := range input {
158 input[i] = byte(i)
159 }
160
161 if size%len(input) != 0 {
162 input = input[:(len(input)/size)*size]
163 }
164
165 b.Run(fmt.Sprintf("N=%d", size), func(b *testing.B) {
166 b.SetBytes(int64(len(input)))
167
168 for i := 0; i < b.N; i++ {
169 n := IndexPair(input, size)
170 if n != -1 {
171 b.Fatal("unexpected result:", n)
172 }
173 }
174 })
175 }
176 }