storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/s3select/internal/parquet-go/encoding/plain-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 encoding 18 19 import ( 20 "math" 21 "reflect" 22 "testing" 23 ) 24 25 func TestPlainEncodeBools(t *testing.T) { 26 testCases := []struct { 27 bs []bool 28 expectedResult []byte 29 }{ 30 {nil, []byte{}}, 31 {[]bool{}, []byte{}}, 32 {[]bool{true}, []byte{1}}, 33 {[]bool{false}, []byte{0}}, 34 {[]bool{true, true}, []byte{3}}, 35 {[]bool{false, false}, []byte{0}}, 36 {[]bool{false, true}, []byte{2}}, 37 {[]bool{true, false}, []byte{1}}, 38 {[]bool{false, false, false, false, false, false, false, true, true}, []byte{128, 1}}, 39 } 40 41 for i, testCase := range testCases { 42 result := plainEncodeBools(testCase.bs) 43 if !reflect.DeepEqual(result, testCase.expectedResult) { 44 t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result) 45 } 46 } 47 } 48 49 func TestPlainEncodeInt32s(t *testing.T) { 50 testCases := []struct { 51 i32s []int32 52 expectedResult []byte 53 }{ 54 {nil, []byte{}}, 55 {[]int32{}, []byte{}}, 56 {[]int32{1}, []byte{1, 0, 0, 0}}, 57 {[]int32{-1}, []byte{255, 255, 255, 255}}, 58 {[]int32{256}, []byte{0, 1, 0, 0}}, 59 {[]int32{math.MinInt32}, []byte{0, 0, 0, 128}}, 60 {[]int32{math.MaxInt32}, []byte{255, 255, 255, 127}}, 61 {[]int32{257, -2}, []byte{1, 1, 0, 0, 254, 255, 255, 255}}, 62 } 63 64 for i, testCase := range testCases { 65 result := plainEncodeInt32s(testCase.i32s) 66 if !reflect.DeepEqual(result, testCase.expectedResult) { 67 t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result) 68 } 69 } 70 } 71 72 func TestPlainEncodeInt64s(t *testing.T) { 73 testCases := []struct { 74 i64s []int64 75 expectedResult []byte 76 }{ 77 {nil, []byte{}}, 78 {[]int64{}, []byte{}}, 79 {[]int64{1}, []byte{1, 0, 0, 0, 0, 0, 0, 0}}, 80 {[]int64{-1}, []byte{255, 255, 255, 255, 255, 255, 255, 255}}, 81 {[]int64{256}, []byte{0, 1, 0, 0, 0, 0, 0, 0}}, 82 {[]int64{math.MinInt64}, []byte{0, 0, 0, 0, 0, 0, 0, 128}}, 83 {[]int64{math.MaxInt64}, []byte{255, 255, 255, 255, 255, 255, 255, 127}}, 84 {[]int64{257, -2}, []byte{1, 1, 0, 0, 0, 0, 0, 0, 254, 255, 255, 255, 255, 255, 255, 255}}, 85 } 86 87 for i, testCase := range testCases { 88 result := plainEncodeInt64s(testCase.i64s) 89 if !reflect.DeepEqual(result, testCase.expectedResult) { 90 t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result) 91 } 92 } 93 } 94 95 func TestPlainEncodeFloat32s(t *testing.T) { 96 testCases := []struct { 97 f32s []float32 98 expectedResult []byte 99 }{ 100 {nil, []byte{}}, 101 {[]float32{}, []byte{}}, 102 {[]float32{1}, []byte{0, 0, 128, 63}}, 103 {[]float32{1.0}, []byte{0, 0, 128, 63}}, 104 {[]float32{-1}, []byte{0, 0, 128, 191}}, 105 {[]float32{-1.0}, []byte{0, 0, 128, 191}}, 106 {[]float32{256}, []byte{0, 0, 128, 67}}, 107 {[]float32{1.1}, []byte{205, 204, 140, 63}}, 108 {[]float32{-1.1}, []byte{205, 204, 140, 191}}, 109 {[]float32{math.Pi}, []byte{219, 15, 73, 64}}, 110 {[]float32{257, -2}, []byte{0, 128, 128, 67, 0, 0, 0, 192}}, 111 {[]float32{257.1, -2.1}, []byte{205, 140, 128, 67, 102, 102, 6, 192}}, 112 } 113 114 for i, testCase := range testCases { 115 result := plainEncodeFloat32s(testCase.f32s) 116 if !reflect.DeepEqual(result, testCase.expectedResult) { 117 t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result) 118 } 119 } 120 } 121 122 func TestPlainEncodeFloat64s(t *testing.T) { 123 testCases := []struct { 124 f64s []float64 125 expectedResult []byte 126 }{ 127 {nil, []byte{}}, 128 {[]float64{}, []byte{}}, 129 {[]float64{1}, []byte{0, 0, 0, 0, 0, 0, 240, 63}}, 130 {[]float64{1.0}, []byte{0, 0, 0, 0, 0, 0, 240, 63}}, 131 {[]float64{-1}, []byte{0, 0, 0, 0, 0, 0, 240, 191}}, 132 {[]float64{-1.0}, []byte{0, 0, 0, 0, 0, 0, 240, 191}}, 133 {[]float64{256}, []byte{0, 0, 0, 0, 0, 0, 112, 64}}, 134 {[]float64{1.1}, []byte{154, 153, 153, 153, 153, 153, 241, 63}}, 135 {[]float64{-1.1}, []byte{154, 153, 153, 153, 153, 153, 241, 191}}, 136 {[]float64{math.Pi}, []byte{24, 45, 68, 84, 251, 33, 9, 64}}, 137 {[]float64{257, -2}, []byte{0, 0, 0, 0, 0, 16, 112, 64, 0, 0, 0, 0, 0, 0, 0, 192}}, 138 {[]float64{257.1, -2.1}, []byte{154, 153, 153, 153, 153, 17, 112, 64, 205, 204, 204, 204, 204, 204, 0, 192}}, 139 } 140 141 for i, testCase := range testCases { 142 result := plainEncodeFloat64s(testCase.f64s) 143 if !reflect.DeepEqual(result, testCase.expectedResult) { 144 t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result) 145 } 146 } 147 }