github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/div/div_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 div 16 17 import ( 18 "testing" 19 20 "github.com/matrixorigin/matrixone/pkg/common/moerr" 21 "github.com/matrixorigin/matrixone/pkg/container/types" 22 "github.com/matrixorigin/matrixone/pkg/container/vector" 23 "github.com/matrixorigin/matrixone/pkg/testutil" 24 "github.com/stretchr/testify/require" 25 ) 26 27 func TestF32DivOf(t *testing.T) { 28 as := make([]float32, 2) 29 bs := make([]float32, 2) 30 for i := 0; i < 2; i++ { 31 as[i] = 50 32 bs[i] = float32(i) 33 } 34 cs := make([]float32, 2) 35 av := testutil.MakeFloat32Vector(as, nil) 36 bv := testutil.MakeFloat32Vector(bs, nil) 37 cv := testutil.MakeFloat32Vector(cs, nil) 38 39 err := NumericDivFloat[float32](av, bv, cv) 40 if err == nil { 41 t.Fatalf("should have overflowed.") 42 } 43 } 44 45 func TestF32Div(t *testing.T) { 46 as := make([]float32, 10) 47 bs := make([]float32, 10) 48 cs := make([]float32, 10) 49 for i := 0; i < 10; i++ { 50 as[i] = float32(i * 100) 51 bs[i] = float32(5) 52 } 53 54 av := testutil.MakeFloat32Vector(as, nil) 55 bv := testutil.MakeFloat32Vector(bs, nil) 56 cv := testutil.MakeFloat32Vector(cs, nil) 57 58 err := NumericDivFloat[float32](av, bv, cv) 59 if err != nil { 60 t.Fatalf("decimal64 div failed") 61 } 62 63 res := vector.MustTCols[float32](cv) 64 for i := 0; i < 10; i++ { 65 require.Equal(t, res[i], as[i]/bs[i]) 66 } 67 } 68 69 func TestF64Div(t *testing.T) { 70 as := make([]float64, 10) 71 bs := make([]float64, 10) 72 cs := make([]float64, 10) 73 for i := 0; i < 10; i++ { 74 as[i] = float64(i * 1024) 75 bs[i] = float64(8.2) 76 } 77 78 av := testutil.MakeFloat64Vector(as, nil) 79 bv := testutil.MakeFloat64Vector(bs, nil) 80 cv := testutil.MakeFloat64Vector(cs, nil) 81 82 err := NumericDivFloat[float64](av, bv, cv) 83 if err != nil { 84 t.Fatalf("decimal64 div failed") 85 } 86 87 res := vector.MustTCols[float64](cv) 88 for i := 0; i < 10; i++ { 89 require.Equal(t, res[i], as[i]/bs[i]) 90 } 91 } 92 93 func TestDec64DivByZero(t *testing.T) { 94 as := make([]int64, 10) 95 bs := make([]int64, 10) 96 cs := make([]int64, 10) 97 for i := 0; i < 10; i++ { 98 as[i] = int64((i + 1) * 1024) 99 bs[i] = int64(i) 100 } 101 102 av := testutil.MakeDecimal64Vector(as, nil, types.T_decimal64.ToType()) 103 bv := testutil.MakeDecimal64Vector(bs, nil, types.T_decimal64.ToType()) 104 cv := testutil.MakeDecimal128Vector(cs, nil, types.T_decimal128.ToType()) 105 106 err := Decimal64VecDiv(av, bv, cv) 107 if err != nil { 108 if !moerr.IsMoErrCode(err, moerr.ErrDivByZero) { 109 t.Fatalf("should have div by zero error.") 110 } 111 } 112 } 113 114 func TestDec128DivByZero(t *testing.T) { 115 as := make([]int64, 10) 116 bs := make([]int64, 10) 117 cs := make([]int64, 10) 118 for i := 0; i < 10; i++ { 119 as[i] = int64((i + 1) * 1024) 120 bs[i] = int64(i) 121 } 122 123 av := testutil.MakeDecimal128Vector(as, nil, types.T_decimal128.ToType()) 124 bv := testutil.MakeDecimal128Vector(bs, nil, types.T_decimal128.ToType()) 125 cv := testutil.MakeDecimal128Vector(cs, nil, types.T_decimal128.ToType()) 126 127 err := Decimal128VecDiv(av, bv, cv) 128 if err != nil { 129 if !moerr.IsMoErrCode(err, moerr.ErrDivByZero) { 130 t.Fatalf("should have div by zero error.") 131 } 132 } 133 } 134 135 func TestDec128Div(t *testing.T) { 136 as := make([]int64, 10) 137 bs := make([]int64, 10) 138 cs := make([]int64, 10) 139 for i := 0; i < 10; i++ { 140 as[i] = int64((i + 1) * 1024) 141 bs[i] = int64(2) 142 } 143 144 av := testutil.MakeDecimal128Vector(as, nil, types.T_decimal128.ToType()) 145 bv := testutil.MakeDecimal128Vector(bs, nil, types.T_decimal128.ToType()) 146 cv := testutil.MakeDecimal128Vector(cs, nil, types.T_decimal128.ToType()) 147 148 err := Decimal128VecDiv(av, bv, cv) 149 if err != nil { 150 t.Fatalf("decimal128 div failed") 151 } 152 153 res := vector.MustTCols[types.Decimal128](cv) 154 for i := 0; i < 10; i++ { 155 d, _ := types.Decimal128_FromInt64(as[i]/bs[i], 64, 0) 156 if !res[i].Eq(d) { 157 t.Fatalf("decimal128 div wrong result") 158 } 159 } 160 } 161 162 func BenchmarkDivF64_C(b *testing.B) { 163 as := make([]float64, 8192) 164 bs := make([]float64, 8192) 165 for i := 0; i < 8192; i++ { 166 as[i] = float64(i) 167 bs[i] = 1 168 } 169 170 cs := make([]float64, 8192) 171 172 av := testutil.MakeFloat64Vector(as, nil) 173 bv := testutil.MakeFloat64Vector(bs, nil) 174 cv := testutil.MakeFloat64Vector(cs, nil) 175 176 for i := 0; i < b.N; i++ { 177 if err := NumericDivFloat[float64](av, bv, cv); err != nil { 178 b.Fail() 179 } 180 } 181 } 182 183 func BenchmarkDivDec64(b *testing.B) { 184 as := make([]int64, 8192) 185 bs := make([]int64, 8192) 186 cs := make([]int64, 8192) 187 for i := 0; i < 8192; i++ { 188 as[i] = int64(i) 189 bs[i] = 1 190 } 191 192 av := testutil.MakeDecimal64Vector(as, nil, types.T_decimal64.ToType()) 193 bv := testutil.MakeDecimal64Vector(bs, nil, types.T_decimal64.ToType()) 194 cv := testutil.MakeDecimal128Vector(cs, nil, types.T_decimal128.ToType()) 195 196 for i := 0; i < b.N; i++ { 197 if err := Decimal64VecDiv(av, bv, cv); err != nil { 198 b.Fail() 199 } 200 } 201 } 202 203 func BenchmarkDivDec128(b *testing.B) { 204 as := make([]int64, 8192) 205 bs := make([]int64, 8192) 206 cs := make([]int64, 8192) 207 for i := 0; i < 8192; i++ { 208 as[i] = int64(i) 209 bs[i] = 1 210 } 211 212 av := testutil.MakeDecimal128Vector(as, nil, types.T_decimal128.ToType()) 213 bv := testutil.MakeDecimal128Vector(bs, nil, types.T_decimal128.ToType()) 214 cv := testutil.MakeDecimal128Vector(cs, nil, types.T_decimal128.ToType()) 215 216 for i := 0; i < b.N; i++ { 217 if err := Decimal128VecDiv(av, bv, cv); err != nil { 218 b.Fail() 219 } 220 } 221 }