Commit 7f983f2f authored by Nigel Tao's avatar Nigel Tao

compress/flate: simplify the TestDegenerateHuffmanCoding data.

Change-Id: I223a4bd6e3ee31324b46ac79a4022e40f1868491
Reviewed-on: https://go-review.googlesource.com/8995Reviewed-by: 's avatarMatthew Dempsky <mdempsky@google.com>
parent 4a7e5bca
...@@ -10,8 +10,8 @@ package flate ...@@ -10,8 +10,8 @@ package flate
import ( import (
"bytes" "bytes"
"encoding/hex"
"io/ioutil" "io/ioutil"
"strings"
"testing" "testing"
) )
...@@ -91,50 +91,28 @@ func TestInvalidBits(t *testing.T) { ...@@ -91,50 +91,28 @@ func TestInvalidBits(t *testing.T) {
} }
func TestDegenerateHuffmanCoding(t *testing.T) { func TestDegenerateHuffmanCoding(t *testing.T) {
// This test case is notable because:
// 1. It's decompressable by zlib.
// 2. It was generated by Go 1.4's compress/flate package.
// 3. It uses a degenerate dynamic Huffman coding block.
//
// The input is somewhat contrived though. It's a sequence of
// 258 bytes with no 3+ byte sequence occuring more than once,
// except that the whole sequence is repeated twice. This
// results in package flate emitting a single match token,
// which consequently means a single symbol in the distance
// coding table.
//
// Additionally, it uses very few unique byte values so that
// the overhead from storing the dynamic Huffman coding still
// results in a smaller encoding than using the fixed Huffman
// coding.
const ( const (
originalHalf = "00013534215002452243512505010034133042401113" + want = "abcabc"
"400415101454022532410254513251155411055331124453555" + // This compressed form has a dynamic Huffman block, even though a
"023120320201523334303524252551414033503012344230210" + // sensible encoder would use a literal data block, as the latter is
"310431305153005314321221315440455204052144332205422" + // shorter. Still, it is a valid flate compression of "abcabc". It has
"235434504441211420062622646656236416326065565261624" + // a degenerate Huffman table with only one coded value: the one
"6256136546" // non-literal back-ref copy of the first "abc" to the second "abc".
compressedHex = "ecd081000030104251a5fad5f9a34d640a4f92b3144" + //
"fa28366669a2ca54e542adba954cf7257c1422dd639ccde6a6b" + // To verify that this is decompressible with zlib (the C library),
"4b6cda659b885110f248d228a38ccd75954c91494b8415ab713" + // it's easy to use the Python wrapper library:
"42fd2e20683e3b5ea86aae13601ad40d6746a6bec221d07d7bb" + // >>> import zlib
"1db9fac2e9b61be7a3c7ceb9f5bec00b0000ffffecd08100003" + // >>> compressed = "\x0c\xc2...etc...\xff\xff"
"0104251a5fad5f9a34d640a4f92b3144fa28366669a2ca54e54" + // >>> zlib.decompress(compressed, -15) # negative means no GZIP header.
"2adba954cf7257c1422dd639ccde6a6b4b6cda659b885110f24" + // 'abcabc'
"8d228a38ccd75954c91494b8415ab71342fd2e20683e3b5ea86" + compressed = "\x0c\xc2\x01\x0d\x00\x00\x00\x82\xb0\xac\x4a\xff\x0e\xb0\x7d\x27" +
"aae13601ad40d6746a6bec221d07d7bb1db9fac2e9b61be7a3c" + "\x06\x00\x00\xff\xff"
"7ceb9f5bec00b0000ffff"
) )
b, err := ioutil.ReadAll(NewReader(strings.NewReader(compressed)))
compressed, err := hex.DecodeString(compressedHex)
if err != nil {
t.Fatal(err)
}
data, err := ioutil.ReadAll(NewReader(bytes.NewReader(compressed)))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if string(data) != originalHalf+originalHalf { if got := string(b); got != want {
t.Fatal("Decompressed data does not match original") t.Fatalf("got %q, want %q", got, want)
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment