github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/multi/floor_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_FloorUint64(t *testing.T) {
    29  	convey.Convey("Test floor 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 := FloorUInt64(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_FloorInt64(t *testing.T) {
    67  	convey.Convey("Test floor 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 := FloorInt64(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_FloorFloat64(t *testing.T) {
   104  	convey.Convey("Test floor 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 := FloorFloat64(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, -2, -3, math.MinInt64 + 1, math.MinInt64 + 2, -101, -2, 0,
   120  			0, 1, 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  }