github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/binary/power_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 binary
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    21  	"github.com/matrixorigin/matrixone/pkg/testutil"
    22  
    23  	"github.com/matrixorigin/matrixone/pkg/container/types"
    24  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    26  	"github.com/stretchr/testify/require"
    27  	"golang.org/x/exp/constraints"
    28  )
    29  
    30  func TestPower(t *testing.T) {
    31  	powerFloat64[float64](t, types.T_float64, 1, 2, 1)
    32  	powerFloat64[float64](t, types.T_float64, 2, 2, 4)
    33  	powerFloat64[float64](t, types.T_float64, 3, 2, 9)
    34  	powerFloat64[float64](t, types.T_float64, 3, 3, 27)
    35  	powerFloat64[float64](t, types.T_float64, 4, 2, 16)
    36  	powerFloat64[float64](t, types.T_float64, 4, 3, 64)
    37  	powerFloat64[float64](t, types.T_float64, 4, 0.5, 2)
    38  	powerFloat64[float64](t, types.T_float64, 5, 2, 25)
    39  	powerFloat64[float64](t, types.T_float64, 6, 2, 36)
    40  	powerFloat64[float64](t, types.T_float64, 7, 2, 49)
    41  	powerFloat64[float64](t, types.T_float64, 8, 2, 64)
    42  	powerFloat64[float64](t, types.T_float64, 0.5, 2, 0.25)
    43  	powerFloat64[float64](t, types.T_float64, 1.5, 2, 2.25)
    44  	powerFloat64[float64](t, types.T_float64, 2.5, 2, 6.25)
    45  	powerFloat64[float64](t, types.T_float64, 3.5, 2, 12.25)
    46  	powerFloat64[float64](t, types.T_float64, 4.5, 2, 20.25)
    47  	powerFloat64[float64](t, types.T_float64, 5.5, 2, 30.25)
    48  }
    49  
    50  func powerFloat64[T constraints.Integer | constraints.Float](t *testing.T, typ types.T, src T, src2 T, res float64) {
    51  	procs := testutil.NewProc()
    52  	cases := []struct {
    53  		name       string
    54  		vecs       []*vector.Vector
    55  		proc       *process.Process
    56  		wantBytes  interface{}
    57  		wantScalar bool
    58  	}{
    59  		{
    60  			name:       "TEST01",
    61  			vecs:       makePowerVectors(src, src2, true, typ),
    62  			proc:       procs,
    63  			wantBytes:  []float64{res},
    64  			wantScalar: true,
    65  		},
    66  	}
    67  
    68  	for _, c := range cases {
    69  		t.Run(c.name, func(t *testing.T) {
    70  			plus, err := Power(c.vecs, c.proc)
    71  			if err != nil {
    72  				t.Fatal(err)
    73  			}
    74  			require.Equal(t, c.wantBytes, plus.Col)
    75  			require.Equal(t, c.wantScalar, plus.IsScalar())
    76  		})
    77  	}
    78  }
    79  
    80  // Construct the vector parameter of the plus operator
    81  func makePowerVectors[T constraints.Integer | constraints.Float](src T, src2 T, srcScalar bool, t types.T) []*vector.Vector {
    82  	mp := mpool.MustNewZero()
    83  	vectors := make([]*vector.Vector, 2)
    84  	vectors[0] = vector.NewConstFixed(t.ToType(), 1, src, mp)
    85  	vectors[1] = vector.NewConstFixed(t.ToType(), 1, src2, mp)
    86  	return vectors
    87  }