github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/mult/mult_test.go (about)

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