github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/mysql/hex_test.go (about) 1 // Copyright 2015 PingCAP, 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 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package mysql 15 16 import ( 17 "strconv" 18 19 . "github.com/insionng/yougam/libraries/pingcap/check" 20 "github.com/insionng/yougam/libraries/pingcap/tidb/util/testleak" 21 ) 22 23 var _ = Suite(&testHexSuite{}) 24 25 type testHexSuite struct { 26 } 27 28 func (s *testHexSuite) TestHex(c *C) { 29 defer testleak.AfterTest(c)() 30 tbl := []struct { 31 Input string 32 Expect int64 33 Err bool 34 }{ 35 {"x'1'", 0, true}, 36 {"x'01'", 1, false}, 37 {"X'01'", 1, false}, 38 {"0x1", 1, false}, 39 {"0x-1", 0, true}, 40 {"0X11", 0, true}, 41 {"x'01+'", 1, true}, 42 {"0x123", 0x123, false}, 43 {"0x10", 0x10, false}, 44 {"", 0, true}, 45 } 46 47 for _, t := range tbl { 48 h, err := ParseHex(t.Input) 49 if t.Err { 50 c.Assert(err, NotNil) 51 continue 52 } 53 54 c.Assert(err, IsNil) 55 c.Assert(h.ToNumber(), Equals, float64(t.Expect)) 56 s := h.String() 57 n, err := strconv.ParseInt(s, 0, 64) 58 c.Assert(err, IsNil) 59 c.Assert(n, Equals, t.Expect) 60 } 61 62 h, err := ParseHex("0x4D7953514C") 63 c.Assert(err, IsNil) 64 c.Assert(h.ToString(), Equals, "MySQL") 65 66 h.Value = 1 67 c.Assert(h.ToString(), Equals, "\x01") 68 69 /* 70 mysql> select hex("I am a long hex string"); 71 +----------------------------------------------+ 72 | hex("I am a long hex string") | 73 +----------------------------------------------+ 74 | 4920616D2061206C6F6E672068657820737472696E67 | 75 +----------------------------------------------+ 76 1 row in set (0.00 sec) 77 */ 78 str := "I am a long hex string" 79 hexStr := "0x4920616D2061206C6F6E672068657820737472696E67" 80 _, err = ParseHex(hexStr) 81 c.Assert(err, NotNil) 82 v, err := ParseHexStr(hexStr) 83 c.Assert(err, IsNil) 84 c.Assert(v, Equals, str) 85 }