github.com/dolthub/go-mysql-server@v0.18.0/sql/types/bit_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 types
    16  
    17  import (
    18  	"fmt"
    19  	"reflect"
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  	"github.com/stretchr/testify/require"
    25  
    26  	"github.com/dolthub/go-mysql-server/sql"
    27  )
    28  
    29  func TestBitCompare(t *testing.T) {
    30  	tests := []struct {
    31  		typ         sql.Type
    32  		val1        interface{}
    33  		val2        interface{}
    34  		expectedCmp int
    35  	}{
    36  		{MustCreateBitType(1), nil, 0, 1},
    37  		{MustCreateBitType(1), 0, nil, -1},
    38  		{MustCreateBitType(1), nil, nil, 0},
    39  		{MustCreateBitType(1), 0, 1, -1},
    40  		{MustCreateBitType(10), 0, true, -1},
    41  		{MustCreateBitType(64), false, 1, -1},
    42  		{MustCreateBitType(1), 1, 0, 1},
    43  		{MustCreateBitType(10), true, sql.False, 1},
    44  		{MustCreateBitType(64), 1, false, 1},
    45  		{MustCreateBitType(1), 1, 1, 0},
    46  		{MustCreateBitType(10), true, 1, 0},
    47  		{MustCreateBitType(64), sql.True, true, 0},
    48  		{MustCreateBitType(1), true, true, 0},
    49  		{MustCreateBitType(1), false, false, 0},
    50  		{MustCreateBitType(64), 0x12345de, 0xed54321, -1},
    51  		{MustCreateBitType(64), 0xed54321, 0x12345de, 1},
    52  		{MustCreateBitType(64), 3848, 3848, 0},
    53  	}
    54  
    55  	for _, test := range tests {
    56  		t.Run(fmt.Sprintf("%v %v", test.val1, test.val2), func(t *testing.T) {
    57  			cmp, err := test.typ.Compare(test.val1, test.val2)
    58  			require.NoError(t, err)
    59  			assert.Equal(t, test.expectedCmp, cmp)
    60  		})
    61  	}
    62  }
    63  
    64  func TestBitCreate(t *testing.T) {
    65  	tests := []struct {
    66  		numOfBits    uint8
    67  		expectedType BitType_
    68  		expectedErr  bool
    69  	}{
    70  		{1, BitType_{1}, false},
    71  		{10, BitType_{10}, false},
    72  		{64, BitType_{64}, false},
    73  		{0, BitType_{}, true},
    74  		{65, BitType_{}, true},
    75  	}
    76  
    77  	for _, test := range tests {
    78  		t.Run(fmt.Sprintf("%v %v", test.numOfBits, test.expectedType), func(t *testing.T) {
    79  			typ, err := CreateBitType(test.numOfBits)
    80  			if test.expectedErr {
    81  				assert.Error(t, err)
    82  			} else {
    83  				require.NoError(t, err)
    84  				assert.Equal(t, test.expectedType, typ)
    85  			}
    86  		})
    87  	}
    88  }
    89  
    90  func TestBitConvert(t *testing.T) {
    91  	tests := []struct {
    92  		typ         sql.Type
    93  		val         interface{}
    94  		expectedVal interface{}
    95  		expectedErr bool
    96  	}{
    97  		{MustCreateBitType(1), nil, nil, false},
    98  		{MustCreateBitType(1), true, uint64(1), false},
    99  		{MustCreateBitType(1), int32(0), uint64(0), false},
   100  		{MustCreateBitType(1), uint16(1), uint64(1), false},
   101  		{MustCreateBitType(1), false, uint64(0), false},
   102  		{MustCreateBitType(1), true, uint64(1), false},
   103  		{MustCreateBitType(10), int(33), uint64(33), false},
   104  		{MustCreateBitType(11), int8(34), uint64(34), false},
   105  		{MustCreateBitType(12), int16(35), uint64(35), false},
   106  		{MustCreateBitType(13), uint8(36), uint64(36), false},
   107  		{MustCreateBitType(14), uint32(37), uint64(37), false},
   108  		{MustCreateBitType(15), uint(38), uint64(38), false},
   109  		{MustCreateBitType(64), uint64(18446744073709551615), uint64(18446744073709551615), false},
   110  		{MustCreateBitType(64), float32(893.22356), uint64(893), false},
   111  		{MustCreateBitType(64), float64(79234.356), uint64(79234), false},
   112  		{MustCreateBitType(21), "32", uint64(13106), false},
   113  		{MustCreateBitType(64), "12341234", uint64(3544952155950691124), false},
   114  		{MustCreateBitType(64), -1, uint64(18446744073709551615), false},
   115  		{MustCreateBitType(22), []byte{36, 107}, uint64(9323), false},
   116  		{MustCreateBitType(1), int64(2), nil, true},
   117  		{MustCreateBitType(20), 47202753, nil, true},
   118  		{MustCreateBitType(64), float64(-1.0), nil, true},
   119  		{MustCreateBitType(21), "324", nil, true},
   120  		{MustCreateBitType(60), "12341234", nil, true},
   121  		{MustCreateBitType(64), "123412341", nil, true},
   122  		{MustCreateBitType(22), []byte{36, 107, 48, 38}, nil, true},
   123  		{MustCreateBitType(64), time.Date(2019, 12, 12, 12, 12, 12, 0, time.UTC), nil, true},
   124  	}
   125  
   126  	for _, test := range tests {
   127  		t.Run(fmt.Sprintf("%v %v %v", test.typ, test.val, test.expectedVal), func(t *testing.T) {
   128  			val, _, err := test.typ.Convert(test.val)
   129  			if test.expectedErr {
   130  				assert.Error(t, err)
   131  			} else {
   132  				require.NoError(t, err)
   133  				assert.Equal(t, test.expectedVal, val)
   134  				if val != nil {
   135  					assert.Equal(t, test.typ.ValueType(), reflect.TypeOf(val))
   136  				}
   137  			}
   138  		})
   139  	}
   140  }
   141  
   142  func TestBitString(t *testing.T) {
   143  	tests := []struct {
   144  		typ         sql.Type
   145  		expectedStr string
   146  	}{
   147  		{MustCreateBitType(1), "bit(1)"},
   148  		{MustCreateBitType(10), "bit(10)"},
   149  		{MustCreateBitType(32), "bit(32)"},
   150  		{MustCreateBitType(64), "bit(64)"},
   151  	}
   152  
   153  	for _, test := range tests {
   154  		t.Run(fmt.Sprintf("%v %v", test.typ, test.expectedStr), func(t *testing.T) {
   155  			str := test.typ.String()
   156  			assert.Equal(t, test.expectedStr, str)
   157  		})
   158  	}
   159  }
   160  
   161  func TestBitZero(t *testing.T) {
   162  	_, ok := MustCreateBitType(1).Zero().(uint64)
   163  	require.True(t, ok)
   164  }