github.com/segmentio/parquet-go@v0.0.0-20230712180008-5d42db8f0d47/value_test.go (about) 1 package parquet_test 2 3 import ( 4 "bytes" 5 "math" 6 "testing" 7 "time" 8 "unsafe" 9 10 "github.com/segmentio/parquet-go" 11 "github.com/segmentio/parquet-go/deprecated" 12 ) 13 14 func TestSizeOfValue(t *testing.T) { 15 t.Logf("sizeof(parquet.Value) = %d", unsafe.Sizeof(parquet.Value{})) 16 } 17 18 func BenchmarkValueAppend(b *testing.B) { 19 const N = 1024 20 row := make(parquet.Row, 0, N) 21 val := parquet.ValueOf(42) 22 23 for i := 0; i < b.N; i++ { 24 row = row[:0] 25 for j := 0; j < N; j++ { 26 row = append(row, val) 27 } 28 } 29 30 b.SetBytes(N * int64(unsafe.Sizeof(parquet.Value{}))) 31 } 32 33 func TestValueClone(t *testing.T) { 34 tests := []struct { 35 scenario string 36 values []interface{} 37 }{ 38 { 39 scenario: "BOOLEAN", 40 values: []interface{}{false, true}, 41 }, 42 43 { 44 scenario: "INT32", 45 values: []interface{}{int32(0), int32(1), int32(math.MinInt32), int32(math.MaxInt32)}, 46 }, 47 48 { 49 scenario: "INT64", 50 values: []interface{}{int64(0), int64(1), int64(math.MinInt64), int64(math.MaxInt64)}, 51 }, 52 53 { 54 scenario: "FLOAT", 55 values: []interface{}{float32(0), float32(1), float32(-1)}, 56 }, 57 58 { 59 scenario: "DOUBLE", 60 values: []interface{}{float64(0), float64(1), float64(-1)}, 61 }, 62 63 { 64 scenario: "BYTE_ARRAY", 65 values: []interface{}{"", "A", "ABC", "Hello World!"}, 66 }, 67 68 { 69 scenario: "FIXED_LEN_BYTE_ARRAY", 70 values: []interface{}{[1]byte{42}, [16]byte{0: 1}}, 71 }, 72 73 { 74 scenario: "TIME", 75 values: []interface{}{ 76 time.Date(2020, 1, 2, 3, 4, 5, 7, time.UTC), 77 time.Date(2021, 2, 3, 4, 5, 6, 8, time.UTC), 78 }, 79 }, 80 } 81 82 for _, test := range tests { 83 t.Run(test.scenario, func(t *testing.T) { 84 for _, value := range test.values { 85 v := parquet.ValueOf(value) 86 c := v.Clone() 87 88 if !parquet.DeepEqual(v, c) { 89 t.Errorf("cloned values are not equal: want=%#v got=%#v", v, c) 90 } 91 if v.RepetitionLevel() != c.RepetitionLevel() { 92 t.Error("cloned values do not have the same repetition level") 93 } 94 if v.DefinitionLevel() != c.DefinitionLevel() { 95 t.Error("cloned values do not have the same definition level") 96 } 97 if v.Column() != c.Column() { 98 t.Error("cloned values do not have the same column index") 99 } 100 } 101 }) 102 } 103 } 104 105 func TestZeroValue(t *testing.T) { 106 var v parquet.Value 107 if !v.IsNull() { 108 t.Error("expected zero value parquet.Value to be null") 109 } 110 111 if v.Byte() != byte(0) { 112 t.Errorf("byte not zero value: got=%#v", v.Byte()) 113 } 114 115 if v.Boolean() != false { 116 t.Errorf("boolean not zero value: got=%#v", v.Boolean()) 117 } 118 119 if v.Int32() != 0 { 120 t.Errorf("int32 not zero value: got=%#v", v.Int32()) 121 } 122 123 if v.Int64() != 0 { 124 t.Errorf("int64 not zero value: got=%#v", v.Int64()) 125 } 126 127 var zeroInt96 deprecated.Int96 128 if v.Int96() != zeroInt96 { 129 t.Errorf("int96 not zero value: got=%#v", zeroInt96) 130 } 131 132 if v.Float() != 0 { 133 t.Errorf("float not zero value: got=%#v", v.Float()) 134 } 135 136 if v.Double() != 0 { 137 t.Errorf("double not zero value: got=%#v", v.Double()) 138 } 139 140 if v.Uint32() != 0 { 141 t.Errorf("uint32 not zero value: got=%#v", v.Uint32()) 142 } 143 144 if v.Uint64() != 0 { 145 t.Errorf("uint64 not zero value: got=%#v", v.Uint64()) 146 } 147 148 var zeroByte []byte 149 if !bytes.Equal(v.ByteArray(), zeroByte) { 150 t.Errorf("byte array not zero value: got=%#v", v.ByteArray()) 151 } 152 }