github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/util/eval_expr_util_test.go (about) 1 // Copyright 2021 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 util 16 17 import ( 18 "testing" 19 20 "github.com/stretchr/testify/require" 21 ) 22 23 func TestHexToInt(t *testing.T) { 24 var val uint64 25 var err error 26 27 val, err = HexToInt("0x1") 28 require.NoError(t, err) 29 require.Equal(t, uint64(1), val) 30 31 val, err = HexToInt("0x123456789abcdef") 32 require.NoError(t, err) 33 require.Equal(t, uint64(81985529216486895), val) 34 35 _, err = HexToInt("0xg") 36 require.Error(t, err) 37 } 38 39 func TestBinaryToInt(t *testing.T) { 40 var val uint64 41 var err error 42 43 val, err = BinaryToInt("0x1") 44 require.NoError(t, err) 45 require.Equal(t, uint64(1), val) 46 47 val, err = BinaryToInt("0b0101010101001") 48 require.NoError(t, err) 49 require.Equal(t, uint64(2729), val) 50 51 _, err = BinaryToInt("0x2") 52 require.Error(t, err) 53 } 54 55 func TestScoreBinaryToInt(t *testing.T) { 56 var val uint64 57 var err error 58 59 val, err = ScoreBinaryToInt("1") 60 require.NoError(t, err) 61 require.Equal(t, uint64(49), val) 62 63 val, err = ScoreBinaryToInt("1234") 64 require.NoError(t, err) 65 require.Equal(t, uint64(825373492), val) 66 67 _, err = ScoreBinaryToInt("123456789") 68 require.Error(t, err) 69 70 val, err = ScoreBinaryToInt("阿斯") 71 require.NoError(t, err) 72 require.Equal(t, uint64(256842263860911), val) 73 }