123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 |
- package zstd
- import (
- "bytes"
- "io"
- "os"
- "testing"
- snappy "github.com/klauspost/compress/internal/snapref"
- )
- func TestSnappy_ConvertSimple(t *testing.T) {
- in, err := os.ReadFile("testdata/z000028")
- if err != nil {
- t.Fatal(err)
- }
- var comp bytes.Buffer
- w := snappy.NewBufferedWriter(&comp)
- _, err = io.Copy(w, bytes.NewBuffer(in))
- if err != nil {
- t.Fatal(err)
- }
- err = w.Close()
- if err != nil {
- t.Fatal(err)
- }
- snapLen := comp.Len()
- s := SnappyConverter{}
- var dst bytes.Buffer
- n, err := s.Convert(&comp, &dst)
- if err != io.EOF {
- t.Fatal(err)
- }
- if n != int64(dst.Len()) {
- t.Errorf("Dest was %d bytes, but said to have written %d bytes", dst.Len(), n)
- }
- t.Log("SnappyConverter len", snapLen, "-> zstd len", dst.Len())
- dec, err := NewReader(nil)
- if err != nil {
- t.Fatal(err)
- }
- defer dec.Close()
- decoded, err := dec.DecodeAll(dst.Bytes(), nil)
- if err != nil {
- t.Error(err, len(decoded))
- }
- if !bytes.Equal(decoded, in) {
- os.WriteFile("testdata/z000028.got", decoded, os.ModePerm)
- t.Fatal("Decoded does not match")
- }
- t.Log("Encoded content matched")
- }
- func TestSnappy_ConvertXML(t *testing.T) {
- f, err := os.Open("testdata/xml.zst")
- if err != nil {
- t.Fatal(err)
- }
- defer f.Close()
- dec, err := NewReader(f)
- if err != nil {
- t.Fatal(err)
- }
- defer dec.Close()
- in, err := io.ReadAll(dec)
- if err != nil {
- t.Fatal(err)
- }
- var comp bytes.Buffer
- w := snappy.NewBufferedWriter(&comp)
- _, err = io.Copy(w, bytes.NewBuffer(in))
- if err != nil {
- t.Fatal(err)
- }
- err = w.Close()
- if err != nil {
- t.Fatal(err)
- }
- snapLen := comp.Len()
- s := SnappyConverter{}
- var dst bytes.Buffer
- n, err := s.Convert(&comp, &dst)
- if err != io.EOF {
- t.Fatal(err)
- }
- if n != int64(dst.Len()) {
- t.Errorf("Dest was %d bytes, but said to have written %d bytes", dst.Len(), n)
- }
- t.Log("Snappy len", snapLen, "-> zstd len", dst.Len())
- decoded, err := dec.DecodeAll(dst.Bytes(), nil)
- if err != nil {
- t.Error(err, len(decoded))
- }
- if !bytes.Equal(decoded, in) {
- os.WriteFile("testdata/xml.got", decoded, os.ModePerm)
- t.Fatal("Decoded does not match")
- }
- t.Log("Encoded content matched")
- }
- func TestSnappy_ConvertSilesia(t *testing.T) {
- if testing.Short() {
- t.SkipNow()
- }
- in, err := os.ReadFile("testdata/silesia.tar")
- if err != nil {
- if os.IsNotExist(err) {
- t.Skip("Missing testdata/silesia.tar")
- return
- }
- t.Fatal(err)
- }
- var comp bytes.Buffer
- w := snappy.NewBufferedWriter(&comp)
- _, err = io.Copy(w, bytes.NewBuffer(in))
- if err != nil {
- t.Fatal(err)
- }
- err = w.Close()
- if err != nil {
- t.Fatal(err)
- }
- snapLen := comp.Len()
- s := SnappyConverter{}
- var dst bytes.Buffer
- n, err := s.Convert(&comp, &dst)
- if err != io.EOF {
- t.Fatal(err)
- }
- if n != int64(dst.Len()) {
- t.Errorf("Dest was %d bytes, but said to have written %d bytes", dst.Len(), n)
- }
- t.Log("SnappyConverter len", snapLen, "-> zstd len", dst.Len())
- dec, err := NewReader(nil)
- if err != nil {
- t.Fatal(err)
- }
- defer dec.Close()
- decoded, err := dec.DecodeAll(dst.Bytes(), nil)
- if err != nil {
- t.Error(err, len(decoded))
- }
- if !bytes.Equal(decoded, in) {
- os.WriteFile("testdata/silesia.tar.got", decoded, os.ModePerm)
- t.Fatal("Decoded does not match")
- }
- t.Log("Encoded content matched")
- }
- func TestSnappy_ConvertEnwik9(t *testing.T) {
- if testing.Short() {
- t.SkipNow()
- }
- file := "testdata/enwik9.zst"
- f, err := os.Open(file)
- if err != nil {
- if os.IsNotExist(err) {
- t.Skip("To run extended tests, download http://mattmahoney.net/dc/enwik9.zip unzip it \n" +
- "compress it with 'zstd -15 -T0 enwik9' and place it in " + file)
- }
- }
- defer f.Close()
- dec, err := NewReader(f)
- if err != nil {
- t.Fatal(err)
- }
- defer dec.Close()
- in, err := io.ReadAll(dec)
- if err != nil {
- t.Fatal(err)
- }
- var comp bytes.Buffer
- w := snappy.NewBufferedWriter(&comp)
- _, err = io.Copy(w, bytes.NewBuffer(in))
- if err != nil {
- t.Fatal(err)
- }
- err = w.Close()
- if err != nil {
- t.Fatal(err)
- }
- snapLen := comp.Len()
- s := SnappyConverter{}
- var dst bytes.Buffer
- n, err := s.Convert(&comp, &dst)
- if err != io.EOF {
- t.Fatal(err)
- }
- if n != int64(dst.Len()) {
- t.Errorf("Dest was %d bytes, but said to have written %d bytes", dst.Len(), n)
- }
- t.Log("SnappyConverter len", snapLen, "-> zstd len", dst.Len())
- decoded, err := dec.DecodeAll(dst.Bytes(), nil)
- if err != nil {
- t.Error(err, len(decoded))
- }
- if !bytes.Equal(decoded, in) {
- os.WriteFile("testdata/enwik9.got", decoded, os.ModePerm)
- t.Fatal("Decoded does not match")
- }
- t.Log("Encoded content matched")
- }
- func BenchmarkSnappy_ConvertXML(b *testing.B) {
- f, err := os.Open("testdata/xml.zst")
- if err != nil {
- b.Fatal(err)
- }
- defer f.Close()
- dec, err := NewReader(f)
- if err != nil {
- b.Fatal(err)
- }
- defer dec.Close()
- in, err := io.ReadAll(dec)
- if err != nil {
- b.Fatal(err)
- }
- var comp bytes.Buffer
- w := snappy.NewBufferedWriter(&comp)
- _, err = io.Copy(w, bytes.NewBuffer(in))
- if err != nil {
- b.Fatal(err)
- }
- err = w.Close()
- if err != nil {
- b.Fatal(err)
- }
- s := SnappyConverter{}
- compBytes := comp.Bytes()
- _, err = s.Convert(&comp, io.Discard)
- if err != io.EOF {
- b.Fatal(err)
- }
- b.ResetTimer()
- b.ReportAllocs()
- b.SetBytes(int64(len(in)))
- for i := 0; i < b.N; i++ {
- _, err := s.Convert(bytes.NewBuffer(compBytes), io.Discard)
- if err != io.EOF {
- b.Fatal(err)
- }
- }
- }
- func BenchmarkSnappy_Enwik9(b *testing.B) {
- f, err := os.Open("testdata/enwik9.zst")
- if err != nil {
- b.Skip(err)
- return
- }
- defer f.Close()
- dec, err := NewReader(f)
- if err != nil {
- b.Fatal(err)
- }
- defer dec.Close()
- in, err := io.ReadAll(dec)
- if err != nil {
- b.Fatal(err)
- }
- defer dec.Close()
- var comp bytes.Buffer
- w := snappy.NewBufferedWriter(&comp)
- _, err = io.Copy(w, bytes.NewBuffer(in))
- if err != nil {
- b.Fatal(err)
- }
- err = w.Close()
- if err != nil {
- b.Fatal(err)
- }
- s := SnappyConverter{}
- compBytes := comp.Bytes()
- _, err = s.Convert(&comp, io.Discard)
- if err != io.EOF {
- b.Fatal(err)
- }
- b.ResetTimer()
- b.ReportAllocs()
- b.SetBytes(int64(len(in)))
- for i := 0; i < b.N; i++ {
- _, err := s.Convert(bytes.NewBuffer(compBytes), io.Discard)
- if err != io.EOF {
- b.Fatal(err)
- }
- }
- }
- func BenchmarkSnappy_ConvertSilesia(b *testing.B) {
- in, err := os.ReadFile("testdata/silesia.tar")
- if err != nil {
- if os.IsNotExist(err) {
- b.Skip("Missing testdata/silesia.tar")
- return
- }
- b.Fatal(err)
- }
- var comp bytes.Buffer
- w := snappy.NewBufferedWriter(&comp)
- _, err = io.Copy(w, bytes.NewBuffer(in))
- if err != nil {
- b.Fatal(err)
- }
- err = w.Close()
- if err != nil {
- b.Fatal(err)
- }
- s := SnappyConverter{}
- compBytes := comp.Bytes()
- _, err = s.Convert(&comp, io.Discard)
- if err != io.EOF {
- b.Fatal(err)
- }
- b.ResetTimer()
- b.ReportAllocs()
- b.SetBytes(int64(len(in)))
- for i := 0; i < b.N; i++ {
- _, err := s.Convert(bytes.NewBuffer(compBytes), io.Discard)
- if err != io.EOF {
- b.Fatal(err)
- }
- }
- }
|