github.com/Schaudge/grailbase@v0.0.0-20240223061707-44c758a471c0/recordio/recordioutil/compress_test.go (about)

     1  // Copyright 2017 GRAIL, Inc. All rights reserved.
     2  // Use of this source code is governed by the Apache-2.0
     3  // license that can be found in the LICENSE file.
     4  
     5  package recordioutil_test
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/Schaudge/grailbase/recordio/recordioutil"
    14  	"github.com/grailbio/testutil/assert"
    15  	"github.com/grailbio/testutil/expect"
    16  	"github.com/klauspost/compress/flate"
    17  )
    18  
    19  func transformTestData(n int) [][]byte {
    20  	r := [][]byte{}
    21  	for i := 0; i < n; i++ {
    22  		r = append(r, []byte(strings.Repeat(fmt.Sprintf("%v", i), i)))
    23  	}
    24  	return r
    25  }
    26  
    27  func TestCompressInverse(t *testing.T) {
    28  	data := transformTestData(10)
    29  
    30  	ft := recordioutil.NewFlateTransform(flate.BestCompression)
    31  
    32  	record, err := ft.CompressTransform(data)
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  
    37  	read, err := ft.DecompressTransform(record)
    38  	assert.NoError(t, err)
    39  	assert.EQ(t, read, bytes.Join(data, nil))
    40  }
    41  
    42  func TestCompressErrors(t *testing.T) {
    43  	data := transformTestData(1)
    44  	ft := recordioutil.NewFlateTransform(33)
    45  	_, err := ft.CompressTransform(data)
    46  	expect.HasSubstr(t, err, "invalid compression level")
    47  }