github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/neg/neg_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 neg
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/container/types"
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func makeIbuffer(l int) []int64 {
    26  	buf := make([]int64, l)
    27  	for i := range buf {
    28  		buf[i] = int64(i)
    29  	}
    30  	return buf
    31  }
    32  
    33  func makeFbuffer(l int) []float32 {
    34  	buf := make([]float32, l)
    35  	for i := range buf {
    36  		buf[i] = float32(i)
    37  	}
    38  	return buf
    39  }
    40  
    41  func TestF64Sum(t *testing.T) {
    42  	xs := makeFbuffer(100)
    43  	rs := make([]float32, 100)
    44  	fmt.Printf("float neg: %v\n", Float32Neg(xs, rs))
    45  	fmt.Printf("pure float neg: %v\n", NumericNeg(xs, rs))
    46  }
    47  
    48  func TestI64Sum(t *testing.T) {
    49  	xs := makeIbuffer(100)
    50  	rs := make([]int64, 100)
    51  	fmt.Printf("int neg: %v\n", Int64Neg(xs, rs))
    52  	fmt.Printf("pure int neg: %v\n", NumericNeg(xs, rs))
    53  }
    54  
    55  func TestDecimal64Neg(t *testing.T) {
    56  	d1, _ := types.InitDecimal64(123, 64, 0)
    57  	d2, _ := types.InitDecimal64(234, 64, 0)
    58  	d3, _ := types.InitDecimal64(345, 64, 0)
    59  	d4, _ := types.InitDecimal64(-234, 64, 0)
    60  	d5, _ := types.InitDecimal64(-123, 64, 0)
    61  	d6, _ := types.InitDecimal64(-234, 64, 0)
    62  	d7, _ := types.InitDecimal64(-345, 64, 0)
    63  	d8, _ := types.InitDecimal64(234, 64, 0)
    64  
    65  	xs := []types.Decimal64{d1, d2, d3, types.Decimal64_Zero, d4}
    66  	rs := make([]types.Decimal64, len(xs))
    67  	rs = Decimal64Neg(xs, rs)
    68  	expectedResult := []types.Decimal64{d5, d6, d7, types.Decimal64_Zero, d8}
    69  	for i, r := range rs {
    70  		require.True(t, r.Eq(expectedResult[i]))
    71  	}
    72  }
    73  
    74  func TestDecimal128Neg(t *testing.T) {
    75  	xs := make([]types.Decimal128, 6)
    76  	xs[0], _ = types.ParseStringToDecimal128("123456.789", 20, 5, false)
    77  	xs[1], _ = types.ParseStringToDecimal128("120000.789", 20, 5, false)
    78  	xs[2], _ = types.ParseStringToDecimal128("-123456.789", 20, 5, false)
    79  	xs[3], _ = types.ParseStringToDecimal128("0", 20, 5, false)
    80  	xs[4], _ = types.ParseStringToDecimal128("-123", 20, 5, false)
    81  	xs[5], _ = types.ParseStringToDecimal128("-123.456789", 20, 5, false)
    82  	rs := make([]types.Decimal128, 6)
    83  	rs = Decimal128Neg(xs, rs)
    84  
    85  	require.Equal(t, "-123456.789", string(rs[0].ToString()))
    86  
    87  	require.Equal(t, "-123456.79", string(rs[0].ToStringWithScale(2)))
    88  
    89  	require.Equal(t, "-120000.79", string(rs[1].ToStringWithScale(2)))
    90  
    91  	require.Equal(t, "123456.79", string(rs[2].ToStringWithScale(2)))
    92  
    93  	require.Equal(t, "0", string(rs[3].ToStringWithScale(0)))
    94  
    95  	require.Equal(t, "123", string(rs[4].ToStringWithScale(0)))
    96  
    97  	require.Equal(t, "123.45679", string(rs[5].ToStringWithScale(5)))
    98  
    99  	require.Equal(t, "123", string(rs[5].ToStringWithScale(0)))
   100  }