github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/unary/empty_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 TestEmpty(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 []uint8
    35  		isScalar bool
    36  	}{
    37  		{
    38  			name:     "Empty string",
    39  			proc:     procs,
    40  			inputstr: []string{""},
    41  			expected: []uint8{1},
    42  			isScalar: false,
    43  		},
    44  		{
    45  			name:     "Empty scalar string",
    46  			proc:     procs,
    47  			inputstr: []string{""},
    48  			expected: []uint8{1},
    49  			isScalar: true,
    50  		},
    51  		{
    52  			name:     "Non-empty scalar string",
    53  			proc:     procs,
    54  			inputstr: []string{"ab"},
    55  			expected: []uint8{0},
    56  			isScalar: true,
    57  		},
    58  		{
    59  			name:     "String with empty element",
    60  			proc:     procs,
    61  			inputstr: []string{"ab", "cd", "", "ef", " ", "\n"},
    62  			expected: []uint8{0, 0, 1, 0, 0, 0},
    63  			isScalar: false,
    64  		},
    65  		{
    66  			name:     "Non-empty string",
    67  			proc:     procs,
    68  			inputstr: []string{"ab", "cd", " ", "\t", "\n", "\r"},
    69  			expected: []uint8{0, 0, 0, 0, 0, 0},
    70  			isScalar: false,
    71  		},
    72  		{
    73  			name:     "Non-empty string with null list",
    74  			proc:     procs,
    75  			inputstr: []string{"ab", "", " ", "\t", "\n", "\r", ""},
    76  			inputNsp: []uint64{1, 4},
    77  			expected: []uint8{0, 1, 0, 0, 0, 0, 1},
    78  			isScalar: false,
    79  		},
    80  		{
    81  			name:     "Null",
    82  			proc:     procs,
    83  			expected: []uint8{0},
    84  			isScalar: true,
    85  		},
    86  	}
    87  
    88  	for _, c := range cases {
    89  		t.Run(c.name, func(t *testing.T) {
    90  			vecs := makeEmptyTestVectors(c.inputstr, c.inputNsp, c.isScalar)
    91  			result, err := Empty(vecs, c.proc)
    92  			if err != nil {
    93  				t.Fatal(err)
    94  			}
    95  			col := result.Col.([]uint8)
    96  			require.Equal(t, c.expected, col)
    97  			require.Equal(t, c.isScalar, result.IsScalar())
    98  		})
    99  	}
   100  }
   101  
   102  func makeEmptyTestVectors(data []string, nsp []uint64, isScalar bool) []*vector.Vector {
   103  	vec := make([]*vector.Vector, 1)
   104  	if data != nil {
   105  		vec[0] = testutil.MakeCharVector(data, nsp)
   106  		if isScalar {
   107  			vec[0].MakeScalar(1)
   108  		}
   109  	} else {
   110  		vec[0] = testutil.MakeScalarNull(types.T_char, 0)
   111  	}
   112  
   113  	return vec
   114  }