| 1 |
//go:build (amd64 || arm64) && !purego |
| 2 |
// +build amd64 arm64 |
| 3 |
// +build !purego |
| 4 |
|
| 5 |
package base64 |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/segmentio/asm/internal/buffer" |
| 12 |
) |
| 13 |
|
| 14 |
func fillBuffers(b *buffer.Buffer, size int) map[string][]byte { |
| 15 |
bufs := map[string][]byte{ |
| 16 |
"head": b.ProtectHead(), |
| 17 |
"tail": b.ProtectTail(), |
| 18 |
} |
| 19 |
|
| 20 |
for _, buf := range bufs { |
| 21 |
for i := 0; i < size; i++ { |
| 22 |
buf[i] = (255 - byte(i&15)*16) - byte(i&255)/16 |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
return bufs |
| 27 |
} |
| 28 |
|
| 29 |
func TestEncodeASM(t *testing.T) { |
| 30 |
b, err := buffer.New(512) |
| 31 |
if err != nil { |
| 32 |
t.Fatal(err) |
| 33 |
} |
| 34 |
defer b.Release() |
| 35 |
|
| 36 |
bufs := fillBuffers(&b, 512) |
| 37 |
|
| 38 |
for _, enc := range encodings { |
| 39 |
if enc.candidate.enc == nil { |
| 40 |
t.Log("asm not enabled") |
| 41 |
continue |
| 42 |
} |
| 43 |
for name, buf := range bufs { |
| 44 |
ok := t.Run(fmt.Sprintf("%s-%s", enc.name, name), func(t *testing.T) { |
| 45 |
dst, err := buffer.New(enc.candidate.EncodedLen(len(buf))) |
| 46 |
if err != nil { |
| 47 |
t.Fatal(err) |
| 48 |
} |
| 49 |
defer dst.Release() |
| 50 |
|
| 51 |
_, ns := enc.candidate.enc(dst.ProtectTail(), buf, &enc.candidate.enclut[0]) |
| 52 |
|
| 53 |
if len(buf)-ns >= minEncodeLen { |
| 54 |
t.Errorf("encode remain should be less than %d, but is %d", minEncodeLen, len(buf)-ns) |
| 55 |
} |
| 56 |
}) |
| 57 |
|
| 58 |
if !ok { |
| 59 |
break |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
func TestDecodeASM(t *testing.T) { |
| 66 |
b, err := buffer.New(512) |
| 67 |
if err != nil { |
| 68 |
t.Fatal(err) |
| 69 |
} |
| 70 |
defer b.Release() |
| 71 |
|
| 72 |
bufs := fillBuffers(&b, 512) |
| 73 |
|
| 74 |
for _, enc := range encodings { |
| 75 |
if enc.candidate.dec == nil { |
| 76 |
t.Log("asm not enabled") |
| 77 |
continue |
| 78 |
} |
| 79 |
|
| 80 |
for name, buf := range bufs { |
| 81 |
ok := t.Run(fmt.Sprintf("%s-%s", enc.name, name), func(t *testing.T) { |
| 82 |
src := make([]byte, enc.candidate.EncodedLen(len(buf))) |
| 83 |
dst, err := buffer.New(len(buf)) |
| 84 |
if err != nil { |
| 85 |
t.Fatal(err) |
| 86 |
} |
| 87 |
defer dst.Release() |
| 88 |
|
| 89 |
enc.candidate.Encode(src, buf) |
| 90 |
|
| 91 |
_, ns := enc.candidate.dec(dst.ProtectTail(), src, &enc.candidate.declut[0]) |
| 92 |
|
| 93 |
if len(buf)-ns >= minDecodeLen { |
| 94 |
t.Errorf("decode remain should be less than %d, but is %d", minDecodeLen, len(buf)-ns) |
| 95 |
} |
| 96 |
}) |
| 97 |
|
| 98 |
if !ok { |
| 99 |
break |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
} |