github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/unary/hex_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/smartystreets/goconvey/convey"
    25  )
    26  
    27  func TestHex(t *testing.T) {
    28  	procs := testutil.NewProc()
    29  	cases := []struct {
    30  		name     string
    31  		proc     *process.Process
    32  		inputstr []string
    33  		expected []string
    34  		isScalar bool
    35  	}{
    36  		{
    37  			name:     "String test",
    38  			proc:     procs,
    39  			inputstr: []string{"a"},
    40  			expected: []string{"61"},
    41  			isScalar: false,
    42  		},
    43  		{
    44  			name:     "Scalar empty string",
    45  			proc:     procs,
    46  			inputstr: []string{""},
    47  			expected: []string{""},
    48  			isScalar: true,
    49  		},
    50  		{
    51  			name:     "Number test",
    52  			proc:     procs,
    53  			inputstr: []string{"255"},
    54  			expected: []string{"323535"},
    55  			isScalar: false,
    56  		},
    57  		{
    58  			name:     "multi row test",
    59  			proc:     procs,
    60  			inputstr: []string{"Hello", "Gopher!"},
    61  			expected: []string{"48656c6c6f", "476f7068657221"},
    62  			isScalar: false,
    63  		},
    64  		{
    65  			name:     "Null",
    66  			proc:     procs,
    67  			expected: []string{""},
    68  			isScalar: true,
    69  		},
    70  	}
    71  	for _, c := range cases {
    72  		convey.Convey(c.name, t, func() {
    73  			var inVector *vector.Vector
    74  			if c.inputstr != nil {
    75  				if c.isScalar {
    76  					inVector = vector.NewConstString(types.T_varchar.ToType(), 1, c.inputstr[0], testutil.TestUtilMp)
    77  				} else {
    78  					inVector = testutil.MakeCharVector(c.inputstr, nil)
    79  				}
    80  			} else {
    81  				inVector = testutil.MakeScalarNull(types.T_char, 0)
    82  			}
    83  			result, err := HexString([]*vector.Vector{inVector}, c.proc)
    84  			convey.So(err, convey.ShouldBeNil)
    85  			convey.So(vector.GetStrVectorValues(result), convey.ShouldResemble, c.expected)
    86  			convey.So(result.IsScalar(), convey.ShouldEqual, c.isScalar)
    87  		})
    88  	}
    89  
    90  	procs = testutil.NewProc()
    91  	cases2 := []struct {
    92  		name     string
    93  		proc     *process.Process
    94  		inputnum []int64
    95  		expected []string
    96  		isScalar bool
    97  	}{
    98  		{
    99  			name:     "Non-empty scalar int",
   100  			proc:     procs,
   101  			inputnum: []int64{255},
   102  			expected: []string{"FF"},
   103  			isScalar: true,
   104  		}, {
   105  			name:     "Number test",
   106  			proc:     procs,
   107  			inputnum: []int64{255},
   108  			expected: []string{"FF"},
   109  			isScalar: false,
   110  		}, {
   111  			name:     "Number test",
   112  			proc:     procs,
   113  			inputnum: []int64{231323423423421},
   114  			expected: []string{"D2632E7B3BBD"},
   115  			isScalar: false,
   116  		}, {
   117  			name:     "Number test",
   118  			proc:     procs,
   119  			inputnum: []int64{123, 234, 345},
   120  			expected: []string{"7B", "EA", "159"},
   121  			isScalar: false,
   122  		}, {
   123  			name:     "Null",
   124  			proc:     procs,
   125  			expected: []string{""},
   126  			isScalar: true,
   127  		},
   128  	}
   129  	for _, c := range cases2 {
   130  		convey.Convey(c.name, t, func() {
   131  			var inVector *vector.Vector
   132  			if c.inputnum != nil {
   133  				if c.isScalar {
   134  					inVector = vector.NewConstFixed(types.T_int64.ToType(), 1, c.inputnum[0], testutil.TestUtilMp)
   135  				} else {
   136  					inVector = testutil.MakeInt64Vector(c.inputnum, nil)
   137  				}
   138  			} else {
   139  				inVector = testutil.MakeScalarNull(types.T_int64, 0)
   140  			}
   141  			result, err := HexInt64([]*vector.Vector{inVector}, c.proc)
   142  			convey.So(err, convey.ShouldBeNil)
   143  			convey.So(vector.GetStrVectorValues(result), convey.ShouldResemble, c.expected)
   144  			convey.So(result.IsScalar(), convey.ShouldEqual, c.isScalar)
   145  		})
   146  	}
   147  
   148  }