github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/compare/equal_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 TestI32Eq(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 := NumericEqual[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  		fmt.Printf("actual res:%+v\n", res[i])
    49  		fmt.Printf("expect res:%+v\n", as[i] == bs[i])
    50  		if res[i] != (as[i] == bs[i]) {
    51  			t.Fatalf("int32 equal wrong result")
    52  		}
    53  	}
    54  }
    55  
    56  func TestU32Eq(t *testing.T) {
    57  	as := make([]uint32, 10)
    58  	bs := make([]uint32, 10)
    59  	for i := 0; i < 10; i++ {
    60  		as[i] = 8
    61  		bs[i] = uint32(i + 3)
    62  	}
    63  	cs := make([]bool, 10)
    64  	av := testutil.MakeUint32Vector(as, nil)
    65  	bv := testutil.MakeUint32Vector(bs, nil)
    66  	cv := testutil.MakeBoolVector(cs)
    67  
    68  	err := NumericEqual[uint32](av, bv, cv)
    69  	if err != nil {
    70  		t.Fatal(err)
    71  		t.Fatalf("should not error.")
    72  	}
    73  
    74  	res := vector.MustTCols[bool](cv)
    75  	for i := 0; i < 10; i++ {
    76  		fmt.Printf("%+v == %+v \n", as[i], bs[i])
    77  		fmt.Printf("actual res:%+v\n", res[i])
    78  		fmt.Printf("expect res:%+v\n", as[i] == bs[i])
    79  		if res[i] != (as[i] == bs[i]) {
    80  			t.Fatalf("uint32 equal wrong result")
    81  		}
    82  	}
    83  }
    84  
    85  func TestF32Eq(t *testing.T) {
    86  	as := make([]float32, 2)
    87  	bs := make([]float32, 2)
    88  	for i := 0; i < 2; i++ {
    89  		as[i] = 2.5
    90  		bs[i] = float32(i) + 1.5
    91  	}
    92  	cs := make([]bool, 2)
    93  	av := testutil.MakeFloat32Vector(as, nil)
    94  	bv := testutil.MakeFloat32Vector(bs, nil)
    95  	cv := testutil.MakeBoolVector(cs)
    96  
    97  	err := NumericEqual[float32](av, bv, cv)
    98  	if err != nil {
    99  		t.Fatalf("should not error.")
   100  	}
   101  
   102  	res := vector.MustTCols[bool](cv)
   103  	for i := 0; i < 2; i++ {
   104  		//fmt.Printf("%+v == %+v \n", as[i], bs[i])
   105  		//fmt.Printf("actual res:%+v\n", res[i])
   106  		//fmt.Printf("expect res:%+v\n", as[i] == bs[i])
   107  		if res[i] != (as[i] == bs[i]) {
   108  			t.Fatalf("float32 equal wrong result")
   109  		}
   110  	}
   111  }
   112  
   113  func TestF64Eq(t *testing.T) {
   114  	as := make([]float64, 2)
   115  	bs := make([]float64, 2)
   116  	for i := 0; i < 2; i++ {
   117  		as[i] = 2.5
   118  		bs[i] = float64(i) + 1.5
   119  	}
   120  	cs := make([]bool, 2)
   121  	av := testutil.MakeFloat64Vector(as, nil)
   122  	bv := testutil.MakeFloat64Vector(bs, nil)
   123  	cv := testutil.MakeBoolVector(cs)
   124  
   125  	err := NumericEqual[float64](av, bv, cv)
   126  	if err != nil {
   127  		t.Fatalf("should not error.")
   128  	}
   129  
   130  	res := vector.MustTCols[bool](cv)
   131  	for i := 0; i < 2; i++ {
   132  		//fmt.Printf("%+v == %+v \n", as[i], bs[i])
   133  		//fmt.Printf("actual res:%+v\n", res[i])
   134  		//fmt.Printf("expect res:%+v\n", as[i] == bs[i])
   135  		if res[i] != (as[i] == bs[i]) {
   136  			t.Fatalf("float32 equal wrong result")
   137  		}
   138  	}
   139  }
   140  
   141  func TestBoolEq(t *testing.T) {
   142  	as := make([]bool, 2)
   143  	bs := make([]bool, 2)
   144  	for i := 0; i < 2; i++ {
   145  		as[i] = true
   146  		bs[i] = false
   147  	}
   148  	cs := make([]bool, 2)
   149  	av := testutil.MakeBoolVector(as)
   150  	bv := testutil.MakeBoolVector(bs)
   151  	cv := testutil.MakeBoolVector(cs)
   152  
   153  	err := NumericEqual[bool](av, bv, cv)
   154  	if err != nil {
   155  		t.Fatalf("should not error.")
   156  	}
   157  
   158  	res := vector.MustTCols[bool](cv)
   159  	for i := 0; i < 2; i++ {
   160  		fmt.Printf("%+v == %+v : %v \n", as[i], bs[i], res[i])
   161  		assert.Equal(t, as[i] == bs[i], res[i])
   162  	}
   163  }
   164  
   165  func TestDec64Eq(t *testing.T) {
   166  	as := make([]int64, 10)
   167  	bs := make([]int64, 10)
   168  	cs := make([]bool, 10)
   169  	for i := 0; i < 10; i++ {
   170  		as[i] = int64(i)
   171  		bs[i] = int64(3 * i)
   172  	}
   173  
   174  	av := testutil.MakeDecimal64Vector(as, nil, types.T_decimal64.ToType())
   175  	bv := testutil.MakeDecimal64Vector(bs, nil, types.T_decimal64.ToType())
   176  	cv := testutil.MakeBoolVector(cs)
   177  
   178  	err := Decimal64VecEq(av, bv, cv)
   179  	if err != nil {
   180  		t.Fatalf("decimal64 equal failed")
   181  	}
   182  
   183  	res := vector.MustTCols[bool](cv)
   184  	for i := 0; i < 10; i++ {
   185  		fmt.Printf("%+v == %+v \n", as[i], bs[i])
   186  		fmt.Printf("actual res:%+v\n", res[i])
   187  		fmt.Printf("expect res:%+v\n", as[i] == bs[i])
   188  		if res[i] != (as[i] == bs[i]) {
   189  			t.Fatalf("decimal64 equal wrong result")
   190  		}
   191  	}
   192  }
   193  
   194  func TestDec128Eq(t *testing.T) {
   195  	as := make([]int64, 10)
   196  	bs := make([]int64, 10)
   197  	cs := make([]bool, 10)
   198  	for i := 0; i < 10; i++ {
   199  		as[i] = int64(i)
   200  		bs[i] = int64(3 * i)
   201  	}
   202  
   203  	av := testutil.MakeDecimal128Vector(as, nil, types.T_decimal128.ToType())
   204  	bv := testutil.MakeDecimal128Vector(bs, nil, types.T_decimal128.ToType())
   205  	cv := testutil.MakeBoolVector(cs)
   206  
   207  	err := Decimal128VecEq(av, bv, cv)
   208  	if err != nil {
   209  		t.Fatalf("decimal128 equal failed")
   210  	}
   211  
   212  	res := vector.MustTCols[bool](cv)
   213  	for i := 0; i < 10; i++ {
   214  		fmt.Printf("%+v == %+v \n", as[i], bs[i])
   215  		fmt.Printf("actual res:%+v\n", res[i])
   216  		fmt.Printf("expect res:%+v\n", as[i] == bs[i])
   217  		if res[i] != (as[i] == bs[i]) {
   218  			t.Fatalf("decimal128 equal wrong result")
   219  		}
   220  	}
   221  }
   222  
   223  // benach mark test
   224  
   225  func BenchmarkEqI32Eq_C(b *testing.B) {
   226  	as := make([]int32, 8192)
   227  	bs := make([]int32, 8192)
   228  	for i := 0; i < 8192; i++ {
   229  		as[i] = int32(i)
   230  		bs[i] = 1
   231  	}
   232  
   233  	cs := make([]bool, 8192)
   234  
   235  	av := testutil.MakeInt32Vector(as, nil)
   236  	bv := testutil.MakeInt32Vector(bs, nil)
   237  	cv := testutil.MakeBoolVector(cs)
   238  
   239  	for i := 0; i < b.N; i++ {
   240  		if err := NumericEqual[int32](av, bv, cv); err != nil {
   241  			b.Fail()
   242  		}
   243  	}
   244  }
   245  
   246  func BenchmarkEqUI32Eq_C(b *testing.B) {
   247  	as := make([]uint32, 8192)
   248  	bs := make([]uint32, 8192)
   249  	for i := 0; i < 8192; i++ {
   250  		as[i] = uint32(i)
   251  		bs[i] = 1
   252  	}
   253  
   254  	cs := make([]bool, 8192)
   255  
   256  	av := testutil.MakeUint32Vector(as, nil)
   257  	bv := testutil.MakeUint32Vector(bs, nil)
   258  	cv := testutil.MakeBoolVector(cs)
   259  
   260  	for i := 0; i < b.N; i++ {
   261  		if err := NumericEqual[uint32](av, bv, cv); err != nil {
   262  			b.Fail()
   263  		}
   264  	}
   265  }
   266  
   267  func BenchmarkEqF64Eq_C(b *testing.B) {
   268  	as := make([]float64, 8192)
   269  	bs := make([]float64, 8192)
   270  	for i := 0; i < 8192; i++ {
   271  		as[i] = float64(i)
   272  		bs[i] = 1
   273  	}
   274  
   275  	cs := make([]bool, 8192)
   276  
   277  	av := testutil.MakeFloat64Vector(as, nil)
   278  	bv := testutil.MakeFloat64Vector(bs, nil)
   279  	cv := testutil.MakeBoolVector(cs)
   280  
   281  	for i := 0; i < b.N; i++ {
   282  		if err := NumericEqual[float64](av, bv, cv); err != nil {
   283  			b.Fail()
   284  		}
   285  	}
   286  }
   287  
   288  func BenchmarkEqDec64(b *testing.B) {
   289  	as := make([]int64, 8192)
   290  	bs := make([]int64, 8192)
   291  	cs := make([]bool, 8192)
   292  	for i := 0; i < 8192; i++ {
   293  		as[i] = int64(i)
   294  		bs[i] = 1
   295  	}
   296  
   297  	av := testutil.MakeDecimal64Vector(as, nil, types.T_decimal64.ToType())
   298  	bv := testutil.MakeDecimal64Vector(bs, nil, types.T_decimal64.ToType())
   299  	cv := testutil.MakeBoolVector(cs)
   300  
   301  	for i := 0; i < b.N; i++ {
   302  		if err := Decimal64VecEq(av, bv, cv); err != nil {
   303  			b.Fail()
   304  		}
   305  	}
   306  }
   307  
   308  func BenchmarkEqDec128(b *testing.B) {
   309  	as := make([]int64, 8192)
   310  	bs := make([]int64, 8192)
   311  	cs := make([]bool, 8192)
   312  	for i := 0; i < 8192; i++ {
   313  		as[i] = int64(i)
   314  		bs[i] = 1
   315  	}
   316  
   317  	av := testutil.MakeDecimal128Vector(as, nil, types.T_decimal64.ToType())
   318  	bv := testutil.MakeDecimal128Vector(bs, nil, types.T_decimal64.ToType())
   319  	cv := testutil.MakeBoolVector(cs)
   320  
   321  	for i := 0; i < b.N; i++ {
   322  		if err := Decimal128VecEq(av, bv, cv); err != nil {
   323  			b.Fail()
   324  		}
   325  	}
   326  }