| 1 |
package slices |
| 2 |
|
| 3 |
import ( |
| 4 |
"math/rand" |
| 5 |
"testing" |
| 6 |
) |
| 7 |
|
| 8 |
var size = 1024 * 1024 |
| 9 |
|
| 10 |
func TestSumUint8(t *testing.T) { |
| 11 |
x, y := generateUint8Slices() |
| 12 |
genericXCopy := make([]uint8, len(x)) |
| 13 |
copy(genericXCopy, x) |
| 14 |
sumUint8(x, y) |
| 15 |
sumUint8Generic(genericXCopy, y) |
| 16 |
for i := 0; i < len(x); i++ { |
| 17 |
if x[i] != genericXCopy[i] { |
| 18 |
t.Fatalf("mismatch sums at index %d, expected %d : got %d", i, genericXCopy[i], x[i]) |
| 19 |
} |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
func TestSumUint8YLarger(t *testing.T) { |
| 24 |
x, y := generateUint8Slices() |
| 25 |
y = append(y, uint8(100)) |
| 26 |
genericXCopy := make([]uint8, len(x)) |
| 27 |
copy(genericXCopy, x) |
| 28 |
sumUint8(x, y) |
| 29 |
sumUint8Generic(genericXCopy, y) |
| 30 |
for i := 0; i < len(x); i++ { |
| 31 |
if x[i] != genericXCopy[i] { |
| 32 |
t.Fatalf("mismatch sums at index %d, expected %d : got %d", i, genericXCopy[i], x[i]) |
| 33 |
} |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
func TestSumUint8XLarger(t *testing.T) { |
| 38 |
x, y := generateUint8Slices() |
| 39 |
x = append(x, uint8(100)) |
| 40 |
genericXCopy := make([]uint8, len(x)) |
| 41 |
copy(genericXCopy, x) |
| 42 |
sumUint8(x, y) |
| 43 |
sumUint8Generic(genericXCopy, y) |
| 44 |
for i := 0; i < len(x); i++ { |
| 45 |
if x[i] != genericXCopy[i] { |
| 46 |
t.Fatalf("mismatch sums at index %d, expected %d : got %d", i, genericXCopy[i], x[i]) |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
func TestSumUint16(t *testing.T) { |
| 52 |
x, y := generateUint16Slices() |
| 53 |
genericXCopy := make([]uint16, len(x)) |
| 54 |
copy(genericXCopy, x) |
| 55 |
sumUint16(x, y) |
| 56 |
sumUint16Generic(genericXCopy, y) |
| 57 |
for i := 0; i < len(x); i++ { |
| 58 |
if x[i] != genericXCopy[i] { |
| 59 |
t.Fatalf("mismatch sums at index %d, expected %d : got %d", i, genericXCopy[i], x[i]) |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
func TestSumUint16YLarger(t *testing.T) { |
| 65 |
x, y := generateUint16Slices() |
| 66 |
y = append(y, uint16(100)) |
| 67 |
genericXCopy := make([]uint16, len(x)) |
| 68 |
copy(genericXCopy, x) |
| 69 |
sumUint16(x, y) |
| 70 |
sumUint16Generic(genericXCopy, y) |
| 71 |
for i := 0; i < len(x); i++ { |
| 72 |
if x[i] != genericXCopy[i] { |
| 73 |
t.Fatalf("mismatch sums at index %d, expected %d : got %d", i, genericXCopy[i], x[i]) |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
func TestSumUint16XLarger(t *testing.T) { |
| 79 |
x, y := generateUint16Slices() |
| 80 |
x = append(x, uint16(100)) |
| 81 |
genericXCopy := make([]uint16, len(x)) |
| 82 |
copy(genericXCopy, x) |
| 83 |
sumUint16(x, y) |
| 84 |
sumUint16Generic(genericXCopy, y) |
| 85 |
for i := 0; i < len(x); i++ { |
| 86 |
if x[i] != genericXCopy[i] { |
| 87 |
t.Fatalf("mismatch sums at index %d, expected %d : got %d", i, genericXCopy[i], x[i]) |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
func TestSumUint32(t *testing.T) { |
| 93 |
x, y := generateUint32Slices() |
| 94 |
genericXCopy := make([]uint32, len(x)) |
| 95 |
copy(genericXCopy, x) |
| 96 |
sumUint32(x, y) |
| 97 |
sumUint32Generic(genericXCopy, y) |
| 98 |
for i := 0; i < len(x); i++ { |
| 99 |
if x[i] != genericXCopy[i] { |
| 100 |
t.Fatalf("mismatch sums at index %d, expected %d : got %d", i, genericXCopy[i], x[i]) |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
func TestSumUint32YLarger(t *testing.T) { |
| 106 |
x, y := generateUint32Slices() |
| 107 |
y = append(y, uint32(100)) |
| 108 |
genericXCopy := make([]uint32, len(x)) |
| 109 |
copy(genericXCopy, x) |
| 110 |
sumUint32(x, y) |
| 111 |
sumUint32Generic(genericXCopy, y) |
| 112 |
for i := 0; i < len(x); i++ { |
| 113 |
if x[i] != genericXCopy[i] { |
| 114 |
t.Fatalf("mismatch sums at index %d, expected %d : got %d", i, genericXCopy[i], x[i]) |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
func TestSumUint32XLarger(t *testing.T) { |
| 120 |
x, y := generateUint32Slices() |
| 121 |
x = append(x, uint32(100)) |
| 122 |
genericXCopy := make([]uint32, len(x)) |
| 123 |
copy(genericXCopy, x) |
| 124 |
sumUint32(x, y) |
| 125 |
sumUint32Generic(genericXCopy, y) |
| 126 |
for i := 0; i < len(x); i++ { |
| 127 |
if x[i] != genericXCopy[i] { |
| 128 |
t.Fatalf("mismatch sums at index %d, expected %d : got %d", i, genericXCopy[i], x[i]) |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
func TestSumUint64(t *testing.T) { |
| 134 |
x, y := generateUint64Slices() |
| 135 |
genericXCopy := make([]uint64, len(x)) |
| 136 |
copy(genericXCopy, x) |
| 137 |
sumUint64(x, y) |
| 138 |
sumUint64Generic(genericXCopy, y) |
| 139 |
for i := 0; i < len(x); i++ { |
| 140 |
if x[i] != genericXCopy[i] { |
| 141 |
t.Fatalf("mismatch sums at index %d, expected %d : got %d", i, genericXCopy[i], x[i]) |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
func TestSumUint64YLarger(t *testing.T) { |
| 147 |
x, y := generateUint64Slices() |
| 148 |
y = append(y, uint64(100)) |
| 149 |
genericXCopy := make([]uint64, len(x)) |
| 150 |
copy(genericXCopy, x) |
| 151 |
sumUint64(x, y) |
| 152 |
sumUint64Generic(genericXCopy, y) |
| 153 |
for i := 0; i < len(x); i++ { |
| 154 |
if x[i] != genericXCopy[i] { |
| 155 |
t.Fatalf("mismatch sums at index %d, expected %d : got %d", i, genericXCopy[i], x[i]) |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
func TestSumUint64XLarger(t *testing.T) { |
| 161 |
x, y := generateUint64Slices() |
| 162 |
x = append(x, uint64(100)) |
| 163 |
genericXCopy := make([]uint64, len(x)) |
| 164 |
copy(genericXCopy, x) |
| 165 |
sumUint64(x, y) |
| 166 |
sumUint64Generic(genericXCopy, y) |
| 167 |
for i := 0; i < len(x); i++ { |
| 168 |
if x[i] != genericXCopy[i] { |
| 169 |
t.Fatalf("mismatch sums at index %d, expected %d : got %d", i, genericXCopy[i], x[i]) |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
func generateUint8Slices() ([]uint8, []uint8) { |
| 175 |
var x []uint8 |
| 176 |
var y []uint8 |
| 177 |
for i := 0; i < size; i++ { |
| 178 |
x = append(x, uint8(i)) |
| 179 |
y = append(y, uint8(i)) |
| 180 |
} |
| 181 |
return x, y |
| 182 |
} |
| 183 |
|
| 184 |
func generateUint16Slices() ([]uint16, []uint16) { |
| 185 |
var x []uint16 |
| 186 |
var y []uint16 |
| 187 |
for i := 0; i < size; i++ { |
| 188 |
x = append(x, uint16(i)) |
| 189 |
y = append(y, uint16(i)) |
| 190 |
} |
| 191 |
return x, y |
| 192 |
} |
| 193 |
|
| 194 |
func generateUint32Slices() ([]uint32, []uint32) { |
| 195 |
var x []uint32 |
| 196 |
var y []uint32 |
| 197 |
prng := rand.New(rand.NewSource(0)) |
| 198 |
for i := 0; i < size; i++ { |
| 199 |
x = append(x, prng.Uint32()) |
| 200 |
y = append(y, prng.Uint32()) |
| 201 |
} |
| 202 |
return x, y |
| 203 |
} |
| 204 |
|
| 205 |
func generateUint64Slices() ([]uint64, []uint64) { |
| 206 |
var x []uint64 |
| 207 |
var y []uint64 |
| 208 |
prng := rand.New(rand.NewSource(0)) |
| 209 |
for i := 0; i < size; i++ { |
| 210 |
x = append(x, prng.Uint64()) |
| 211 |
y = append(y, prng.Uint64()) |
| 212 |
} |
| 213 |
return x, y |
| 214 |
} |
| 215 |
|
| 216 |
func BenchmarkSumUnit8(b *testing.B) { |
| 217 |
x, y := generateUint8Slices() |
| 218 |
for i := 0; i < b.N; i++ { |
| 219 |
SumUint8(x, y) |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
func BenchmarkSumUnit8Generic(b *testing.B) { |
| 224 |
x, y := generateUint8Slices() |
| 225 |
for i := 0; i < b.N; i++ { |
| 226 |
sumUint8Generic(x, y) |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
func BenchmarkSumUnit16(b *testing.B) { |
| 231 |
x, y := generateUint16Slices() |
| 232 |
for i := 0; i < b.N; i++ { |
| 233 |
SumUint16(x, y) |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
func BenchmarkSumUnit16Generic(b *testing.B) { |
| 238 |
x, y := generateUint16Slices() |
| 239 |
for i := 0; i < b.N; i++ { |
| 240 |
sumUint16Generic(x, y) |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
func BenchmarkSumUnit32(b *testing.B) { |
| 245 |
x, y := generateUint32Slices() |
| 246 |
for i := 0; i < b.N; i++ { |
| 247 |
SumUint32(x, y) |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
func BenchmarkSumUnit32Generic(b *testing.B) { |
| 252 |
x, y := generateUint32Slices() |
| 253 |
for i := 0; i < b.N; i++ { |
| 254 |
sumUint32Generic(x, y) |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
func BenchmarkSumUnit64(b *testing.B) { |
| 259 |
x, y := generateUint64Slices() |
| 260 |
for i := 0; i < b.N; i++ { |
| 261 |
SumUint64(x, y) |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
func BenchmarkSumUnit64Generic(b *testing.B) { |
| 266 |
x, y := generateUint64Slices() |
| 267 |
for i := 0; i < b.N; i++ { |
| 268 |
sumUint64Generic(x, y) |
| 269 |
} |
| 270 |
} |