github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/unary/bit_length_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 unary
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/container/types"
    21  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    22  	"github.com/matrixorigin/matrixone/pkg/testutil"
    23  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func TestBitLength(t *testing.T) {
    28  	procs := testutil.NewProc()
    29  	cases := []struct {
    30  		name     string
    31  		proc     *process.Process
    32  		inputstr []string
    33  		inputNsp []uint64
    34  		expected []int64
    35  		isScalar bool
    36  	}{
    37  		{
    38  			name:     "Empty string",
    39  			proc:     procs,
    40  			inputstr: []string{""},
    41  			expected: []int64{0},
    42  			isScalar: false,
    43  		},
    44  		{
    45  			name:     "Scalar empty string",
    46  			proc:     procs,
    47  			inputstr: []string{""},
    48  			expected: []int64{0},
    49  			isScalar: true,
    50  		},
    51  		{
    52  			name:     "Non-empty scalar string",
    53  			proc:     procs,
    54  			inputstr: []string{"matrix", "origin", "=", "mo", " ", "\t", ""},
    55  			expected: []int64{48, 48, 8, 16, 8, 8, 0},
    56  			isScalar: false,
    57  		},
    58  		{
    59  			name:     "Null",
    60  			proc:     procs,
    61  			expected: []int64{0},
    62  			isScalar: true,
    63  		},
    64  	}
    65  
    66  	for _, c := range cases {
    67  		t.Run(c.name, func(t *testing.T) {
    68  			vecs := makeBitLenTestVectors(c.inputstr, c.inputNsp, c.isScalar)
    69  			result, err := BitLengthFunc(vecs, c.proc)
    70  			if err != nil {
    71  				t.Fatal(err)
    72  			}
    73  			col := result.Col.([]int64)
    74  			require.Equal(t, c.expected, col)
    75  			require.Equal(t, c.isScalar, result.IsScalar())
    76  		})
    77  	}
    78  }
    79  
    80  func makeBitLenTestVectors(data []string, nsp []uint64, isScalar bool) []*vector.Vector {
    81  	vec := make([]*vector.Vector, 1)
    82  	if data != nil {
    83  		if isScalar {
    84  			vec[0] = vector.NewConstString(types.T_varchar.ToType(), 1, data[0], testutil.TestUtilMp)
    85  		} else {
    86  			vec[0] = testutil.MakeCharVector(data, nsp)
    87  		}
    88  	} else {
    89  		vec[0] = testutil.MakeScalarNull(types.T_char, 0)
    90  	}
    91  
    92  	return vec
    93  }