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
|
package flate
import (
"bytes"
"os"
"testing"
)
type testFatal interface {
Fatal(args ...interface{})
}
// loadTestTokens will load test tokens.
// First block from enwik9, varint encoded.
func loadTestTokens(t testFatal) *tokens {
b, err := os.ReadFile("testdata/tokens.bin")
if err != nil {
t.Fatal(err)
}
var tokens tokens
err = tokens.FromVarInt(b)
if err != nil {
t.Fatal(err)
}
return &tokens
}
func Test_tokens_EstimatedBits(t *testing.T) {
tok := loadTestTokens(t)
// The estimated size, update if method changes.
const expect = 221057
n := tok.EstimatedBits()
var buf bytes.Buffer
wr := newHuffmanBitWriter(&buf)
wr.writeBlockDynamic(tok, true, nil, true)
if wr.err != nil {
t.Fatal(wr.err)
}
wr.flush()
t.Log("got:", n, "actual:", buf.Len()*8, "(header not part of estimate)")
if n != expect {
t.Error("want:", expect, "bits, got:", n)
}
}
func Benchmark_tokens_EstimatedBits(b *testing.B) {
tok := loadTestTokens(b)
b.ResetTimer()
// One "byte", one token iteration.
b.SetBytes(1)
for i := 0; i < b.N; i++ {
_ = tok.EstimatedBits()
}
}
|