github.com/XiaoMi/Gaea@v1.2.5/parser/tidb-types/binary_literal_test.go (about) 1 // Copyright 2017 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 types 15 16 import ( 17 . "github.com/pingcap/check" 18 19 "github.com/XiaoMi/Gaea/parser/stmtctx" 20 "github.com/XiaoMi/Gaea/util/testleak" 21 ) 22 23 var _ = Suite(&testBinaryLiteralSuite{}) 24 25 type testBinaryLiteralSuite struct { 26 } 27 28 func (s *testBinaryLiteralSuite) TestTrimLeadingZeroBytes(c *C) { 29 defer testleak.AfterTest(c)() 30 tbl := []struct { 31 Input []byte 32 Expected []byte 33 }{ 34 {[]byte{}, []byte{}}, 35 {[]byte{0x0}, []byte{0x0}}, 36 {[]byte{0x1}, []byte{0x1}}, 37 {[]byte{0x1, 0x0}, []byte{0x1, 0x0}}, 38 {[]byte{0x0, 0x1}, []byte{0x1}}, 39 {[]byte{0x0, 0x0, 0x0}, []byte{0x0}}, 40 {[]byte{0x1, 0x0, 0x0}, []byte{0x1, 0x0, 0x0}}, 41 {[]byte{0x0, 0x1, 0x0, 0x0, 0x1, 0x0, 0x0}, []byte{0x1, 0x0, 0x0, 0x1, 0x0, 0x0}}, 42 {[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x1, 0x0, 0x0}, []byte{0x1, 0x0, 0x0, 0x1, 0x0, 0x0}}, 43 } 44 for _, t := range tbl { 45 b := trimLeadingZeroBytes(t.Input) 46 c.Assert(b, DeepEquals, t.Expected, Commentf("%#v", t)) 47 } 48 } 49 50 func (s *testBinaryLiteralSuite) TestParseBitStr(c *C) { 51 defer testleak.AfterTest(c)() 52 tbl := []struct { 53 Input string 54 Expected []byte 55 IsError bool 56 }{ 57 {"b''", []byte{}, false}, 58 {"B''", []byte{}, false}, 59 {"0b''", nil, true}, 60 {"0b0", []byte{0x0}, false}, 61 {"b'0'", []byte{0x0}, false}, 62 {"B'0'", []byte{0x0}, false}, 63 {"0B0", nil, true}, 64 {"0b123", nil, true}, 65 {"b'123'", nil, true}, 66 {"0b'1010'", nil, true}, 67 {"0b0000000", []byte{0x0}, false}, 68 {"b'0000000'", []byte{0x0}, false}, 69 {"B'0000000'", []byte{0x0}, false}, 70 {"0b00000000", []byte{0x0}, false}, 71 {"b'00000000'", []byte{0x0}, false}, 72 {"B'00000000'", []byte{0x0}, false}, 73 {"0b000000000", []byte{0x0, 0x0}, false}, 74 {"b'000000000'", []byte{0x0, 0x0}, false}, 75 {"B'000000000'", []byte{0x0, 0x0}, false}, 76 {"0b1", []byte{0x1}, false}, 77 {"b'1'", []byte{0x1}, false}, 78 {"B'1'", []byte{0x1}, false}, 79 {"0b00000001", []byte{0x1}, false}, 80 {"b'00000001'", []byte{0x1}, false}, 81 {"B'00000001'", []byte{0x1}, false}, 82 {"0b000000010", []byte{0x0, 0x2}, false}, 83 {"b'000000010'", []byte{0x0, 0x2}, false}, 84 {"B'000000010'", []byte{0x0, 0x2}, false}, 85 {"0b000000001", []byte{0x0, 0x1}, false}, 86 {"b'000000001'", []byte{0x0, 0x1}, false}, 87 {"B'000000001'", []byte{0x0, 0x1}, false}, 88 {"0b11111111", []byte{0xFF}, false}, 89 {"b'11111111'", []byte{0xFF}, false}, 90 {"B'11111111'", []byte{0xFF}, false}, 91 {"0b111111111", []byte{0x1, 0xFF}, false}, 92 {"b'111111111'", []byte{0x1, 0xFF}, false}, 93 {"B'111111111'", []byte{0x1, 0xFF}, false}, 94 {"0b1101000011001010110110001101100011011110010000001110111011011110111001001101100011001000010000001100110011011110110111100100000011000100110000101110010", []byte("hello world foo bar"), false}, 95 {"b'1101000011001010110110001101100011011110010000001110111011011110111001001101100011001000010000001100110011011110110111100100000011000100110000101110010'", []byte("hello world foo bar"), false}, 96 {"B'1101000011001010110110001101100011011110010000001110111011011110111001001101100011001000010000001100110011011110110111100100000011000100110000101110010'", []byte("hello world foo bar"), false}, 97 {"0b01101000011001010110110001101100011011110010000001110111011011110111001001101100011001000010000001100110011011110110111100100000011000100110000101110010", []byte("hello world foo bar"), false}, 98 {"b'01101000011001010110110001101100011011110010000001110111011011110111001001101100011001000010000001100110011011110110111100100000011000100110000101110010'", []byte("hello world foo bar"), false}, 99 {"B'01101000011001010110110001101100011011110010000001110111011011110111001001101100011001000010000001100110011011110110111100100000011000100110000101110010'", []byte("hello world foo bar"), false}, 100 } 101 for _, t := range tbl { 102 b, err := ParseBitStr(t.Input) 103 if t.IsError { 104 c.Assert(err, NotNil, Commentf("%#v", t)) 105 } else { 106 c.Assert(err, IsNil, Commentf("%#v", t)) 107 c.Assert([]byte(b), DeepEquals, t.Expected, Commentf("%#v", t)) 108 } 109 } 110 } 111 112 func (s *testBinaryLiteralSuite) TestParseHexStr(c *C) { 113 defer testleak.AfterTest(c)() 114 tbl := []struct { 115 Input string 116 Expected []byte 117 IsError bool 118 }{ 119 {"x'1'", nil, true}, 120 {"x'01'", []byte{0x1}, false}, 121 {"X'01'", []byte{0x1}, false}, 122 {"0x1", []byte{0x1}, false}, 123 {"0x-1", nil, true}, 124 {"0X11", nil, true}, 125 {"x'01+'", nil, true}, 126 {"0x123", []byte{0x01, 0x23}, false}, 127 {"0x10", []byte{0x10}, false}, 128 {"0x4D7953514C", []byte("MySQL"), false}, 129 {"0x4920616D2061206C6F6E672068657820737472696E67", []byte("I am a long hex string"), false}, 130 {"x'4920616D2061206C6F6E672068657820737472696E67'", []byte("I am a long hex string"), false}, 131 {"X'4920616D2061206C6F6E672068657820737472696E67'", []byte("I am a long hex string"), false}, 132 {"x''", []byte{}, false}, 133 } 134 for _, t := range tbl { 135 hex, err := ParseHexStr(t.Input) 136 if t.IsError { 137 c.Assert(err, NotNil, Commentf("%#v", t)) 138 } else { 139 c.Assert(err, IsNil, Commentf("%#v", t)) 140 c.Assert([]byte(hex), DeepEquals, t.Expected, Commentf("%#v", t)) 141 } 142 } 143 } 144 145 func (s *testBinaryLiteralSuite) TestString(c *C) { 146 defer testleak.AfterTest(c)() 147 tbl := []struct { 148 Input BinaryLiteral 149 Expected string 150 }{ 151 {BinaryLiteral{}, ""}, // Expected 152 {BinaryLiteral{0x0}, "0x00"}, 153 {BinaryLiteral{0x1}, "0x01"}, 154 {BinaryLiteral{0xff, 0x01}, "0xff01"}, 155 } 156 for _, t := range tbl { 157 str := t.Input.String() 158 c.Assert(str, Equals, t.Expected) 159 } 160 } 161 162 func (s *testBinaryLiteralSuite) TestToBitLiteralString(c *C) { 163 defer testleak.AfterTest(c)() 164 tbl := []struct { 165 Input BinaryLiteral 166 TrimLeadingZero bool 167 Expected string 168 }{ 169 {BinaryLiteral{}, true, "b''"}, 170 {BinaryLiteral{}, false, "b''"}, 171 {BinaryLiteral{0x0}, true, "b'0'"}, 172 {BinaryLiteral{0x0}, false, "b'00000000'"}, 173 {BinaryLiteral{0x0, 0x0}, true, "b'0'"}, 174 {BinaryLiteral{0x0, 0x0}, false, "b'0000000000000000'"}, 175 {BinaryLiteral{0x1}, true, "b'1'"}, 176 {BinaryLiteral{0x1}, false, "b'00000001'"}, 177 {BinaryLiteral{0xff, 0x01}, true, "b'1111111100000001'"}, 178 {BinaryLiteral{0xff, 0x01}, false, "b'1111111100000001'"}, 179 {BinaryLiteral{0x0, 0xff, 0x01}, true, "b'1111111100000001'"}, 180 {BinaryLiteral{0x0, 0xff, 0x01}, false, "b'000000001111111100000001'"}, 181 } 182 for _, t := range tbl { 183 str := t.Input.ToBitLiteralString(t.TrimLeadingZero) 184 c.Assert(str, Equals, t.Expected) 185 } 186 } 187 188 func (s *testBinaryLiteralSuite) TestToInt(c *C) { 189 defer testleak.AfterTest(c)() 190 tbl := []struct { 191 Input string 192 Expected uint64 193 HasError bool 194 }{ 195 {"x''", 0, false}, 196 {"0x00", 0x0, false}, 197 {"0xff", 0xff, false}, 198 {"0x10ff", 0x10ff, false}, 199 {"0x1010ffff", 0x1010ffff, false}, 200 {"0x1010ffff8080", 0x1010ffff8080, false}, 201 {"0x1010ffff8080ff12", 0x1010ffff8080ff12, false}, 202 {"0x1010ffff8080ff12ff", 0xffffffffffffffff, true}, 203 } 204 sc := new(stmtctx.StatementContext) 205 for _, t := range tbl { 206 hex, err := ParseHexStr(t.Input) 207 c.Assert(err, IsNil) 208 intValue, err := hex.ToInt(sc) 209 if t.HasError { 210 c.Assert(err, NotNil) 211 } else { 212 c.Assert(err, IsNil) 213 } 214 c.Assert(intValue, Equals, t.Expected) 215 } 216 } 217 218 func (s *testBinaryLiteralSuite) TestNewBinaryLiteralFromUint(c *C) { 219 defer testleak.AfterTest(c)() 220 tbl := []struct { 221 Input uint64 222 ByteSize int 223 Expected []byte 224 }{ 225 {0x0, -1, []byte{0x0}}, 226 {0x0, 1, []byte{0x0}}, 227 {0x0, 2, []byte{0x0, 0x0}}, 228 {0x1, -1, []byte{0x1}}, 229 {0x1, 1, []byte{0x1}}, 230 {0x1, 2, []byte{0x0, 0x1}}, 231 {0x1, 3, []byte{0x0, 0x0, 0x1}}, 232 {0x10, -1, []byte{0x10}}, 233 {0x123, -1, []byte{0x1, 0x23}}, 234 {0x123, 2, []byte{0x1, 0x23}}, 235 {0x123, 1, []byte{0x23}}, 236 {0x123, 5, []byte{0x0, 0x0, 0x0, 0x1, 0x23}}, 237 {0x4D7953514C, -1, []byte{0x4D, 0x79, 0x53, 0x51, 0x4C}}, 238 {0x4D7953514C, 8, []byte{0x0, 0x0, 0x0, 0x4D, 0x79, 0x53, 0x51, 0x4C}}, 239 {0x4920616D2061206C, -1, []byte{0x49, 0x20, 0x61, 0x6D, 0x20, 0x61, 0x20, 0x6C}}, 240 {0x4920616D2061206C, 8, []byte{0x49, 0x20, 0x61, 0x6D, 0x20, 0x61, 0x20, 0x6C}}, 241 {0x4920616D2061206C, 5, []byte{0x6D, 0x20, 0x61, 0x20, 0x6C}}, 242 } 243 for _, t := range tbl { 244 hex := NewBinaryLiteralFromUint(t.Input, t.ByteSize) 245 c.Assert([]byte(hex), DeepEquals, t.Expected, Commentf("%#v", t)) 246 } 247 } 248 249 func (s *testBinaryLiteralSuite) TestCompare(c *C) { 250 tbl := []struct { 251 a BinaryLiteral 252 b BinaryLiteral 253 cmp int 254 }{ 255 {BinaryLiteral{0, 0, 1}, BinaryLiteral{2}, -1}, 256 {BinaryLiteral{0, 1}, BinaryLiteral{0, 0, 2}, -1}, 257 {BinaryLiteral{0, 1}, BinaryLiteral{1}, 0}, 258 {BinaryLiteral{0, 2, 1}, BinaryLiteral{1, 2}, 1}, 259 } 260 for _, t := range tbl { 261 c.Assert(t.a.Compare(t.b), Equals, t.cmp) 262 } 263 }