| 1 |
package flate |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"os" |
| 6 |
"testing" |
| 7 |
) |
| 8 |
|
| 9 |
type testFatal interface { |
| 10 |
Fatal(args ...any) |
| 11 |
} |
| 12 |
|
| 13 |
// loadTestTokens will load test tokens. |
| 14 |
// First block from enwik9, varint encoded. |
| 15 |
func loadTestTokens(t testFatal) *tokens { |
| 16 |
b, err := os.ReadFile("testdata/tokens.bin") |
| 17 |
if err != nil { |
| 18 |
t.Fatal(err) |
| 19 |
} |
| 20 |
var tokens tokens |
| 21 |
err = tokens.FromVarInt(b) |
| 22 |
if err != nil { |
| 23 |
t.Fatal(err) |
| 24 |
} |
| 25 |
return &tokens |
| 26 |
} |
| 27 |
|
| 28 |
func Test_tokens_EstimatedBits(t *testing.T) { |
| 29 |
tok := loadTestTokens(t) |
| 30 |
// The estimated size, update if method changes. |
| 31 |
const expect = 221057 |
| 32 |
n := tok.EstimatedBits() |
| 33 |
var buf bytes.Buffer |
| 34 |
wr := newHuffmanBitWriter(&buf) |
| 35 |
wr.writeBlockDynamic(tok, true, nil, true) |
| 36 |
if wr.err != nil { |
| 37 |
t.Fatal(wr.err) |
| 38 |
} |
| 39 |
wr.flush() |
| 40 |
t.Log("got:", n, "actual:", buf.Len()*8, "(header not part of estimate)") |
| 41 |
if n != expect { |
| 42 |
t.Error("want:", expect, "bits, got:", n) |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
func Benchmark_tokens_EstimatedBits(b *testing.B) { |
| 47 |
tok := loadTestTokens(b) |
| 48 |
b.ResetTimer() |
| 49 |
// One "byte", one token iteration. |
| 50 |
b.SetBytes(1) |
| 51 |
for i := 0; i < b.N; i++ { |
| 52 |
_ = tok.EstimatedBits() |
| 53 |
} |
| 54 |
} |