github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/mod/mod_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 mod 16 17 import ( 18 "math" 19 "testing" 20 21 "github.com/matrixorigin/matrixone/pkg/container/vector" 22 "github.com/matrixorigin/matrixone/pkg/testutil" 23 ) 24 25 func TestI32ModOf(t *testing.T) { 26 as := make([]int32, 2) 27 bs := make([]int32, 2) 28 for i := 0; i < 2; i++ { 29 as[i] = math.MaxInt32 30 bs[i] = int32(i) 31 } 32 cs := make([]int32, 2) 33 av := testutil.MakeInt32Vector(as, nil) 34 bv := testutil.MakeInt32Vector(bs, nil) 35 cv := testutil.MakeInt32Vector(cs, nil) 36 37 err := NumericModSigned[int32](av, bv, cv) 38 if err == nil { 39 t.Fatalf("should have overflowed.") 40 } 41 } 42 43 func TestU32ModOf(t *testing.T) { 44 as := make([]uint32, 2) 45 bs := make([]uint32, 2) 46 for i := 0; i < 2; i++ { 47 as[i] = math.MaxUint32 48 bs[i] = uint32(i) 49 } 50 cs := make([]uint32, 2) 51 av := testutil.MakeUint32Vector(as, nil) 52 bv := testutil.MakeUint32Vector(bs, nil) 53 cv := testutil.MakeUint32Vector(cs, nil) 54 55 err := NumericModUnsigned[uint32](av, bv, cv) 56 if err == nil { 57 t.Fatalf("should have overflowed.") 58 } 59 } 60 61 func TestI32Mod(t *testing.T) { 62 as := make([]int32, 10) 63 bs := make([]int32, 10) 64 cs := make([]int32, 10) 65 for i := 0; i < 10; i++ { 66 as[i] = int32((i + 1) * 1024) 67 bs[i] = int32(7) 68 } 69 70 av := testutil.MakeInt32Vector(as, nil) 71 bv := testutil.MakeInt32Vector(bs, nil) 72 cv := testutil.MakeInt32Vector(cs, nil) 73 74 err := NumericModSigned[int32](av, bv, cv) 75 if err != nil { 76 t.Fatalf("int32 mod failed") 77 } 78 79 res := vector.MustTCols[int32](cv) 80 for i := 0; i < 10; i++ { 81 if res[i] != as[i]%bs[i] { 82 t.Fatalf("int mod wrong result") 83 } 84 } 85 } 86 87 func TestUI32Mod(t *testing.T) { 88 as := make([]uint32, 10) 89 bs := make([]uint32, 10) 90 cs := make([]uint32, 10) 91 for i := 0; i < 10; i++ { 92 as[i] = uint32((i + 1) * 1024) 93 bs[i] = uint32(7) 94 } 95 96 av := testutil.MakeUint32Vector(as, nil) 97 bv := testutil.MakeUint32Vector(bs, nil) 98 cv := testutil.MakeUint32Vector(cs, nil) 99 100 err := NumericModUnsigned[uint32](av, bv, cv) 101 if err != nil { 102 t.Fatalf("uint32 mod failed") 103 } 104 105 res := vector.MustTCols[uint32](cv) 106 for i := 0; i < 10; i++ { 107 if res[i] != as[i]%bs[i] { 108 t.Fatalf("int mod wrong result") 109 } 110 } 111 } 112 113 func TestF32Mod(t *testing.T) { 114 as := make([]float64, 10) 115 bs := make([]float64, 10) 116 cs := make([]float64, 10) 117 for i := 0; i < 10; i++ { 118 as[i] = float64(747.34 * (float64)(i+3)) 119 bs[i] = float64(2.5) 120 } 121 122 av := testutil.MakeFloat64Vector(as, nil) 123 bv := testutil.MakeFloat64Vector(bs, nil) 124 cv := testutil.MakeFloat64Vector(cs, nil) 125 126 err := NumericModFloat[float64](av, bv, cv) 127 if err != nil { 128 t.Fatalf("uint32 mod failed") 129 } 130 131 res := vector.MustTCols[float64](cv) 132 for i := 0; i < 10; i++ { 133 if res[i] != math.Mod(as[i], bs[i]) { 134 t.Fatalf("int mod wrong result") 135 } 136 } 137 } 138 139 func BenchmarkModI32_C(b *testing.B) { 140 as := make([]int32, 8192) 141 bs := make([]int32, 8192) 142 for i := 0; i < 8192; i++ { 143 as[i] = int32(i) 144 bs[i] = 1 145 } 146 147 cs := make([]int32, 8192) 148 149 av := testutil.MakeInt32Vector(as, nil) 150 bv := testutil.MakeInt32Vector(bs, nil) 151 cv := testutil.MakeInt32Vector(cs, nil) 152 153 for i := 0; i < b.N; i++ { 154 if err := NumericModSigned[int32](av, bv, cv); err != nil { 155 b.Fail() 156 } 157 } 158 } 159 160 func BenchmarkAddUI32_C(b *testing.B) { 161 as := make([]uint32, 8192) 162 bs := make([]uint32, 8192) 163 for i := 0; i < 8192; i++ { 164 as[i] = uint32(i) 165 bs[i] = 1 166 } 167 168 cs := make([]uint32, 8192) 169 170 av := testutil.MakeUint32Vector(as, nil) 171 bv := testutil.MakeUint32Vector(bs, nil) 172 cv := testutil.MakeUint32Vector(cs, nil) 173 174 for i := 0; i < b.N; i++ { 175 if err := NumericModUnsigned[uint32](av, bv, cv); err != nil { 176 b.Fail() 177 } 178 } 179 } 180 181 func BenchmarkModF64_C(b *testing.B) { 182 as := make([]float64, 8192) 183 bs := make([]float64, 8192) 184 for i := 0; i < 8192; i++ { 185 as[i] = float64(i) 186 bs[i] = 1 187 } 188 189 cs := make([]float64, 8192) 190 191 av := testutil.MakeFloat64Vector(as, nil) 192 bv := testutil.MakeFloat64Vector(bs, nil) 193 cv := testutil.MakeFloat64Vector(cs, nil) 194 195 for i := 0; i < b.N; i++ { 196 if err := NumericModFloat[float64](av, bv, cv); err != nil { 197 b.Fail() 198 } 199 } 200 }