github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/compare/lessThan_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 compare 16 17 import ( 18 "fmt" 19 "testing" 20 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/assert" 25 ) 26 27 func TestI32Lt(t *testing.T) { 28 as := make([]int32, 10) 29 bs := make([]int32, 10) 30 for i := 0; i < 10; i++ { 31 as[i] = 4 32 bs[i] = int32(i - 3) 33 } 34 cs := make([]bool, 10) 35 av := testutil.MakeInt32Vector(as, nil) 36 bv := testutil.MakeInt32Vector(bs, nil) 37 cv := testutil.MakeBoolVector(cs) 38 39 err := NumericLessThan[int32](av, bv, cv) 40 if err != nil { 41 t.Fatal(err) 42 t.Fatalf("should not error.") 43 } 44 45 res := vector.MustTCols[bool](cv) 46 for i := 0; i < 10; i++ { 47 fmt.Printf("%+v < %+v \n", as[i], bs[i]) 48 assert.Equal(t, as[i] < bs[i], res[i]) 49 } 50 } 51 52 func TestU32Lt(t *testing.T) { 53 as := make([]uint32, 10) 54 bs := make([]uint32, 10) 55 for i := 0; i < 10; i++ { 56 as[i] = 8 57 bs[i] = uint32(i + 3) 58 } 59 cs := make([]bool, 10) 60 av := testutil.MakeUint32Vector(as, nil) 61 bv := testutil.MakeUint32Vector(bs, nil) 62 cv := testutil.MakeBoolVector(cs) 63 64 err := NumericLessThan[uint32](av, bv, cv) 65 if err != nil { 66 t.Fatal(err) 67 t.Fatalf("should not error.") 68 } 69 70 res := vector.MustTCols[bool](cv) 71 for i := 0; i < 10; i++ { 72 fmt.Printf("%+v < %+v \n", as[i], bs[i]) 73 assert.Equal(t, as[i] < bs[i], res[i]) 74 } 75 } 76 77 func TestF32Lt(t *testing.T) { 78 as := make([]float32, 2) 79 bs := make([]float32, 2) 80 for i := 0; i < 2; i++ { 81 as[i] = 2.5 82 bs[i] = float32(i) + 1.5 83 } 84 cs := make([]bool, 2) 85 av := testutil.MakeFloat32Vector(as, nil) 86 bv := testutil.MakeFloat32Vector(bs, nil) 87 cv := testutil.MakeBoolVector(cs) 88 89 err := NumericLessThan[float32](av, bv, cv) 90 if err != nil { 91 t.Fatalf("should not error.") 92 } 93 94 res := vector.MustTCols[bool](cv) 95 for i := 0; i < 2; i++ { 96 fmt.Printf("%+v < %+v \n", as[i], bs[i]) 97 assert.Equal(t, as[i] < bs[i], res[i]) 98 } 99 } 100 101 func TestF64Lt(t *testing.T) { 102 as := make([]float64, 2) 103 bs := make([]float64, 2) 104 for i := 0; i < 2; i++ { 105 as[i] = 2.5 106 bs[i] = float64(i) + 1.5 107 } 108 cs := make([]bool, 2) 109 av := testutil.MakeFloat64Vector(as, nil) 110 bv := testutil.MakeFloat64Vector(bs, nil) 111 cv := testutil.MakeBoolVector(cs) 112 113 err := NumericLessThan[float64](av, bv, cv) 114 if err != nil { 115 t.Fatalf("should not error.") 116 } 117 118 res := vector.MustTCols[bool](cv) 119 for i := 0; i < 2; i++ { 120 fmt.Printf("%+v < %+v \n", as[i], bs[i]) 121 assert.Equal(t, as[i] < bs[i], res[i]) 122 } 123 } 124 125 func TestBoolLt(t *testing.T) { 126 as := make([]bool, 2) 127 bs := make([]bool, 2) 128 for i := 0; i < 2; i++ { 129 as[i] = true 130 bs[i] = false 131 } 132 cs := make([]bool, 2) 133 av := testutil.MakeBoolVector(as) 134 bv := testutil.MakeBoolVector(bs) 135 cv := testutil.MakeBoolVector(cs) 136 137 err := NumericLessThan[bool](av, bv, cv) 138 if err != nil { 139 t.Fatalf("should not error.") 140 } 141 142 res := vector.MustTCols[bool](cv) 143 for i := 0; i < 2; i++ { 144 fmt.Printf("%+v < %+v : %v \n", as[i], bs[i], res[i]) 145 assert.Equal(t, !(as[i]) && bs[i], res[i]) 146 } 147 } 148 149 func TestDec64Lt(t *testing.T) { 150 as := make([]int64, 10) 151 bs := make([]int64, 10) 152 cs := make([]bool, 10) 153 for i := 0; i < 10; i++ { 154 as[i] = int64(i + 5) 155 bs[i] = int64(3 * i) 156 } 157 158 av := testutil.MakeDecimal64Vector(as, nil, types.T_decimal64.ToType()) 159 bv := testutil.MakeDecimal64Vector(bs, nil, types.T_decimal64.ToType()) 160 cv := testutil.MakeBoolVector(cs) 161 162 err := Decimal64VecLt(av, bv, cv) 163 if err != nil { 164 t.Fatalf("decimal64 less than failed") 165 } 166 167 res := vector.MustTCols[bool](cv) 168 for i := 0; i < 10; i++ { 169 fmt.Printf("%+v < %+v \n", as[i], bs[i]) 170 assert.Equal(t, as[i] < bs[i], res[i]) 171 } 172 } 173 174 func TestDec128Lt(t *testing.T) { 175 as := make([]int64, 10) 176 bs := make([]int64, 10) 177 cs := make([]bool, 10) 178 for i := 0; i < 10; i++ { 179 as[i] = int64(i) 180 bs[i] = int64(3 * i) 181 } 182 183 av := testutil.MakeDecimal128Vector(as, nil, types.T_decimal128.ToType()) 184 bv := testutil.MakeDecimal128Vector(bs, nil, types.T_decimal128.ToType()) 185 cv := testutil.MakeBoolVector(cs) 186 187 err := Decimal128VecLt(av, bv, cv) 188 if err != nil { 189 t.Fatalf("decimal128 less than failed") 190 } 191 192 res := vector.MustTCols[bool](cv) 193 for i := 0; i < 10; i++ { 194 fmt.Printf("%+v < %+v \n", as[i], bs[i]) 195 assert.Equal(t, as[i] < bs[i], res[i]) 196 } 197 }