(name string, t *testing.T)
| 38 | } |
| 39 | |
| 40 | func testCompress(name string, t *testing.T) { |
| 41 | c := encoding.GetCompressor(name) |
| 42 | assert.Equal(t, name, c.Name()) |
| 43 | |
| 44 | tests := []struct { |
| 45 | test string |
| 46 | input string |
| 47 | }{ |
| 48 | {"empty", ""}, |
| 49 | {"short", "hello world"}, |
| 50 | {"long", strings.Repeat("123456789", 2024)}, |
| 51 | } |
| 52 | for _, test := range tests { |
| 53 | t.Run(test.test, func(t *testing.T) { |
| 54 | buf := &bytes.Buffer{} |
| 55 | // Compress |
| 56 | w, err := c.Compress(buf) |
| 57 | require.NoError(t, err) |
| 58 | n, err := w.Write([]byte(test.input)) |
| 59 | require.NoError(t, err) |
| 60 | assert.Len(t, test.input, n) |
| 61 | err = w.Close() |
| 62 | require.NoError(t, err) |
| 63 | compressedBytes := buf.Bytes() |
| 64 | buf = bytes.NewBuffer(compressedBytes) |
| 65 | |
| 66 | // Decompress |
| 67 | r, err := c.Decompress(buf) |
| 68 | require.NoError(t, err) |
| 69 | out, err := io.ReadAll(r) |
| 70 | require.NoError(t, err) |
| 71 | assert.Equal(t, test.input, string(out)) |
| 72 | |
| 73 | if sizer, ok := c.(interface { |
| 74 | DecompressedSize(compressedBytes []byte) int |
| 75 | }); ok { |
| 76 | buf = bytes.NewBuffer(compressedBytes) |
| 77 | r, err := c.Decompress(buf) |
| 78 | require.NoError(t, err) |
| 79 | out, err := io.ReadAll(r) |
| 80 | require.NoError(t, err) |
| 81 | assert.Equal(t, len(out), sizer.DecompressedSize(compressedBytes)) |
| 82 | } |
| 83 | }) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | func BenchmarkCompress(b *testing.B) { |
| 88 | data := []byte(strings.Repeat("123456789", 1024)) |
no test coverage detected