diff --git a/internal/compress/zstd_test.go b/internal/compress/zstd_test.go index cee1898177..7329e5cf7b 100644 --- a/internal/compress/zstd_test.go +++ b/internal/compress/zstd_test.go @@ -43,45 +43,6 @@ var ( } ) -func TestZstdCompressVector(t *testing.T) { - tests := []struct { - vector []float32 - }{ - { - vector: []float32{0.1, 0.2, 0.3}, - }, - { - vector: []float32{0.4, 0.2, 0.3, 0.1}, - }, - { - vector: []float32{0.1, 0.5, 0.12, 0.13, 1.0}, - }, - } - - for _, tc := range tests { - zstdc, err := NewZstd() - if err != nil { - t.Fatalf("initialize failed: %s", err) - } - - compressed, err := zstdc.CompressVector(tc.vector) - if err != nil { - t.Fatalf("Compress failed: %s", err) - } - - decompressed, err := zstdc.DecompressVector(compressed) - if err != nil { - t.Fatalf("Decompress failed: %s", err) - } - t.Logf("converted: origin %+v, compressed -> decompressed %+v", tc.vector, decompressed) - for i := range tc.vector { - if tc.vector[i] != decompressed[i] { - t.Fatalf("Invalid convert: origin %+v, compressed -> decompressed %+v", tc.vector, decompressed) - } - } - } -} - func TestNewZstd(t *testing.T) { type args struct { opts []ZstdOption @@ -258,21 +219,6 @@ func Test_zstdCompressor_CompressVector(t *testing.T) { return nil, errors.New("gobc err") }, }, - zstd: &zstd.MockZstd{ - NewWriterFunc: func(w io.Writer, opts ...zstd.EOption) (zstd.Encoder, error) { - return &zstd.MockEncoder{ - WriteFunc: func([]byte) (int, error) { - return 0, nil - }, - CloseFunc: func() error { - return nil - }, - ReadFromFunc: func(r io.Reader) (int64, error) { - return io.Copy(w, r) - }, - }, nil - }, - }, }, want: want{ err: errors.New("gobc err"), @@ -316,9 +262,6 @@ func Test_zstdCompressor_CompressVector(t *testing.T) { WriteFunc: func([]byte) (int, error) { return 0, nil }, - CloseFunc: func() error { - return nil - }, ReadFromFunc: func(r io.Reader) (int64, error) { return 0, errors.New("readFrom err") }, @@ -390,6 +333,74 @@ func Test_zstdCompressor_CompressVector(t *testing.T) { } } +func Test_E2E_zstdCompressor_CompressVector(t *testing.T) { + type args struct { + vector []float32 + } + type want struct { + want []float32 + err error + } + type test struct { + name string + args args + want want + checkFunc func(want, []byte, error, Compressor) error + beforeFunc func(args) + afterFunc func(args) + } + + defaultCheckFunc := func(w want, got []byte, err error, l Compressor) error { + if !errors.Is(err, w.err) { + return errors.Errorf("got error = %v, want %v", err, w.err) + } + decompressed, err := l.DecompressVector(got) + if err != nil { + return errors.Errorf("decompress error: %v", err) + } + if !reflect.DeepEqual(decompressed, w.want) { + return errors.Errorf("got = %v, want %v", decompressed, w.want) + } + return nil + } + tests := []test{ + { + name: "returns same vector after decompress the compressed data", + args: args{ + vector: []float32{0.1, 0.2, 0.3}, + }, + want: want{ + want: []float32{0.1, 0.2, 0.3}, + }, + }, + } + + for _, test := range tests { + t.Run(test.name, func(tt *testing.T) { + defer goleak.VerifyNone(tt) + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + g, err := NewZstd() + if err != nil { + t.Fatal(err) + } + + got, err := g.CompressVector(test.args.vector) + if err := test.checkFunc(test.want, got, err, g); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + func Test_zstdCompressor_DecompressVector(t *testing.T) { type args struct { bs []byte