1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
25 "github.com/apache/arrow/go/v6/parquet/compress"
26 "github.com/stretchr/testify/assert"
30 RandomDataSize = 3 * 1024 * 1024
31 CompressibleDataSize = 8 * 1024 * 1024
34 func makeRandomData(size int) []byte {
35 ret := make([]byte, size)
36 r := rand.New(rand.NewSource(1234))
41 func makeCompressibleData(size int) []byte {
42 const base = "Apache Arrow is a cross-language development platform for in-memory data"
44 data := make([]byte, size)
46 for i := n; i < len(data); i *= 2 {
47 copy(data[i:], data[:i])
52 func TestErrorForUnimplemented(t *testing.T) {
53 _, err := compress.GetCodec(compress.Codecs.Lzo)
56 _, err = compress.GetCodec(compress.Codecs.Lz4)
60 func TestCompressDataOneShot(t *testing.T) {
62 c compress.Compression
64 {compress.Codecs.Uncompressed},
65 {compress.Codecs.Snappy},
66 {compress.Codecs.Gzip},
67 {compress.Codecs.Brotli},
68 {compress.Codecs.Zstd},
69 // {compress.Codecs.Lzo},
70 // {compress.Codecs.Lz4},
73 for _, tt := range tests {
74 t.Run(tt.c.String(), func(t *testing.T) {
75 codec, err := compress.GetCodec(tt.c)
76 assert.NoError(t, err)
77 data := makeCompressibleData(CompressibleDataSize)
79 buf := make([]byte, codec.CompressBound(int64(len(data))))
80 compressed := codec.Encode(buf, data)
81 assert.Same(t, &buf[0], &compressed[0])
83 out := make([]byte, len(data))
84 uncompressed := codec.Decode(out, compressed)
85 assert.Same(t, &out[0], &uncompressed[0])
87 assert.Exactly(t, data, uncompressed)
92 func TestCompressReaderWriter(t *testing.T) {
94 c compress.Compression
96 {compress.Codecs.Uncompressed},
97 {compress.Codecs.Snappy},
98 {compress.Codecs.Gzip},
99 {compress.Codecs.Brotli},
100 {compress.Codecs.Zstd},
101 // {compress.Codecs.Lzo},
102 // {compress.Codecs.Lz4},
105 for _, tt := range tests {
106 t.Run(tt.c.String(), func(t *testing.T) {
108 codec, err := compress.GetCodec(tt.c)
109 assert.NoError(t, err)
110 data := makeRandomData(RandomDataSize)
112 wr := codec.NewWriter(&buf)
114 const chunkSize = 1111
121 if len(input) > chunkSize {
122 n, err = wr.Write(input[:chunkSize])
124 n, err = wr.Write(input)
127 assert.NoError(t, err)
132 rdr := codec.NewReader(&buf)
133 out, err := ioutil.ReadAll(rdr)
134 assert.NoError(t, err)
135 assert.Exactly(t, data, out)