reader_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package lz4_test
  2. import (
  3. "bytes"
  4. "io"
  5. "io/ioutil"
  6. "os"
  7. "reflect"
  8. "strings"
  9. "testing"
  10. "github.com/pierrec/lz4"
  11. )
  12. func TestReader(t *testing.T) {
  13. goldenFiles := []string{
  14. "testdata/e.txt.lz4",
  15. "testdata/gettysburg.txt.lz4",
  16. "testdata/Mark.Twain-Tom.Sawyer.txt.lz4",
  17. "testdata/Mark.Twain-Tom.Sawyer_long.txt.lz4",
  18. "testdata/pg1661.txt.lz4",
  19. "testdata/pi.txt.lz4",
  20. "testdata/random.data.lz4",
  21. "testdata/repeat.txt.lz4",
  22. "testdata/pg_control.tar.lz4",
  23. }
  24. for _, fname := range goldenFiles {
  25. t.Run(fname, func(t *testing.T) {
  26. fname := fname
  27. t.Parallel()
  28. f, err := os.Open(fname)
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. defer f.Close()
  33. rawfile := strings.TrimSuffix(fname, ".lz4")
  34. raw, err := ioutil.ReadFile(rawfile)
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. var out bytes.Buffer
  39. zr := lz4.NewReader(f)
  40. n, err := io.Copy(&out, zr)
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. if got, want := int(n), len(raw); got != want {
  45. t.Errorf("invalid sizes: got %d; want %d", got, want)
  46. }
  47. if got, want := out.Bytes(), raw; !reflect.DeepEqual(got, want) {
  48. t.Fatal("uncompressed data does not match original")
  49. }
  50. if len(raw) < 20 {
  51. return
  52. }
  53. f2, err := os.Open(fname)
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. defer f2.Close()
  58. out.Reset()
  59. zr = lz4.NewReader(f2)
  60. _, err = io.CopyN(&out, zr, 10)
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. if !reflect.DeepEqual(out.Bytes(), raw[:10]) {
  65. t.Fatal("partial read does not match original")
  66. }
  67. pos, err := zr.Seek(-1, io.SeekCurrent)
  68. if err == nil {
  69. t.Fatal("expected error from invalid seek")
  70. }
  71. if pos != 10 {
  72. t.Fatalf("unexpected position %d", pos)
  73. }
  74. pos, err = zr.Seek(1, io.SeekStart)
  75. if err == nil {
  76. t.Fatal("expected error from invalid seek")
  77. }
  78. if pos != 10 {
  79. t.Fatalf("unexpected position %d", pos)
  80. }
  81. pos, err = zr.Seek(-1, io.SeekEnd)
  82. if err == nil {
  83. t.Fatal("expected error from invalid seek")
  84. }
  85. if pos != 10 {
  86. t.Fatalf("unexpected position %d", pos)
  87. }
  88. pos, err = zr.Seek(int64(len(raw)-20), io.SeekCurrent)
  89. if err != nil {
  90. t.Fatal(err)
  91. }
  92. if pos != int64(len(raw)-10) {
  93. t.Fatalf("unexpected position %d", pos)
  94. }
  95. out.Reset()
  96. _, err = io.CopyN(&out, zr, 10)
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. if !reflect.DeepEqual(out.Bytes(), raw[len(raw)-10:]) {
  101. t.Fatal("after seek, partial read does not match original")
  102. }
  103. })
  104. }
  105. }