aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/segmentio/asm/bswap/swap64_test.go
blob: ff5539be184c74e5b0c12e535e624950bd5efab9 (plain) (blame)
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
package bswap

import (
	"encoding/binary"
	"io"
	"math/rand"
	"testing"
)

func TestSwap64(t *testing.T) {
	input := make([]byte, 4096)
	prng := rand.New(rand.NewSource(0))
	io.ReadFull(prng, input)

	output := make([]byte, 4096)

	for i := 0; i < 4096; i += 8 {
		copy(output, input)
		Swap64(output[:i])
		for j := 0; j < i; j += 8 {
			u1 := binary.BigEndian.Uint64(input[j:])
			u2 := binary.LittleEndian.Uint64(output[j:])
			if u1 != u2 {
				t.Fatalf("bytes weren't swapped at offset %d: %v / %v", i, u1, u2)
			}
		}
	}
}

func BenchmarkSwap64(b *testing.B) {
	input := make([]byte, 64*1024)
	prng := rand.New(rand.NewSource(0))
	io.ReadFull(prng, input)

	b.SetBytes(int64(len(input)))
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		Swap64(input)
	}
}