1 // Copyright 2016 The Gorilla WebSocket Authors. All rights reserved. Use of
2 // this source code is governed by a BSD-style license that can be found in the
14 func maskBytesByByte(key [4]byte, pos int, b []byte) int {
22 func notzero(b []byte) int {
31 func TestMaskBytes(t *testing.T) {
32 key := [4]byte{1, 2, 3, 4}
33 for size := 1; size <= 1024; size++ {
34 for align := 0; align < wordSize; align++ {
35 for pos := 0; pos < 4; pos++ {
36 b := make([]byte, size+align)[align:]
37 maskBytes(key, pos, b)
38 maskBytesByByte(key, pos, b)
39 if i := notzero(b); i >= 0 {
40 t.Errorf("size:%d, align:%d, pos:%d, offset:%d", size, align, pos, i)
47 func BenchmarkMaskBytes(b *testing.B) {
48 for _, size := range []int{2, 4, 8, 16, 32, 512, 1024} {
49 b.Run(fmt.Sprintf("size-%d", size), func(b *testing.B) {
50 for _, align := range []int{wordSize / 2} {
51 b.Run(fmt.Sprintf("align-%d", align), func(b *testing.B) {
52 for _, fn := range []struct {
54 fn func(key [4]byte, pos int, b []byte) int
56 {"byte", maskBytesByByte},
59 b.Run(fn.name, func(b *testing.B) {
61 data := make([]byte, size+align)[align:]
62 for i := 0; i < b.N; i++ {
65 b.SetBytes(int64(len(data)))