File: base64_asm_test.go

package info (click to toggle)
golang-github-segmentio-asm 1.2.0%2Bgit20231107.1cfacc8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 932 kB
  • sloc: asm: 6,093; makefile: 32
file content (103 lines) | stat: -rw-r--r-- 2,037 bytes parent folder | download
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//go:build (amd64 || arm64) && !purego
// +build amd64 arm64
// +build !purego

package base64

import (
	"fmt"
	"testing"

	"github.com/segmentio/asm/internal/buffer"
)

func fillBuffers(b *buffer.Buffer, size int) map[string][]byte {
	bufs := map[string][]byte{
		"head": b.ProtectHead(),
		"tail": b.ProtectTail(),
	}

	for _, buf := range bufs {
		for i := 0; i < size; i++ {
			buf[i] = (255 - byte(i&15)*16) - byte(i&255)/16
		}
	}

	return bufs
}

func TestEncodeASM(t *testing.T) {
	b, err := buffer.New(512)
	if err != nil {
		t.Fatal(err)
	}
	defer b.Release()

	bufs := fillBuffers(&b, 512)

	for _, enc := range encodings {
		if enc.candidate.enc == nil {
			t.Log("asm not enabled")
			continue
		}
		for name, buf := range bufs {
			ok := t.Run(fmt.Sprintf("%s-%s", enc.name, name), func(t *testing.T) {
				dst, err := buffer.New(enc.candidate.EncodedLen(len(buf)))
				if err != nil {
					t.Fatal(err)
				}
				defer dst.Release()

				_, ns := enc.candidate.enc(dst.ProtectTail(), buf, &enc.candidate.enclut[0])

				if len(buf)-ns >= minEncodeLen {
					t.Errorf("encode remain should be less than %d, but is %d", minEncodeLen, len(buf)-ns)
				}
			})

			if !ok {
				break
			}
		}
	}
}

func TestDecodeASM(t *testing.T) {
	b, err := buffer.New(512)
	if err != nil {
		t.Fatal(err)
	}
	defer b.Release()

	bufs := fillBuffers(&b, 512)

	for _, enc := range encodings {
		if enc.candidate.dec == nil {
			t.Log("asm not enabled")
			continue
		}

		for name, buf := range bufs {
			ok := t.Run(fmt.Sprintf("%s-%s", enc.name, name), func(t *testing.T) {
				src := make([]byte, enc.candidate.EncodedLen(len(buf)))
				dst, err := buffer.New(len(buf))
				if err != nil {
					t.Fatal(err)
				}
				defer dst.Release()

				enc.candidate.Encode(src, buf)

				_, ns := enc.candidate.dec(dst.ProtectTail(), src, &enc.candidate.declut[0])

				if len(buf)-ns >= minDecodeLen {
					t.Errorf("decode remain should be less than %d, but is %d", minDecodeLen, len(buf)-ns)
				}
			})

			if !ok {
				break
			}
		}
	}
}