func TestEncoderCycle2(t *testing.T) { buf := new(bytes.Buffer) const txtlen = 50000 io.CopyN(buf, randtxt.NewReader(rand.NewSource(42)), txtlen) txt := buf.String() buf.Reset() const dictCap = MinDictCap m, err := newHashTable(dictCap, 4) if err != nil { t.Fatal(err) } encoderDict, err := newEncoderDict(dictCap, dictCap+1024, m) if err != nil { t.Fatal(err) } props := Properties{3, 0, 2} if err := props.verify(); err != nil { t.Fatalf("properties error %s", err) } state := newState(props) lbw := &LimitedByteWriter{BW: buf, N: 100} w, err := newEncoder(lbw, state, encoderDict, 0) if err != nil { t.Fatalf("NewEncoder error %s", err) } _, err = io.WriteString(w, txt) if err != nil && err != ErrLimit { t.Fatalf("WriteString error %s", err) } if err = w.Close(); err != nil { t.Fatalf("w.Close error %s", err) } n := w.Compressed() txt = txt[:n] decoderDict, err := newDecoderDict(dictCap) if err != nil { t.Fatalf("NewDecoderDict error %s", err) } state.Reset() r, err := newDecoder(buf, state, decoderDict, n) if err != nil { t.Fatalf("NewDecoder error %s", err) } out := new(bytes.Buffer) if _, err = io.Copy(out, r); err != nil { t.Fatalf("decompress copy error %s", err) } got := out.String() t.Logf("%s", got) if len(got) != int(n) { t.Fatalf("len(got) %d; want %d", len(got), n) } if got != txt { t.Fatalf("got and txt differ") } }
func TestWriter2(t *testing.T) { const txtlen = 1023 var buf bytes.Buffer io.CopyN(&buf, randtxt.NewReader(rand.NewSource(41)), txtlen) txt := buf.String() buf.Reset() w, err := NewWriter(&buf) if err != nil { t.Fatalf("NewWriter error %s", err) } n, err := io.WriteString(w, txt) if err != nil { t.Fatalf("WriteString error %s", err) } if n != len(txt) { t.Fatalf("WriteString wrote %d bytes; want %d", n, len(txt)) } if err = w.Close(); err != nil { t.Fatalf("Close error %s", err) } t.Logf("buf.Len() %d", buf.Len()) r, err := NewReader(&buf) if err != nil { t.Fatalf("NewReader error %s", err) } var out bytes.Buffer k, err := io.Copy(&out, r) if err != nil { t.Fatalf("Decompressing copy error %s after %d bytes", err, n) } if k != txtlen { t.Fatalf("Decompression data length %d; want %d", k, txtlen) } if txt != out.String() { t.Fatal("decompressed data differs from original") } }
func TestCycle2(t *testing.T) { buf := new(bytes.Buffer) w, err := Writer2Config{DictCap: 4096}.NewWriter2(buf) if err != nil { t.Fatalf("NewWriter error %s", err) } // const txtlen = 1024 const txtlen = 2100000 io.CopyN(buf, randtxt.NewReader(rand.NewSource(42)), txtlen) txt := buf.String() buf.Reset() n, err := io.Copy(w, strings.NewReader(txt)) if err != nil { t.Fatalf("Compressing copy error %s", err) } if n != txtlen { t.Fatalf("Compressing data length %d; want %d", n, txtlen) } if err = w.Close(); err != nil { t.Fatalf("w.Close error %s", err) } t.Logf("buf.Len() %d", buf.Len()) r, err := Reader2Config{DictCap: 4096}.NewReader2(buf) if err != nil { t.Fatalf("NewReader error %s", err) } out := new(bytes.Buffer) n, err = io.Copy(out, r) if err != nil { t.Fatalf("Decompressing copy error %s after %d bytes", err, n) } if n != txtlen { t.Fatalf("Decompression data length %d; want %d", n, txtlen) } if txt != out.String() { t.Fatal("decompressed data differs from original") } }