github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/multi/round_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 multi
    16  
    17  import (
    18  	"log"
    19  	"math"
    20  	"testing"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    23  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    24  	"github.com/matrixorigin/matrixone/pkg/testutil"
    25  	"github.com/smartystreets/goconvey/convey"
    26  )
    27  
    28  func Test_RoundUint64(t *testing.T) {
    29  	convey.Convey("Test round for uint64 succ", t, func() {
    30  		var uint64VecBase = []uint64{1, 4, 8, 16, 32, math.MaxUint64, 0}
    31  		var nsp1 = []uint64{6}
    32  		var origVecs = make([]*vector.Vector, 1)
    33  		var proc = testutil.NewProc()
    34  		origVecs[0] = testutil.MakeUint64Vector(uint64VecBase, nsp1)
    35  		vec, err := RoundUint64(origVecs, proc)
    36  		if err != nil {
    37  			log.Fatal(err)
    38  		}
    39  		data, ok := vec.Col.([]uint64)
    40  		if !ok {
    41  			log.Fatal(moerr.NewInternalError(proc.Ctx, "the AbsUint64 function return value type is not []uint64"))
    42  		}
    43  		compVec := []uint64{1, 4, 8, 16, 32, math.MaxUint64, 0}
    44  		compNsp := []int64{6}
    45  
    46  		for i := 0; i < len(compVec); i++ {
    47  			convey.So(data[i], convey.ShouldEqual, compVec[i])
    48  		}
    49  		j := 0
    50  		for i := 0; i < len(compVec); i++ {
    51  			if j < len(compNsp) {
    52  				if compNsp[j] == int64(i) {
    53  					convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeTrue)
    54  					j++
    55  				} else {
    56  					convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeFalse)
    57  				}
    58  			} else {
    59  				convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeFalse)
    60  			}
    61  		}
    62  
    63  	})
    64  }
    65  
    66  func Test_RoundInt64(t *testing.T) {
    67  	convey.Convey("Test round for int64 succ", t, func() {
    68  		var int64VecBase = []int64{math.MinInt64 + 1, math.MinInt64 + 2, -100, -1, 0, 1, 4, 8, 16, 32, 64, math.MaxInt64, 0}
    69  		var nsp1 = []uint64{12}
    70  		var origVecs = make([]*vector.Vector, 1)
    71  		var proc = testutil.NewProc()
    72  		origVecs[0] = testutil.MakeInt64Vector(int64VecBase, nsp1)
    73  		vec, err := RoundInt64(origVecs, proc)
    74  		if err != nil {
    75  			log.Fatal(err)
    76  		}
    77  		data, ok := vec.Col.([]int64)
    78  		if !ok {
    79  			log.Fatal(moerr.NewInternalError(proc.Ctx, "the AbsUint64 function return value type is not []int64"))
    80  		}
    81  		compVec := []int64{math.MinInt64 + 1, math.MinInt64 + 2, -100, -1, 0, 1, 4, 8, 16, 32, 64, math.MaxInt64, 0}
    82  		compNsp := []int64{12}
    83  
    84  		for i := 0; i < len(compVec); i++ {
    85  			convey.So(data[i], convey.ShouldEqual, compVec[i])
    86  		}
    87  		j := 0
    88  		for i := 0; i < len(compVec); i++ {
    89  			if j < len(compNsp) {
    90  				if compNsp[j] == int64(i) {
    91  					convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeTrue)
    92  					j++
    93  				} else {
    94  					convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeFalse)
    95  				}
    96  			} else {
    97  				convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeFalse)
    98  			}
    99  		}
   100  	})
   101  }
   102  
   103  func Test_RoundFloat64(t *testing.T) {
   104  	convey.Convey("Test round for float64 succ", t, func() {
   105  		var float64VecBase = []float64{math.SmallestNonzeroFloat64, -1.2, -2.3, math.MinInt64 + 1, math.MinInt64 + 2, -100.2, -1.3, 0.9, 0,
   106  			1.5, 4.4, 8.5, 16.32, 32.345, 64.09, math.MaxInt64, math.MaxFloat64, 0}
   107  		var nsp1 = []uint64{uint64(len(float64VecBase) - 1)}
   108  		var origVecs = make([]*vector.Vector, 1)
   109  		var proc = testutil.NewProc()
   110  		origVecs[0] = testutil.MakeFloat64Vector(float64VecBase, nsp1)
   111  		vec, err := RoundFloat64(origVecs, proc)
   112  		if err != nil {
   113  			log.Fatal(err)
   114  		}
   115  		data, ok := vec.Col.([]float64)
   116  		if !ok {
   117  			log.Fatal(moerr.NewInternalError(proc.Ctx, "the AbsUint64 function return value type is not []int64"))
   118  		}
   119  		compVec := []float64{0, -1, -2, math.MinInt64 + 1, math.MinInt64 + 2, -100, -1, 1,
   120  			0, 2, 4, 8, 16, 32, 64, math.MaxInt64, math.MaxFloat64, 0}
   121  		compNsp := []int64{int64(len(float64VecBase) - 1)}
   122  
   123  		for i := 0; i < len(compVec); i++ {
   124  			convey.So(data[i], convey.ShouldEqual, compVec[i])
   125  		}
   126  		j := 0
   127  		for i := 0; i < len(compVec); i++ {
   128  			if j < len(compNsp) {
   129  				if compNsp[j] == int64(i) {
   130  					convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeTrue)
   131  					j++
   132  				} else {
   133  					convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeFalse)
   134  				}
   135  			} else {
   136  				convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeFalse)
   137  			}
   138  		}
   139  	})
   140  }
   141  
   142  func Test_RoundFloat64AndInt64(t *testing.T) {
   143  	convey.Convey("Test floor for float64 and int64 succ", t, func() {
   144  		var float64VecBase = []float64{-1.234567, 0, 1.2345678, 0}
   145  		var int64VecBase = []int64{-4, -3, -2, -1, 0, 1, 2, 3, 4, 5}
   146  		var nsp1 = []uint64{3}
   147  		var origVecs = make([]*vector.Vector, 2)
   148  		var proc = testutil.NewProc()
   149  		var compVecVec = [][]float64{{-0, 0, 0, 0}, {-0, 0, 0, 0}, {-0, 0, 0, 0}, {-0, 0, 0, 0}, {-1, 0, 1, 0},
   150  			{-1.2, 0, 1.2, 0}, {-1.23, 0, 1.23, 0}, {-1.235, 0, 1.235, 0}, {-1.2346, 0, 1.2346, 0}, {-1.23457, 0, 1.23457, 0}}
   151  
   152  		origVecs[0] = testutil.MakeFloat64Vector(float64VecBase, nsp1)
   153  		for i := 0; i < len(int64VecBase); i++ {
   154  			origVecs[1] = testutil.MakeScalarInt64(int64VecBase[i], 1)
   155  
   156  			vec, err := RoundFloat64(origVecs, proc)
   157  			if err != nil {
   158  				log.Fatal(err)
   159  			}
   160  			data, ok := vec.Col.([]float64)
   161  			if !ok {
   162  				log.Fatal(moerr.NewInternalError(proc.Ctx, "the AbsUint64 function return value type is not []int64"))
   163  			}
   164  			compVec := compVecVec[i]
   165  			for j := 0; j < len(compVec); j++ {
   166  				convey.So(data[j], convey.ShouldEqual, compVec[j])
   167  			}
   168  		}
   169  	})
   170  }
   171  
   172  func Test_Round2(t *testing.T) {
   173  	convey.Convey("Test_RoundFloat64AndInt64_2", t, func() {
   174  		type kase struct {
   175  			n      float64
   176  			format int64
   177  			want   float64
   178  		}
   179  
   180  		kases := []kase{
   181  			{
   182  				n:      999999999.0,
   183  				format: -9,
   184  				want:   1000000000,
   185  			},
   186  			{
   187  				n:      999999999.0,
   188  				format: -8,
   189  				want:   1000000000,
   190  			},
   191  			{
   192  				n:      999999999999999999.0,
   193  				format: -18,
   194  				want:   1000000000000000000,
   195  			},
   196  			{
   197  				n:      999999999999999999.0,
   198  				format: -10,
   199  				want:   1000000000000000000,
   200  			},
   201  			{
   202  				n:      9999999.99999999999,
   203  				format: 10,
   204  				want:   10000000.0000000000,
   205  			},
   206  		}
   207  		for _, k := range kases {
   208  			srcVector := testutil.MakeScalarFloat64(k.n, 1)
   209  			formatVector := testutil.MakeScalarInt64(k.format, 1)
   210  
   211  			wantVec := testutil.MakeScalarFloat64(k.want, 1)
   212  			proc := testutil.NewProc()
   213  			res, err := RoundFloat64([]*vector.Vector{srcVector, formatVector}, proc)
   214  			convey.So(err, convey.ShouldBeNil)
   215  			compare := testutil.CompareVectors(wantVec, res)
   216  			convey.So(compare, convey.ShouldBeTrue)
   217  
   218  		}
   219  
   220  	})
   221  
   222  }