1package flate 2 3import ( 4 "bytes" 5 "io/ioutil" 6 "testing" 7) 8 9type testFatal interface { 10 Fatal(args ...interface{}) 11} 12 13// loadTestTokens will load test tokens. 14// First block from enwik9, varint encoded. 15func loadTestTokens(t testFatal) *tokens { 16 b, err := ioutil.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 28func 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 46func 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} 55