Files
hakurei/internal/zstd/fse_test.go
T
cat 390f3b2dae
Test / Create distribution (push) Successful in 57s
Test / Sandbox (push) Successful in 3m2s
Test / ShareFS (push) Successful in 4m3s
Test / Hakurei (push) Successful in 4m16s
Test / Sandbox (race detector) (push) Successful in 5m37s
Test / Hakurei (race detector) (push) Successful in 6m49s
Test / Flake checks (push) Successful in 1m11s
internal/pkg: decompress zstd streams
This uses internal/zstd until compress/zstd becomes available.

Signed-off-by: Ophestra <cat@gensokyo.uk>
2026-07-23 22:18:06 +09:00

89 lines
2.6 KiB
Go

// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zstd
import (
"slices"
"testing"
)
// literalPredefinedDistribution is the predefined distribution table
// for literal lengths. RFC 3.1.1.3.2.2.1.
var literalPredefinedDistribution = []int16{
4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 1, 1, 1, 1, 1,
-1, -1, -1, -1,
}
// offsetPredefinedDistribution is the predefined distribution table
// for offsets. RFC 3.1.1.3.2.2.3.
var offsetPredefinedDistribution = []int16{
1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
}
// matchPredefinedDistribution is the predefined distribution table
// for match lengths. RFC 3.1.1.3.2.2.2.
var matchPredefinedDistribution = []int16{
1, 4, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1,
-1, -1, -1, -1, -1,
}
// TestPredefinedTables verifies that we can generate the predefined
// literal/offset/match tables from the input data in RFC 8878.
// This serves as a test of the predefined tables, and also of buildFSE
// and the functions that make baseline FSE tables.
func TestPredefinedTables(t *testing.T) {
tests := []struct {
name string
distribution []int16
tableBits int
toBaseline func(*Reader, int, []fseEntry, []fseBaselineEntry) error
predef []fseBaselineEntry
}{
{
name: "literal",
distribution: literalPredefinedDistribution,
tableBits: 6,
toBaseline: (*Reader).makeLiteralBaselineFSE,
predef: predefinedLiteralTable[:],
},
{
name: "offset",
distribution: offsetPredefinedDistribution,
tableBits: 5,
toBaseline: (*Reader).makeOffsetBaselineFSE,
predef: predefinedOffsetTable[:],
},
{
name: "match",
distribution: matchPredefinedDistribution,
tableBits: 6,
toBaseline: (*Reader).makeMatchBaselineFSE,
predef: predefinedMatchTable[:],
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var r Reader
table := make([]fseEntry, 1<<test.tableBits)
if err := r.buildFSE(0, test.distribution, table, test.tableBits); err != nil {
t.Fatal(err)
}
baselineTable := make([]fseBaselineEntry, len(table))
if err := test.toBaseline(&r, 0, table, baselineTable); err != nil {
t.Fatal(err)
}
if !slices.Equal(baselineTable, test.predef) {
t.Errorf("got %v, want %v", baselineTable, test.predef)
}
})
}
}