| 1 |
package bswap |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/binary" |
| 5 |
"io" |
| 6 |
"math/rand" |
| 7 |
"testing" |
| 8 |
) |
| 9 |
|
| 10 |
func TestSwap64(t *testing.T) { |
| 11 |
input := make([]byte, 4096) |
| 12 |
prng := rand.New(rand.NewSource(0)) |
| 13 |
io.ReadFull(prng, input) |
| 14 |
|
| 15 |
output := make([]byte, 4096) |
| 16 |
|
| 17 |
for i := 0; i < 4096; i += 8 { |
| 18 |
copy(output, input) |
| 19 |
Swap64(output[:i]) |
| 20 |
for j := 0; j < i; j += 8 { |
| 21 |
u1 := binary.BigEndian.Uint64(input[j:]) |
| 22 |
u2 := binary.LittleEndian.Uint64(output[j:]) |
| 23 |
if u1 != u2 { |
| 24 |
t.Fatalf("bytes weren't swapped at offset %d: %v / %v", i, u1, u2) |
| 25 |
} |
| 26 |
} |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
func BenchmarkSwap64(b *testing.B) { |
| 31 |
input := make([]byte, 64*1024) |
| 32 |
prng := rand.New(rand.NewSource(0)) |
| 33 |
io.ReadFull(prng, input) |
| 34 |
|
| 35 |
b.SetBytes(int64(len(input))) |
| 36 |
b.ResetTimer() |
| 37 |
|
| 38 |
for i := 0; i < b.N; i++ { |
| 39 |
Swap64(input) |
| 40 |
} |
| 41 |
} |