github.com/XiaoMi/Gaea@v1.2.5/parser/tidb-types/fsp_test.go (about) 1 // Copyright 2019 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 "strconv" 18 "testing" 19 20 . "github.com/pingcap/check" 21 ) 22 23 func Test(t *testing.T) { 24 TestingT(t) 25 } 26 27 var _ = Suite(&FspTest{}) 28 29 type FspTest struct{} 30 31 func (s *FspTest) TestCheckFsp(c *C) { 32 c.Parallel() 33 obtained, err := CheckFsp(UnspecifiedFsp) 34 c.Assert(obtained, Equals, DefaultFsp) 35 c.Assert(err, IsNil) 36 37 obtained, err = CheckFsp(-2019) 38 c.Assert(obtained, Equals, DefaultFsp) 39 c.Assert(err, ErrorMatches, "Invalid fsp -2019") 40 41 obtained, err = CheckFsp(MinFsp - 4294967296) 42 c.Assert(obtained, Equals, DefaultFsp) 43 c.Assert(err, ErrorMatches, "Invalid fsp "+strconv.Itoa(MinFsp-4294967296)) 44 45 // UnspecifiedFsp 46 obtained, err = CheckFsp(-1) 47 c.Assert(obtained, Equals, DefaultFsp) 48 c.Assert(err, IsNil) 49 50 obtained, err = CheckFsp(MaxFsp + 1) 51 c.Assert(obtained, Equals, DefaultFsp) 52 c.Assert(err, ErrorMatches, "Invalid fsp "+strconv.Itoa(MaxFsp+1)) 53 54 obtained, err = CheckFsp(MaxFsp + 2019) 55 c.Assert(obtained, Equals, DefaultFsp) 56 c.Assert(err, ErrorMatches, "Invalid fsp "+strconv.Itoa(MaxFsp+2019)) 57 58 obtained, err = CheckFsp(MaxFsp + 4294967296) 59 c.Assert(obtained, Equals, DefaultFsp) 60 c.Assert(err, ErrorMatches, "Invalid fsp "+strconv.Itoa(MaxFsp+4294967296)) 61 62 obtained, err = CheckFsp((MaxFsp + MinFsp) / 2) 63 c.Assert(obtained, Equals, (MaxFsp+MinFsp)/2) 64 c.Assert(err, IsNil) 65 66 obtained, err = CheckFsp(5) 67 c.Assert(obtained, Equals, 5) 68 c.Assert(err, IsNil) 69 } 70 71 func (s *FspTest) TestParseFrac(c *C) { 72 c.Parallel() 73 obtained, overflow, err := ParseFrac("", 5) 74 c.Assert(obtained, Equals, 0) 75 c.Assert(overflow, Equals, false) 76 c.Assert(err, IsNil) 77 78 obtained, overflow, err = ParseFrac("999", 200) 79 c.Assert(obtained, Equals, 0) 80 c.Assert(overflow, Equals, false) 81 c.Assert(err, ErrorMatches, "Invalid fsp .*") 82 83 obtained, overflow, err = ParseFrac("NotNum", MaxFsp) 84 c.Assert(obtained, Equals, 0) 85 c.Assert(overflow, Equals, false) 86 c.Assert(err, ErrorMatches, "strconv.ParseInt:.*") 87 88 obtained, overflow, err = ParseFrac("1235", 6) 89 c.Assert(obtained, Equals, 123500) 90 c.Assert(overflow, Equals, false) 91 c.Assert(err, IsNil) 92 93 obtained, overflow, err = ParseFrac("123456", 4) 94 c.Assert(obtained, Equals, 123500) 95 c.Assert(overflow, Equals, false) 96 c.Assert(err, IsNil) 97 98 // 1236 round 3 -> 124 -> 124000 99 obtained, overflow, err = ParseFrac("1236", 3) 100 c.Assert(obtained, Equals, 124000) 101 c.Assert(overflow, Equals, false) 102 c.Assert(err, IsNil) 103 104 // 03123 round 2 -> 3 -> 30000 105 obtained, overflow, err = ParseFrac("0312", 2) 106 c.Assert(obtained, Equals, 30000) 107 c.Assert(overflow, Equals, false) 108 c.Assert(err, IsNil) 109 110 // 999 round 2 -> 100 -> overflow 111 obtained, overflow, err = ParseFrac("999", 2) 112 c.Assert(obtained, Equals, 0) 113 c.Assert(overflow, Equals, true) 114 c.Assert(err, IsNil) 115 } 116 117 func (s *FspTest) TestAlignFrac(c *C) { 118 c.Parallel() 119 obtained := alignFrac("100", 6) 120 c.Assert(obtained, Equals, "100000") 121 obtained = alignFrac("10000000000", 6) 122 c.Assert(obtained, Equals, "10000000000") 123 }