lz4stream_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package protocol
  2. import (
  3. "bytes"
  4. "crypto/rand"
  5. "io"
  6. "testing"
  7. )
  8. var toWrite = [][]byte{
  9. []byte("this is a short byte string that should pass through somewhat compressed this is a short byte string that should pass through somewhat compressed this is a short byte string that should pass through somewhat compressed this is a short byte string that should pass through somewhat compressed this is a short byte string that should pass through somewhat compressed this is a short byte string that should pass through somewhat compressed"),
  10. []byte("this is another short byte string that should pass through uncompressed"),
  11. []byte{0, 1, 2, 3, 4, 5},
  12. }
  13. func TestLZ4Stream(t *testing.T) {
  14. tb := make([]byte, 128*1024)
  15. rand.Reader.Read(tb)
  16. toWrite = append(toWrite, tb)
  17. tb = make([]byte, 512*1024)
  18. rand.Reader.Read(tb)
  19. toWrite = append(toWrite, tb)
  20. toWrite = append(toWrite, toWrite[0])
  21. toWrite = append(toWrite, toWrite[1])
  22. rd, wr := io.Pipe()
  23. lz4r := newLZ4Reader(rd)
  24. lz4w := newLZ4Writer(wr)
  25. go func() {
  26. for i := 0; i < 5; i++ {
  27. for _, bs := range toWrite {
  28. n, err := lz4w.Write(bs)
  29. if err != nil {
  30. t.Error(err)
  31. }
  32. if n != len(bs) {
  33. t.Errorf("weird write length; %d != %d", n, len(bs))
  34. }
  35. }
  36. }
  37. }()
  38. buf := make([]byte, 512*1024)
  39. for i := 0; i < 5; i++ {
  40. for _, bs := range toWrite {
  41. n, err := lz4r.Read(buf)
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. if n != len(bs) {
  46. t.Errorf("Unexpected len %d != %d", n, len(bs))
  47. }
  48. if bytes.Compare(bs, buf[:n]) != 0 {
  49. t.Error("Unexpected data")
  50. }
  51. }
  52. }
  53. }