storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/s3select/internal/parquet-go/encode_test.go (about)

     1  /*
     2   * Minio Cloud Storage, (C) 2019 Minio, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package parquet
    18  
    19  import (
    20  	"math"
    21  	"reflect"
    22  	"testing"
    23  
    24  	"storj.io/minio/pkg/s3select/internal/parquet-go/gen-go/parquet"
    25  )
    26  
    27  func TestBoolsToBytes(t *testing.T) {
    28  	testCases := []struct {
    29  		bs             []bool
    30  		expectedResult []byte
    31  	}{
    32  		{nil, []byte{}},
    33  		{[]bool{}, []byte{}},
    34  		{[]bool{true}, []byte{1}},
    35  		{[]bool{false}, []byte{0}},
    36  		{[]bool{true, true}, []byte{3}},
    37  		{[]bool{false, false}, []byte{0}},
    38  		{[]bool{false, true}, []byte{2}},
    39  		{[]bool{true, false}, []byte{1}},
    40  		{[]bool{false, false, false, false, false, false, false, true, true}, []byte{128, 1}},
    41  	}
    42  
    43  	for i, testCase := range testCases {
    44  		result := boolsToBytes(testCase.bs)
    45  		if !reflect.DeepEqual(result, testCase.expectedResult) {
    46  			t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result)
    47  		}
    48  	}
    49  }
    50  
    51  func TestInt32sToBytes(t *testing.T) {
    52  	testCases := []struct {
    53  		i32s           []int32
    54  		expectedResult []byte
    55  	}{
    56  		{nil, []byte{}},
    57  		{[]int32{}, []byte{}},
    58  		{[]int32{1}, []byte{1, 0, 0, 0}},
    59  		{[]int32{-1}, []byte{255, 255, 255, 255}},
    60  		{[]int32{256}, []byte{0, 1, 0, 0}},
    61  		{[]int32{math.MinInt32}, []byte{0, 0, 0, 128}},
    62  		{[]int32{math.MaxInt32}, []byte{255, 255, 255, 127}},
    63  		{[]int32{257, -2}, []byte{1, 1, 0, 0, 254, 255, 255, 255}},
    64  	}
    65  
    66  	for i, testCase := range testCases {
    67  		result := int32sToBytes(testCase.i32s)
    68  		if !reflect.DeepEqual(result, testCase.expectedResult) {
    69  			t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result)
    70  		}
    71  	}
    72  }
    73  
    74  func TestInt64sToBytes(t *testing.T) {
    75  	testCases := []struct {
    76  		i64s           []int64
    77  		expectedResult []byte
    78  	}{
    79  		{nil, []byte{}},
    80  		{[]int64{}, []byte{}},
    81  		{[]int64{1}, []byte{1, 0, 0, 0, 0, 0, 0, 0}},
    82  		{[]int64{-1}, []byte{255, 255, 255, 255, 255, 255, 255, 255}},
    83  		{[]int64{256}, []byte{0, 1, 0, 0, 0, 0, 0, 0}},
    84  		{[]int64{math.MinInt64}, []byte{0, 0, 0, 0, 0, 0, 0, 128}},
    85  		{[]int64{math.MaxInt64}, []byte{255, 255, 255, 255, 255, 255, 255, 127}},
    86  		{[]int64{257, -2}, []byte{1, 1, 0, 0, 0, 0, 0, 0, 254, 255, 255, 255, 255, 255, 255, 255}},
    87  	}
    88  
    89  	for i, testCase := range testCases {
    90  		result := int64sToBytes(testCase.i64s)
    91  		if !reflect.DeepEqual(result, testCase.expectedResult) {
    92  			t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result)
    93  		}
    94  	}
    95  }
    96  
    97  func TestFloat32sToBytes(t *testing.T) {
    98  	testCases := []struct {
    99  		f32s           []float32
   100  		expectedResult []byte
   101  	}{
   102  		{nil, []byte{}},
   103  		{[]float32{}, []byte{}},
   104  		{[]float32{1}, []byte{0, 0, 128, 63}},
   105  		{[]float32{1.0}, []byte{0, 0, 128, 63}},
   106  		{[]float32{-1}, []byte{0, 0, 128, 191}},
   107  		{[]float32{-1.0}, []byte{0, 0, 128, 191}},
   108  		{[]float32{256}, []byte{0, 0, 128, 67}},
   109  		{[]float32{1.1}, []byte{205, 204, 140, 63}},
   110  		{[]float32{-1.1}, []byte{205, 204, 140, 191}},
   111  		{[]float32{math.Pi}, []byte{219, 15, 73, 64}},
   112  		{[]float32{257, -2}, []byte{0, 128, 128, 67, 0, 0, 0, 192}},
   113  		{[]float32{257.1, -2.1}, []byte{205, 140, 128, 67, 102, 102, 6, 192}},
   114  	}
   115  
   116  	for i, testCase := range testCases {
   117  		result := float32sToBytes(testCase.f32s)
   118  		if !reflect.DeepEqual(result, testCase.expectedResult) {
   119  			t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result)
   120  		}
   121  	}
   122  }
   123  
   124  func TestFloat64sToBytes(t *testing.T) {
   125  	testCases := []struct {
   126  		f64s           []float64
   127  		expectedResult []byte
   128  	}{
   129  		{nil, []byte{}},
   130  		{[]float64{}, []byte{}},
   131  		{[]float64{1}, []byte{0, 0, 0, 0, 0, 0, 240, 63}},
   132  		{[]float64{1.0}, []byte{0, 0, 0, 0, 0, 0, 240, 63}},
   133  		{[]float64{-1}, []byte{0, 0, 0, 0, 0, 0, 240, 191}},
   134  		{[]float64{-1.0}, []byte{0, 0, 0, 0, 0, 0, 240, 191}},
   135  		{[]float64{256}, []byte{0, 0, 0, 0, 0, 0, 112, 64}},
   136  		{[]float64{1.1}, []byte{154, 153, 153, 153, 153, 153, 241, 63}},
   137  		{[]float64{-1.1}, []byte{154, 153, 153, 153, 153, 153, 241, 191}},
   138  		{[]float64{math.Pi}, []byte{24, 45, 68, 84, 251, 33, 9, 64}},
   139  		{[]float64{257, -2}, []byte{0, 0, 0, 0, 0, 16, 112, 64, 0, 0, 0, 0, 0, 0, 0, 192}},
   140  		{[]float64{257.1, -2.1}, []byte{154, 153, 153, 153, 153, 17, 112, 64, 205, 204, 204, 204, 204, 204, 0, 192}},
   141  	}
   142  
   143  	for i, testCase := range testCases {
   144  		result := float64sToBytes(testCase.f64s)
   145  		if !reflect.DeepEqual(result, testCase.expectedResult) {
   146  			t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result)
   147  		}
   148  	}
   149  }
   150  
   151  func TestUnsignedVarIntToBytes(t *testing.T) {
   152  	testCases := []struct {
   153  		ui64           uint64
   154  		expectedResult []byte
   155  	}{
   156  		{0, []byte{0}},
   157  		{1, []byte{1}},
   158  		{0x7F, []byte{127}},
   159  		{0x80, []byte{128, 1}},
   160  		{uint64(math.MaxUint64), []byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 1}},
   161  	}
   162  
   163  	for i, testCase := range testCases {
   164  		result := unsignedVarIntToBytes(testCase.ui64)
   165  		if !reflect.DeepEqual(result, testCase.expectedResult) {
   166  			t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result)
   167  		}
   168  	}
   169  }
   170  
   171  func TestValuesToRLEBytes(t *testing.T) {
   172  	testCases := []struct {
   173  		values         interface{}
   174  		bitWidth       int32
   175  		dataType       parquet.Type
   176  		expectedResult []byte
   177  	}{
   178  		{[]int32{3, 5, 7}, 1, parquet.Type_INT32, []byte{2, 3, 2, 5, 2, 7}},
   179  		{[]int32{3, 3, 3}, 1, parquet.Type_INT32, []byte{6, 3}},
   180  		{[]int32{2, 2, 3, 3, 3}, 1, parquet.Type_INT32, []byte{4, 2, 6, 3}},
   181  	}
   182  
   183  	for i, testCase := range testCases {
   184  		result := valuesToRLEBytes(testCase.values, testCase.bitWidth, testCase.dataType)
   185  		if !reflect.DeepEqual(result, testCase.expectedResult) {
   186  			t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result)
   187  		}
   188  	}
   189  }