This repository has been archived by the owner on Sep 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
popcount_slices_amd64_test.go
227 lines (185 loc) · 5.99 KB
/
popcount_slices_amd64_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//
// Package hamming distance calculations in Go
//
// https://github.com/steakknife/hamming
//
// Copyright © 2014, 2015, 2016, 2018 Barry Allard
//
// MIT license
//
package hamming
import (
"reflect"
"strconv"
"testing"
"unsafe"
)
func TestCountBitsInt8sPopCnt(t *testing.T) {
for _, c := range testSliceCases() {
bHdr := *(*reflect.SliceHeader)(unsafe.Pointer(&c.b))
bHdr.Len *= 64 / 8
bHdr.Cap *= 64 / 8
b := *(*[]int8)(unsafe.Pointer(&bHdr))
if actualN := CountBitsInt8sPopCnt(b); actualN != c.n {
t.Errorf("(%d) -> %d != %d", b, actualN, c.n)
}
}
t0, t0Cnt := []int8{1, 2, 3, 4, 5}, 1+1+2+1+2
if actualN := CountBitsInt8sPopCnt(t0); actualN != t0Cnt {
t.Errorf("(%d) -> %d != %d", t0, actualN, t0Cnt)
}
t1, t1Cnt := []int8{1, 2, 3, 4, 5, 7}, 1+1+2+1+2+3
if actualN := CountBitsInt8sPopCnt(t1); actualN != t1Cnt {
t.Errorf("(%d) -> %d != %d", t1, actualN, t1Cnt)
}
t2, t2Cnt := []int8{1, 2, 3, 4, 5, 7, -1}, 1+1+2+1+2+3+8
if actualN := CountBitsInt8sPopCnt(t2); actualN != t2Cnt {
t.Errorf("(%d) -> %d != %d", t2, actualN, t2Cnt)
}
t3, t3Cnt := []int8{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 4, 5, 7, -1},
1+1+2+1+2+3+8+1+1
if actualN := CountBitsInt8sPopCnt(t3); actualN != t3Cnt {
t.Errorf("(%d) -> %d != %d", t3, actualN, t3Cnt)
}
}
func TestCountBitsInt16sPopCnt(t *testing.T) {
for _, c := range testSliceCases() {
bHdr := *(*reflect.SliceHeader)(unsafe.Pointer(&c.b))
bHdr.Len *= 64 / 16
bHdr.Cap *= 64 / 16
b := *(*[]int16)(unsafe.Pointer(&bHdr))
if actualN := CountBitsInt16sPopCnt(b); actualN != c.n {
t.Errorf("(%d) -> %d != %d", b, actualN, c.n)
}
}
}
func TestCountBitsInt32sPopCnt(t *testing.T) {
for _, c := range testSliceCases() {
bHdr := *(*reflect.SliceHeader)(unsafe.Pointer(&c.b))
bHdr.Len *= 64 / 32
bHdr.Cap *= 64 / 32
b := *(*[]int32)(unsafe.Pointer(&bHdr))
if actualN := CountBitsInt32sPopCnt(b); actualN != c.n {
t.Errorf("(%d) -> actual %d != expected %d", b, actualN, c.n)
} else {
t.Logf("(%d) -> actual %d == expected %d", b, actualN, c.n)
}
}
}
func TestCountBitsInt64sPopCnt(t *testing.T) {
for _, c := range testSliceCases() {
b := *(*[]int64)(unsafe.Pointer(&c.b))
if actualN := CountBitsInt64sPopCnt(b); actualN != c.n {
t.Errorf("(%d) -> %d != %d", b, actualN, c.n)
}
}
}
func TestCountBitsIntsPopCnt(t *testing.T) {
for _, c := range testSliceCases() {
bHdr := *(*reflect.SliceHeader)(unsafe.Pointer(&c.b))
bHdr.Len *= 64 / strconv.IntSize
bHdr.Cap *= 64 / strconv.IntSize
b := *(*[]int)(unsafe.Pointer(&bHdr))
if actualN := CountBitsIntsPopCnt(b); actualN != c.n {
t.Errorf("(%d) -> actual %d != expected %d", b, actualN, c.n)
} else {
t.Logf("(%d) -> actual %d == expected %d", b, actualN, c.n)
}
}
}
func TestCountBitsUint8sPopCnt(t *testing.T) {
for _, c := range testSliceCases() {
bHdr := *(*reflect.SliceHeader)(unsafe.Pointer(&c.b))
bHdr.Len *= 64 / 8
bHdr.Cap *= 64 / 8
b := *(*[]uint8)(unsafe.Pointer(&bHdr))
if actualN := CountBitsUint8sPopCnt(b); actualN != c.n {
t.Errorf("(%d) -> actual %d != expected %d", b, actualN, c.n)
} else {
t.Logf("(%d) -> actual %d == expected %d", b, actualN, c.n)
}
}
}
func TestCountBitsUint16sPopCnt(t *testing.T) {
for _, c := range testSliceCases() {
bHdr := *(*reflect.SliceHeader)(unsafe.Pointer(&c.b))
bHdr.Len *= 64 / 16
bHdr.Cap *= 64 / 16
b := *(*[]uint16)(unsafe.Pointer(&bHdr))
if actualN := CountBitsUint16sPopCnt(b); actualN != c.n {
t.Errorf("(%d) -> actual %d != expected %d", b, actualN, c.n)
} else {
t.Logf("(%d) -> actual %d == expected %d", b, actualN, c.n)
}
}
}
func TestCountBitsUint32sPopCnt(t *testing.T) {
for _, c := range testSliceCases() {
bHdr := *(*reflect.SliceHeader)(unsafe.Pointer(&c.b))
bHdr.Len *= 64 / 32
bHdr.Cap *= 64 / 32
b := *(*[]uint32)(unsafe.Pointer(&bHdr))
if actualN := CountBitsUint32sPopCnt(b); actualN != c.n {
t.Errorf("(%d) -> actual %d != expected %d", b, actualN, c.n)
} else {
t.Logf("(%d) -> actual %d == expected %d", b, actualN, c.n)
}
}
}
func TestCountBitsUint64sPopCnt(t *testing.T) {
for _, c := range testSliceCases() {
if actualN := CountBitsUint64sPopCnt(c.b); actualN != c.n {
t.Errorf("(%d) -> actual %d != expected %d", c.b, actualN, c.n)
} else {
t.Logf("(%d) -> actual %d == expected %d", c.b, actualN, c.n)
}
}
}
func TestCountBitsUintsPopCnt(t *testing.T) {
for _, c := range testSliceCases() {
bHdr := *(*reflect.SliceHeader)(unsafe.Pointer(&c.b))
bHdr.Len *= 64 / strconv.IntSize
bHdr.Cap *= 64 / strconv.IntSize
b := *(*[]uint)(unsafe.Pointer(&bHdr))
if actualN := CountBitsUintsPopCnt(b); actualN != c.n {
t.Errorf("(%d) -> %d != %d", b, actualN, c.n)
}
}
}
func TestCountBitsBytesPopCnt(t *testing.T) {
for _, c := range testSliceCases() {
bHdr := *(*reflect.SliceHeader)(unsafe.Pointer(&c.b))
bHdr.Len *= 64 / 8
bHdr.Cap *= 64 / 8
b := *(*[]byte)(unsafe.Pointer(&bHdr))
if actualN := CountBitsBytesPopCnt(b); actualN != c.n {
t.Errorf("(%d) -> %d != %d", b, actualN, c.n)
}
}
}
func TestCountBitsRunesPopCnt(t *testing.T) {
for _, c := range testSliceCases() {
bHdr := *(*reflect.SliceHeader)(unsafe.Pointer(&c.b))
bHdr.Len *= 64 / 32
bHdr.Cap *= 64 / 32
b := *(*[]rune)(unsafe.Pointer(&bHdr))
if actualN := CountBitsRunesPopCnt(b); actualN != c.n {
t.Errorf("(%d) -> %d != %d", b, actualN, c.n)
}
}
}
func TestCountBitsStringPopCnt(t *testing.T) {
for _, c := range testSliceCases() {
bHdr := *(*reflect.SliceHeader)(unsafe.Pointer(&c.b))
b := *(*string)(unsafe.Pointer(&reflect.StringHeader{
Data: bHdr.Data,
Len: bHdr.Len * 64 / 8}))
if actualN := CountBitsStringPopCnt(b); actualN != c.n {
t.Errorf("(%v) -> %d != %d", []byte(b), actualN, c.n)
}
}
}