github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/add/add_test.go (about)

     1  // Copyright 2021 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 add
    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  	"github.com/matrixorigin/matrixone/pkg/testutil"
    24  )
    25  
    26  func TestI32Of(t *testing.T) {
    27  	as := make([]int32, 2)
    28  	bs := make([]int32, 2)
    29  	for i := 0; i < 2; i++ {
    30  		as[i] = math.MaxInt32
    31  		bs[i] = int32(i)
    32  	}
    33  	cs := make([]int32, 2)
    34  	av := testutil.MakeInt32Vector(as, nil)
    35  	bv := testutil.MakeInt32Vector(bs, nil)
    36  	cv := testutil.MakeInt32Vector(cs, nil)
    37  
    38  	err := NumericAddSigned[int32](av, bv, cv)
    39  	if err == nil {
    40  		t.Fatalf("should have overflowed.")
    41  	}
    42  }
    43  
    44  func TestU32Of(t *testing.T) {
    45  	as := make([]uint32, 2)
    46  	bs := make([]uint32, 2)
    47  	for i := 0; i < 2; i++ {
    48  		as[i] = math.MaxUint32
    49  		bs[i] = uint32(i)
    50  	}
    51  	cs := make([]uint32, 2)
    52  	av := testutil.MakeUint32Vector(as, nil)
    53  	bv := testutil.MakeUint32Vector(bs, nil)
    54  	cv := testutil.MakeUint32Vector(cs, nil)
    55  
    56  	err := NumericAddUnsigned[uint32](av, bv, cv)
    57  	if err == nil {
    58  		t.Fatalf("should have overflowed.")
    59  	}
    60  }
    61  
    62  func TestDec64(t *testing.T) {
    63  	as := make([]int64, 10)
    64  	bs := make([]int64, 10)
    65  	cs := make([]int64, 10)
    66  	for i := 0; i < 10; i++ {
    67  		as[i] = int64(i)
    68  		bs[i] = int64(3 * i)
    69  	}
    70  
    71  	av := testutil.MakeDecimal64Vector(as, nil, types.T_decimal64.ToType())
    72  	bv := testutil.MakeDecimal64Vector(bs, nil, types.T_decimal64.ToType())
    73  	cv := testutil.MakeDecimal64Vector(cs, nil, types.T_decimal64.ToType())
    74  
    75  	err := Decimal64VecAdd(av, bv, cv)
    76  	if err != nil {
    77  		t.Fatalf("decimal64 add failed")
    78  	}
    79  
    80  	res := vector.MustTCols[types.Decimal64](cv)
    81  	for i := 0; i < 10; i++ {
    82  		d, _ := types.Decimal64_FromInt64(as[i]+bs[i], 64, 0)
    83  		if !res[i].Eq(d) {
    84  			t.Fatalf("decimal64 add wrong result")
    85  		}
    86  	}
    87  }
    88  
    89  func TestDec128(t *testing.T) {
    90  	as := make([]int64, 10)
    91  	bs := make([]int64, 10)
    92  	cs := make([]int64, 10)
    93  	for i := 0; i < 10; i++ {
    94  		as[i] = int64(i)
    95  		bs[i] = int64(3 * i)
    96  	}
    97  
    98  	av := testutil.MakeDecimal128Vector(as, nil, types.T_decimal128.ToType())
    99  	bv := testutil.MakeDecimal128Vector(bs, nil, types.T_decimal128.ToType())
   100  	cv := testutil.MakeDecimal128Vector(cs, nil, types.T_decimal128.ToType())
   101  
   102  	err := Decimal128VecAdd(av, bv, cv)
   103  	if err != nil {
   104  		t.Fatalf("decimal128 add failed")
   105  	}
   106  
   107  	res := vector.MustTCols[types.Decimal128](cv)
   108  	for i := 0; i < 10; i++ {
   109  		d, _ := types.Decimal128_FromInt64(as[i]+bs[i], 64, 0)
   110  		if !res[i].Eq(d) {
   111  			t.Fatalf("decimal128 add wrong result")
   112  		}
   113  	}
   114  }
   115  
   116  func TestDec64OfOppNumber(t *testing.T) {
   117  	as := make([]int64, 10)
   118  	bs := make([]int64, 10)
   119  	cs := make([]int64, 10)
   120  	for i := 0; i < 10; i++ {
   121  		as[i] = int64(-i)
   122  		bs[i] = int64(i)
   123  	}
   124  
   125  	av := testutil.MakeDecimal64Vector(as, nil, types.T_decimal64.ToType())
   126  	bv := testutil.MakeDecimal64Vector(bs, nil, types.T_decimal64.ToType())
   127  	cv := testutil.MakeDecimal64Vector(cs, nil, types.T_decimal64.ToType())
   128  
   129  	err := Decimal64VecAdd(av, bv, cv)
   130  	if err != nil {
   131  		t.Fatalf("decimal64 add failed")
   132  	}
   133  
   134  	res := vector.MustTCols[types.Decimal64](cv)
   135  	for i := 0; i < 10; i++ {
   136  		d, _ := types.Decimal64_FromInt64(as[i]+bs[i], 64, 0)
   137  		if !res[i].Eq(d) {
   138  			t.Fatalf("decimal64 add wrong result")
   139  		}
   140  	}
   141  }
   142  
   143  func TestDec128OfOppNumber(t *testing.T) {
   144  	as := make([]int64, 10)
   145  	bs := make([]int64, 10)
   146  	cs := make([]int64, 10)
   147  	for i := 0; i < 10; i++ {
   148  		as[i] = int64(i)
   149  		bs[i] = int64(-i)
   150  	}
   151  
   152  	av := testutil.MakeDecimal128Vector(as, nil, types.T_decimal128.ToType())
   153  	bv := testutil.MakeDecimal128Vector(bs, nil, types.T_decimal128.ToType())
   154  	cv := testutil.MakeDecimal128Vector(cs, nil, types.T_decimal128.ToType())
   155  
   156  	err := Decimal128VecAdd(av, bv, cv)
   157  	if err != nil {
   158  		t.Fatalf("decimal128 add failed")
   159  	}
   160  
   161  	res := vector.MustTCols[types.Decimal128](cv)
   162  	for i := 0; i < 10; i++ {
   163  		d, _ := types.Decimal128_FromInt64(as[i]+bs[i], 64, 0)
   164  		if !res[i].Eq(d) {
   165  			t.Fatalf("decimal128 add wrong result")
   166  		}
   167  	}
   168  }
   169  
   170  func BenchmarkAddI32(b *testing.B) {
   171  	as := make([]int32, 8192)
   172  	bs := make([]int32, 8192)
   173  	for i := 0; i < 8192; i++ {
   174  		as[i] = int32(i)
   175  		bs[i] = 1
   176  	}
   177  
   178  	cs := make([]int32, 8192)
   179  
   180  	av := testutil.MakeInt32Vector(as, nil)
   181  	bv := testutil.MakeInt32Vector(bs, nil)
   182  	cv := testutil.MakeInt32Vector(cs, nil)
   183  
   184  	for i := 0; i < b.N; i++ {
   185  		if err := goNumericAddSigned[int32](av, bv, cv); err != nil {
   186  			b.Fail()
   187  		}
   188  	}
   189  }
   190  
   191  func BenchmarkAddI32_C(b *testing.B) {
   192  	as := make([]int32, 8192)
   193  	bs := make([]int32, 8192)
   194  	for i := 0; i < 8192; i++ {
   195  		as[i] = int32(i)
   196  		bs[i] = 1
   197  	}
   198  
   199  	cs := make([]int32, 8192)
   200  
   201  	av := testutil.MakeInt32Vector(as, nil)
   202  	bv := testutil.MakeInt32Vector(bs, nil)
   203  	cv := testutil.MakeInt32Vector(cs, nil)
   204  
   205  	for i := 0; i < b.N; i++ {
   206  		if err := cNumericAddSigned[int32](av, bv, cv); err != nil {
   207  			b.Fail()
   208  		}
   209  	}
   210  }
   211  
   212  func BenchmarkAddUI32(b *testing.B) {
   213  	as := make([]uint32, 8192)
   214  	bs := make([]uint32, 8192)
   215  	for i := 0; i < 8192; i++ {
   216  		as[i] = uint32(i)
   217  		bs[i] = 1
   218  	}
   219  
   220  	cs := make([]uint32, 8192)
   221  
   222  	av := testutil.MakeUint32Vector(as, nil)
   223  	bv := testutil.MakeUint32Vector(bs, nil)
   224  	cv := testutil.MakeUint32Vector(cs, nil)
   225  
   226  	for i := 0; i < b.N; i++ {
   227  		if err := goNumericAddUnsigned[uint32](av, bv, cv); err != nil {
   228  			b.Fail()
   229  		}
   230  	}
   231  }
   232  
   233  func BenchmarkAddUI32_C(b *testing.B) {
   234  	as := make([]uint32, 8192)
   235  	bs := make([]uint32, 8192)
   236  	for i := 0; i < 8192; i++ {
   237  		as[i] = uint32(i)
   238  		bs[i] = 1
   239  	}
   240  
   241  	cs := make([]uint32, 8192)
   242  
   243  	av := testutil.MakeUint32Vector(as, nil)
   244  	bv := testutil.MakeUint32Vector(bs, nil)
   245  	cv := testutil.MakeUint32Vector(cs, nil)
   246  
   247  	for i := 0; i < b.N; i++ {
   248  		if err := cNumericAddUnsigned[uint32](av, bv, cv); err != nil {
   249  			b.Fail()
   250  		}
   251  	}
   252  }
   253  
   254  func BenchmarkAddF64(b *testing.B) {
   255  	as := make([]float64, 8192)
   256  	bs := make([]float64, 8192)
   257  	for i := 0; i < 8192; i++ {
   258  		as[i] = float64(i)
   259  		bs[i] = 1
   260  	}
   261  
   262  	cs := make([]float64, 8192)
   263  
   264  	av := testutil.MakeFloat64Vector(as, nil)
   265  	bv := testutil.MakeFloat64Vector(bs, nil)
   266  	cv := testutil.MakeFloat64Vector(cs, nil)
   267  
   268  	for i := 0; i < b.N; i++ {
   269  		if err := goNumericAddFloat[float64](av, bv, cv); err != nil {
   270  			b.Fail()
   271  		}
   272  	}
   273  }
   274  
   275  func BenchmarkAddF64_C(b *testing.B) {
   276  	as := make([]float64, 8192)
   277  	bs := make([]float64, 8192)
   278  	for i := 0; i < 8192; i++ {
   279  		as[i] = float64(i)
   280  		bs[i] = 1
   281  	}
   282  
   283  	cs := make([]float64, 8192)
   284  
   285  	av := testutil.MakeFloat64Vector(as, nil)
   286  	bv := testutil.MakeFloat64Vector(bs, nil)
   287  	cv := testutil.MakeFloat64Vector(cs, nil)
   288  
   289  	for i := 0; i < b.N; i++ {
   290  		if err := cNumericAddFloat[float64](av, bv, cv); err != nil {
   291  			b.Fail()
   292  		}
   293  	}
   294  }
   295  
   296  func BenchmarkAddDec64(b *testing.B) {
   297  	as := make([]int64, 8192)
   298  	bs := make([]int64, 8192)
   299  	cs := make([]int64, 8192)
   300  	for i := 0; i < 8192; i++ {
   301  		as[i] = int64(i)
   302  		bs[i] = 1
   303  	}
   304  
   305  	av := testutil.MakeDecimal64Vector(as, nil, types.T_decimal64.ToType())
   306  	bv := testutil.MakeDecimal64Vector(bs, nil, types.T_decimal64.ToType())
   307  	cv := testutil.MakeDecimal64Vector(cs, nil, types.T_decimal64.ToType())
   308  
   309  	for i := 0; i < b.N; i++ {
   310  		if err := Decimal64VecAdd(av, bv, cv); err != nil {
   311  			b.Fail()
   312  		}
   313  	}
   314  }
   315  
   316  func BenchmarkAddDec128(b *testing.B) {
   317  	as := make([]int64, 8192)
   318  	bs := make([]int64, 8192)
   319  	cs := make([]int64, 8192)
   320  	for i := 0; i < 8192; i++ {
   321  		as[i] = int64(i)
   322  		bs[i] = 1
   323  	}
   324  
   325  	av := testutil.MakeDecimal128Vector(as, nil, types.T_decimal128.ToType())
   326  	bv := testutil.MakeDecimal128Vector(bs, nil, types.T_decimal128.ToType())
   327  	cv := testutil.MakeDecimal128Vector(cs, nil, types.T_decimal128.ToType())
   328  
   329  	for i := 0; i < b.N; i++ {
   330  		if err := Decimal128VecAdd(av, bv, cv); err != nil {
   331  			b.Fail()
   332  		}
   333  	}
   334  }