/
uzer_007
/
gobase64
Обзор
Документация
Войти
/
uzer_007
/
gobase64
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
decode_fallback_alloc_test.go
97 строк
3 KB
uzer_007
release: v1.1.0
12 авг 2026, 01:34
12 авг 2026, 01:34
b80d01d
Код
Авторство
О чём код?
//go:build !race package base64 import ( "bytes" stdbase64 "encoding/base64" "testing" ) func TestDecodeStringFallbackAllocations(t *testing.T) { detected := cpuFeatures paths := append( []forcedCPUPathCase{{name: "public", features: detected}}, forcedCPUPathCases(detected)..., ) for _, path := range paths { t.Run(path.name, func(t *testing.T) { if path.skip { t.Skip("CPU does not support forced path") } withCPUFeatures(t, path.features, func() { testDecodeFallbackAllocations(t) }) }) } } func testDecodeFallbackAllocations(t *testing.T) { for _, size := range []int{1, 2, 3, 8, 16, 32, 64, 128, 512, 1024, 64 << 10} { src := benchData(size) encoded := []byte(stdbase64.StdEncoding.EncodeToString(src)) badIndex := len(encoded) - 1 for badIndex > 0 && encoded[badIndex] == '=' { badIndex-- } encoded[badIndex] = '!' input := string(encoded) want, wantErr := stdbase64.StdEncoding.DecodeString(input) got, gotErr := StdEncoding.DecodeString(input) if !bytes.Equal(got, want) || !sameDecodeErr(gotErr, wantErr) { t.Fatalf("malformed size=%d = %x/%v, want %x/%v", size, got, gotErr, want, wantErr) } got, gotErr = StdEncoding.DecodeToBytes(encoded) if !bytes.Equal(got, want) || !sameDecodeErr(gotErr, wantErr) { t.Fatalf("DecodeToBytes malformed size=%d = %x/%v, want %x/%v", size, got, gotErr, want, wantErr) } stdlibAllocs := testing.AllocsPerRun(20, func() { benchBytesSink, benchErrSink = stdbase64.StdEncoding.DecodeString(input) }) gobaseAllocs := testing.AllocsPerRun(20, func() { benchBytesSink, benchErrSink = StdEncoding.DecodeString(input) }) if gobaseAllocs > stdlibAllocs { t.Fatalf("malformed size=%d allocations = %.0f, stdlib %.0f", size, gobaseAllocs, stdlibAllocs) } gobaseAllocs = testing.AllocsPerRun(20, func() { benchBytesSink, benchErrSink = StdEncoding.DecodeToBytes(encoded) }) if gobaseAllocs > stdlibAllocs { t.Fatalf("DecodeToBytes malformed size=%d allocations = %.0f, stdlib %.0f", size, gobaseAllocs, stdlibAllocs) } } src := benchData(64 << 10) encoded := stdbase64.StdEncoding.EncodeToString(src) mid := len(encoded) / 2 &^ 3 input := encoded[:mid] + "\r\n\r\n" + encoded[mid:] inputBytes := []byte(input) want, wantErr := stdbase64.StdEncoding.DecodeString(input) got, gotErr := StdEncoding.DecodeString(input) if !bytes.Equal(got, want) || !sameDecodeErr(gotErr, wantErr) { t.Fatalf("CRLF = %x/%v, want %x/%v", got, gotErr, want, wantErr) } got, gotErr = StdEncoding.DecodeToBytes(inputBytes) if !bytes.Equal(got, want) || !sameDecodeErr(gotErr, wantErr) { t.Fatalf("DecodeToBytes CRLF = %x/%v, want %x/%v", got, gotErr, want, wantErr) } stdlibAllocs := testing.AllocsPerRun(20, func() { benchBytesSink, benchErrSink = stdbase64.StdEncoding.DecodeString(input) }) gobaseAllocs := testing.AllocsPerRun(20, func() { benchBytesSink, benchErrSink = StdEncoding.DecodeString(input) }) if gobaseAllocs > stdlibAllocs { t.Fatalf("CRLF allocations = %.0f, stdlib %.0f", gobaseAllocs, stdlibAllocs) } gobaseAllocs = testing.AllocsPerRun(20, func() { benchBytesSink, benchErrSink = StdEncoding.DecodeToBytes(inputBytes) }) if gobaseAllocs > stdlibAllocs { t.Fatalf("DecodeToBytes CRLF allocations = %.0f, stdlib %.0f", gobaseAllocs, stdlibAllocs) } }