github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/conv_test.go (about)

     1  // Copyright 2022 Dolthub, Inc.
     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 function
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/require"
    21  
    22  	"github.com/dolthub/go-mysql-server/sql"
    23  	"github.com/dolthub/go-mysql-server/sql/expression"
    24  	"github.com/dolthub/go-mysql-server/sql/types"
    25  )
    26  
    27  func TestConv(t *testing.T) {
    28  	testCases := []struct {
    29  		name     string
    30  		nType    sql.Type
    31  		row      sql.Row
    32  		expected interface{}
    33  	}{
    34  		// NULL inputs
    35  		{"n is nil", types.Int32, sql.NewRow(nil, 16, 2), nil},
    36  		{"fromBase is nil", types.LongText, sql.NewRow('a', nil, 2), nil},
    37  		{"toBase is nil", types.LongText, sql.NewRow('a', 16, nil), nil},
    38  
    39  		// invalid inputs
    40  		{"invalid N", types.LongText, sql.NewRow("r", 16, 2), "0"},
    41  		{"invalid N for 10 fromBase", types.LongText, sql.NewRow("B11", 10, 10), "0"},
    42  		{"invalid negative N for 10 fromBase", types.LongText, sql.NewRow("-C17", 10, 10), "0"},
    43  		{"invalid N for 16 fromBase", types.LongText, sql.NewRow("LAX", 16, 10), "0"},
    44  		{"bigger N than max uint64 from base 2 to base 16", types.LongText, sql.NewRow("184467440737095485216", 10, -16), "-1"},
    45  		{"bigger N than max uint64 from base 2 to base -16", types.LongText, sql.NewRow("184467440737095485216", 10, 16), "FFFFFFFFFFFFFFFF"},
    46  		{"bigger N than max uint64 from base 2 to base 10", types.LongText, sql.NewRow("111111111111111111111111111111111111111111111111111111111111111111", 2, 10), "18446744073709551615"},
    47  		{"bigger N than max uint64 from base 2 to base -10", types.LongText, sql.NewRow("111111111111111111111111111111111111111111111111111111111111111111", 2, -10), "-1"},
    48  		{"invalid 37 fromBase", types.LongText, sql.NewRow(2, 37, 2), nil},
    49  		{"invalid -1 fromBase", types.LongText, sql.NewRow(2, -1, 2), nil},
    50  		{"invalid 0 fromBase", types.LongText, sql.NewRow(2, 0, 2), nil},
    51  		{"invalid 1 fromBase", types.LongText, sql.NewRow(2, 1, 2), nil},
    52  		{"invalid 37 toBase", types.LongText, sql.NewRow(2, 16, 37), nil},
    53  		{"invalid -1 toBase", types.LongText, sql.NewRow(2, 10, -1), nil},
    54  		{"invalid 0 toBase", types.LongText, sql.NewRow(2, 10, 0), nil},
    55  		{"invalid 1 toBase", types.LongText, sql.NewRow(2, 10, 1), nil},
    56  
    57  		// valid inputs
    58  		{"truncate the first convertable subpart for N for 10 fromBase", types.LongText, sql.NewRow("11B", 10, 10), "11"},
    59  		{"truncate the first convertable subpart for N for 16 fromBase", types.LongText, sql.NewRow("BX", 16, 10), "11"},
    60  		{"truncate the first convertable subpart for longer N for 16 fromBase", types.LongText, sql.NewRow("ABCF9XCD", 16, 10), "703737"},
    61  		{"truncate the first convertable subpart for longer N for 2 fromBase", types.LongText, sql.NewRow("18", 2, -16), "1"},
    62  		{"base 16 to base 2", types.LongText, sql.NewRow("a", 16, 2), "1010"},
    63  		{"base 16 to base 2 with positive sign", types.LongText, sql.NewRow("+a", 16, 2), "1010"},
    64  		{"base 2 to base 16", types.LongText, sql.NewRow(1010, 2, 16), "A"},
    65  		{"base 18 to base 8", types.LongText, sql.NewRow("6E", 18, 8), "172"},
    66  		{"base 8 to base 18", types.LongText, sql.NewRow("172", 8, 18), "6E"},
    67  		{"base 10 to base -18", types.LongText, sql.NewRow("-17", 10, -18), "-H"},
    68  		{"base -18 to base -10", types.LongText, sql.NewRow("-H", -18, -10), "-17"},
    69  		{"base -18 to base 10", types.LongText, sql.NewRow("-H", -18, 10), "18446744073709551599"},
    70  		{"base 10 to base 16", types.LongText, sql.NewRow(-17, 10, 16), "FFFFFFFFFFFFFFEF"},
    71  		{"base -10 to base 16", types.LongText, sql.NewRow(-17, -10, 16), "FFFFFFFFFFFFFFEF"},
    72  		{"base 10 to base -16", types.LongText, sql.NewRow(-17, 10, -16), "-11"},
    73  		{"base -10 to base -16", types.LongText, sql.NewRow(-17, -10, -16), "-11"},
    74  		{"negative N for base 16 to base -10", types.LongText, sql.NewRow("-C17", 16, -10), "-3095"},
    75  		{"negative N for base 16 to base 10", types.LongText, sql.NewRow("-C17", 16, 10), "18446744073709548521"},
    76  		{"big N for base 10 to base -16", types.LongText, sql.NewRow("18446744073709548521", 10, -16), "-C17"},
    77  		{"big N for base 10 to base 16", types.LongText, sql.NewRow("18446744073709548521", 10, 16), "FFFFFFFFFFFFF3E9"},
    78  		{"max N for base 10 to base -16", types.LongText, sql.NewRow("18446744073709551615", 10, -16), "-1"},
    79  		{"big N for base 10 to base -16", types.LongText, sql.NewRow("18446744073709551614", 10, -16), "-2"},
    80  		{"n as hex", types.LongText, sql.NewRow(0x0a, 10, 10), "10"},
    81  	}
    82  
    83  	for _, tt := range testCases {
    84  		f := NewConv(
    85  			expression.NewGetField(0, tt.nType, "N", false),
    86  			expression.NewGetField(1, types.Int64, "FromBase", false),
    87  			expression.NewGetField(2, types.Int64, "ToBase", false),
    88  		)
    89  
    90  		t.Run(tt.name, func(t *testing.T) {
    91  			require := require.New(t)
    92  
    93  			result, _ := f.Eval(sql.NewEmptyContext(), tt.row)
    94  			require.Equal(tt.expected, result)
    95  		})
    96  	}
    97  }