github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/unary/oct_test.go (about) 1 // Copyright 2022 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package unary 16 17 import ( 18 "testing" 19 20 "github.com/matrixorigin/matrixone/pkg/container/types" 21 "github.com/matrixorigin/matrixone/pkg/container/vector" 22 "github.com/matrixorigin/matrixone/pkg/testutil" 23 "github.com/stretchr/testify/require" 24 ) 25 26 func TestOctUint8(t *testing.T) { 27 procs := testutil.NewProc() 28 vecs := make([]*vector.Vector, 1) 29 vecs[0] = testutil.MakeUint8Vector([]uint8{12, 99, 100, 255}, nil) 30 e1, _ := types.Decimal128_FromStringWithScale("14", 64, 0) 31 e2, _ := types.Decimal128_FromStringWithScale("143", 64, 0) 32 e3, _ := types.Decimal128_FromStringWithScale("144", 64, 0) 33 e4, _ := types.Decimal128_FromStringWithScale("377", 64, 0) 34 expected := []types.Decimal128{e1, e2, e3, e4} 35 36 t.Run("oct uin8 test", func(t *testing.T) { 37 result, err := Oct[uint8](vecs, procs) 38 if err != nil { 39 t.Fatal(err) 40 } 41 42 checkOctResult(t, result, expected, false) 43 }) 44 } 45 46 func TestOctUint16(t *testing.T) { 47 procs := testutil.NewProc() 48 vecs := make([]*vector.Vector, 1) 49 vecs[0] = testutil.MakeUint16Vector([]uint16{12, 99, 100, 255, 1024, 10000, 65535}, nil) 50 e1, _ := types.Decimal128_FromStringWithScale("14", 64, 0) 51 e2, _ := types.Decimal128_FromStringWithScale("143", 64, 0) 52 e3, _ := types.Decimal128_FromStringWithScale("144", 64, 0) 53 e4, _ := types.Decimal128_FromStringWithScale("377", 64, 0) 54 e5, _ := types.Decimal128_FromStringWithScale("2000", 64, 0) 55 e6, _ := types.Decimal128_FromStringWithScale("23420", 64, 0) 56 e7, _ := types.Decimal128_FromStringWithScale("177777", 64, 0) 57 expected := []types.Decimal128{e1, e2, e3, e4, e5, e6, e7} 58 59 t.Run("oct uin16 test", func(t *testing.T) { 60 result, err := Oct[uint16](vecs, procs) 61 if err != nil { 62 t.Fatal(err) 63 } 64 checkOctResult(t, result, expected, false) 65 }) 66 } 67 68 func TestOctUint32(t *testing.T) { 69 procs := testutil.NewProc() 70 vecs := []*vector.Vector{testutil.MakeUint32Vector([]uint32{12, 99, 100, 255, 1024, 10000, 65535, 4294967295}, nil)} 71 e1, _ := types.Decimal128_FromStringWithScale("14", 64, 0) 72 e2, _ := types.Decimal128_FromStringWithScale("143", 64, 0) 73 e3, _ := types.Decimal128_FromStringWithScale("144", 64, 0) 74 e4, _ := types.Decimal128_FromStringWithScale("377", 64, 0) 75 e5, _ := types.Decimal128_FromStringWithScale("2000", 64, 0) 76 e6, _ := types.Decimal128_FromStringWithScale("23420", 64, 0) 77 e7, _ := types.Decimal128_FromStringWithScale("177777", 64, 0) 78 e8, _ := types.Decimal128_FromStringWithScale("37777777777", 64, 0) 79 expected := []types.Decimal128{e1, e2, e3, e4, e5, e6, e7, e8} 80 81 t.Run("oct uin32 test", func(t *testing.T) { 82 result, err := Oct[uint32](vecs, procs) 83 if err != nil { 84 t.Fatal(err) 85 } 86 87 checkOctResult(t, result, expected, false) 88 }) 89 } 90 91 func TestOctUint64(t *testing.T) { 92 procs := testutil.NewProc() 93 vecs := []*vector.Vector{testutil.MakeUint64Vector([]uint64{12, 99, 100, 255, 1024, 10000, 65535, 4294967295, 18446744073709551615}, nil)} 94 e1, _ := types.Decimal128_FromStringWithScale("14", 64, 0) 95 e2, _ := types.Decimal128_FromStringWithScale("143", 64, 0) 96 e3, _ := types.Decimal128_FromStringWithScale("144", 64, 0) 97 e4, _ := types.Decimal128_FromStringWithScale("377", 64, 0) 98 e5, _ := types.Decimal128_FromStringWithScale("2000", 64, 0) 99 e6, _ := types.Decimal128_FromStringWithScale("23420", 64, 0) 100 e7, _ := types.Decimal128_FromStringWithScale("177777", 64, 0) 101 e8, _ := types.Decimal128_FromStringWithScale("37777777777", 64, 0) 102 e9, _ := types.Decimal128_FromStringWithScale("1777777777777777777777", 64, 0) 103 expected := []types.Decimal128{e1, e2, e3, e4, e5, e6, e7, e8, e9} 104 105 t.Run("oct uin64 test", func(t *testing.T) { 106 result, err := Oct[uint64](vecs, procs) 107 if err != nil { 108 t.Fatal(err) 109 } 110 checkOctResult(t, result, expected, false) 111 }) 112 } 113 114 func TestOctInt8(t *testing.T) { 115 procs := testutil.NewProc() 116 vecs := []*vector.Vector{testutil.MakeInt8Vector([]int8{-128, -1, 127}, nil)} 117 e1, _ := types.Decimal128_FromStringWithScale("1777777777777777777600", 64, 0) 118 e2, _ := types.Decimal128_FromStringWithScale("1777777777777777777777", 64, 0) 119 e3, _ := types.Decimal128_FromStringWithScale("177", 64, 0) 120 expected := []types.Decimal128{e1, e2, e3} 121 122 t.Run("oct int8 test", func(t *testing.T) { 123 result, err := Oct[int8](vecs, procs) 124 if err != nil { 125 t.Fatal(err) 126 } 127 checkOctResult(t, result, expected, false) 128 }) 129 } 130 131 func TestOctInt16(t *testing.T) { 132 procs := testutil.NewProc() 133 vecs := []*vector.Vector{testutil.MakeInt16Vector([]int16{-32768}, nil)} 134 e1, _ := types.Decimal128_FromStringWithScale("1777777777777777700000", 64, 0) 135 expected := []types.Decimal128{e1} 136 137 t.Run("oct int16 test", func(t *testing.T) { 138 result, err := Oct[int16](vecs, procs) 139 if err != nil { 140 t.Fatal(err) 141 } 142 checkOctResult(t, result, expected, false) 143 }) 144 } 145 146 func TestOctInt32(t *testing.T) { 147 procs := testutil.NewProc() 148 vecs := []*vector.Vector{testutil.MakeInt32Vector([]int32{-2147483648}, nil)} 149 e1, _ := types.Decimal128_FromStringWithScale("1777777777760000000000", 64, 0) 150 expected := []types.Decimal128{e1} 151 152 t.Run("oct int32 test", func(t *testing.T) { 153 result, err := Oct[int32](vecs, procs) 154 if err != nil { 155 t.Fatal(err) 156 } 157 checkOctResult(t, result, expected, false) 158 }) 159 } 160 161 func TestOctInt64(t *testing.T) { 162 procs := testutil.NewProc() 163 vecs := []*vector.Vector{testutil.MakeInt64Vector([]int64{-9223372036854775808}, nil)} 164 e1, _ := types.Decimal128_FromStringWithScale("1000000000000000000000", 64, 0) 165 expected := []types.Decimal128{e1} 166 167 t.Run("oct int64 test", func(t *testing.T) { 168 result, err := Oct[int64](vecs, procs) 169 if err != nil { 170 t.Fatal(err) 171 } 172 checkOctResult(t, result, expected, false) 173 }) 174 } 175 176 func TestOctScalar(t *testing.T) { 177 procs := testutil.NewProc() 178 vecs := []*vector.Vector{testutil.MakeInt64Vector([]int64{-9223372036854775808}, nil)} 179 vecs[0].MakeScalar(1) 180 e1, _ := types.Decimal128_FromStringWithScale("1000000000000000000000", 64, 0) 181 expected := []types.Decimal128{e1} 182 183 t.Run("oct scalar test", func(t *testing.T) { 184 result, err := Oct[int64](vecs, procs) 185 if err != nil { 186 t.Fatal(err) 187 } 188 checkOctResult(t, result, expected, true) 189 }) 190 } 191 192 func checkOctResult(t *testing.T, result *vector.Vector, expected []types.Decimal128, isScalar bool) { 193 col := result.Col.([]types.Decimal128) 194 195 require.Equal(t, expected, col) 196 require.Equal(t, isScalar, result.IsScalar()) 197 }