github.com/XiaoMi/Gaea@v1.2.5/parser/tidb-types/etc_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 types
    15  
    16  import (
    17  	"io"
    18  	"testing"
    19  
    20  	. "github.com/pingcap/check"
    21  
    22  	"github.com/XiaoMi/Gaea/mysql"
    23  	"github.com/XiaoMi/Gaea/parser/terror"
    24  	"github.com/XiaoMi/Gaea/util/testleak"
    25  )
    26  
    27  func TestT(t *testing.T) {
    28  	TestingT(t)
    29  }
    30  
    31  var _ = Suite(&testTypeEtcSuite{})
    32  
    33  type testTypeEtcSuite struct {
    34  }
    35  
    36  func testIsTypeBlob(c *C, tp byte, expect bool) {
    37  	v := IsTypeBlob(tp)
    38  	c.Assert(v, Equals, expect)
    39  }
    40  
    41  func testIsTypeChar(c *C, tp byte, expect bool) {
    42  	v := IsTypeChar(tp)
    43  	c.Assert(v, Equals, expect)
    44  }
    45  
    46  func (s *testTypeEtcSuite) TestIsType(c *C) {
    47  	defer testleak.AfterTest(c)()
    48  	testIsTypeBlob(c, mysql.TypeTinyBlob, true)
    49  	testIsTypeBlob(c, mysql.TypeMediumBlob, true)
    50  	testIsTypeBlob(c, mysql.TypeBlob, true)
    51  	testIsTypeBlob(c, mysql.TypeLongBlob, true)
    52  	testIsTypeBlob(c, mysql.TypeInt24, false)
    53  
    54  	testIsTypeChar(c, mysql.TypeString, true)
    55  	testIsTypeChar(c, mysql.TypeVarchar, true)
    56  	testIsTypeChar(c, mysql.TypeLong, false)
    57  }
    58  
    59  func testTypeStr(c *C, tp byte, expect string) {
    60  	v := TypeStr(tp)
    61  	c.Assert(v, Equals, expect)
    62  }
    63  
    64  func testTypeToStr(c *C, tp byte, charset string, expect string) {
    65  	v := TypeToStr(tp, charset)
    66  	c.Assert(v, Equals, expect)
    67  }
    68  
    69  func (s *testTypeEtcSuite) TestTypeToStr(c *C) {
    70  	defer testleak.AfterTest(c)()
    71  	testTypeStr(c, mysql.TypeYear, "year")
    72  	testTypeStr(c, 0xdd, "")
    73  
    74  	testTypeToStr(c, mysql.TypeBlob, "utf8", "text")
    75  	testTypeToStr(c, mysql.TypeLongBlob, "utf8", "longtext")
    76  	testTypeToStr(c, mysql.TypeTinyBlob, "utf8", "tinytext")
    77  	testTypeToStr(c, mysql.TypeMediumBlob, "utf8", "mediumtext")
    78  	testTypeToStr(c, mysql.TypeVarchar, "binary", "varbinary")
    79  	testTypeToStr(c, mysql.TypeString, "binary", "binary")
    80  	testTypeToStr(c, mysql.TypeTiny, "binary", "tinyint")
    81  	testTypeToStr(c, mysql.TypeBlob, "binary", "blob")
    82  	testTypeToStr(c, mysql.TypeLongBlob, "binary", "longblob")
    83  	testTypeToStr(c, mysql.TypeTinyBlob, "binary", "tinyblob")
    84  	testTypeToStr(c, mysql.TypeMediumBlob, "binary", "mediumblob")
    85  	testTypeToStr(c, mysql.TypeVarchar, "utf8", "varchar")
    86  	testTypeToStr(c, mysql.TypeString, "utf8", "char")
    87  	testTypeToStr(c, mysql.TypeShort, "binary", "smallint")
    88  	testTypeToStr(c, mysql.TypeInt24, "binary", "mediumint")
    89  	testTypeToStr(c, mysql.TypeLong, "binary", "int")
    90  	testTypeToStr(c, mysql.TypeLonglong, "binary", "bigint")
    91  	testTypeToStr(c, mysql.TypeFloat, "binary", "float")
    92  	testTypeToStr(c, mysql.TypeDouble, "binary", "double")
    93  	testTypeToStr(c, mysql.TypeYear, "binary", "year")
    94  	testTypeToStr(c, mysql.TypeDuration, "binary", "time")
    95  	testTypeToStr(c, mysql.TypeDatetime, "binary", "datetime")
    96  	testTypeToStr(c, mysql.TypeDate, "binary", "date")
    97  	testTypeToStr(c, mysql.TypeTimestamp, "binary", "timestamp")
    98  	testTypeToStr(c, mysql.TypeNewDecimal, "binary", "decimal")
    99  	testTypeToStr(c, mysql.TypeUnspecified, "binary", "unspecified")
   100  	testTypeToStr(c, 0xdd, "binary", "")
   101  	testTypeToStr(c, mysql.TypeBit, "binary", "bit")
   102  	testTypeToStr(c, mysql.TypeEnum, "binary", "enum")
   103  	testTypeToStr(c, mysql.TypeSet, "binary", "set")
   104  }
   105  
   106  func (s *testTypeEtcSuite) TestEOFAsNil(c *C) {
   107  	defer testleak.AfterTest(c)()
   108  	err := EOFAsNil(io.EOF)
   109  	c.Assert(err, IsNil)
   110  }
   111  
   112  func (s *testTypeEtcSuite) TestMaxFloat(c *C) {
   113  	defer testleak.AfterTest(c)()
   114  	tbl := []struct {
   115  		Flen    int
   116  		Decimal int
   117  		Expect  float64
   118  	}{
   119  		{3, 2, 9.99},
   120  		{5, 2, 999.99},
   121  		{10, 1, 999999999.9},
   122  		{5, 5, 0.99999},
   123  	}
   124  
   125  	for _, t := range tbl {
   126  		f := GetMaxFloat(t.Flen, t.Decimal)
   127  		c.Assert(f, Equals, t.Expect)
   128  	}
   129  }
   130  
   131  func (s *testTypeEtcSuite) TestRoundFloat(c *C) {
   132  	defer testleak.AfterTest(c)()
   133  	tbl := []struct {
   134  		Input  float64
   135  		Expect float64
   136  	}{
   137  		{2.5, 3},
   138  		{1.5, 2},
   139  		{0.5, 1},
   140  		{0.49999999999999997, 0},
   141  		{0, 0},
   142  		{-0.49999999999999997, 0},
   143  		{-0.5, -1},
   144  		{-2.5, -3},
   145  		{-1.5, -2},
   146  	}
   147  
   148  	for _, t := range tbl {
   149  		f := RoundFloat(t.Input)
   150  		c.Assert(f, Equals, t.Expect)
   151  	}
   152  }
   153  
   154  func (s *testTypeEtcSuite) TestRound(c *C) {
   155  	defer testleak.AfterTest(c)()
   156  	tbl := []struct {
   157  		Input  float64
   158  		Dec    int
   159  		Expect float64
   160  	}{
   161  		{-1.23, 0, -1},
   162  		{-1.58, 0, -2},
   163  		{1.58, 0, 2},
   164  		{1.298, 1, 1.3},
   165  		{1.298, 0, 1},
   166  		{23.298, -1, 20},
   167  	}
   168  
   169  	for _, t := range tbl {
   170  		f := Round(t.Input, t.Dec)
   171  		c.Assert(f, Equals, t.Expect)
   172  	}
   173  }
   174  
   175  func (s *testTypeEtcSuite) TestTruncate(c *C) {
   176  	defer testleak.AfterTest(c)()
   177  	tbl := []struct {
   178  		Input   float64
   179  		Flen    int
   180  		Decimal int
   181  		Expect  float64
   182  		Err     error
   183  	}{
   184  		{100.114, 10, 2, 100.11, nil},
   185  		{100.115, 10, 2, 100.12, nil},
   186  		{100.1156, 10, 3, 100.116, nil},
   187  		{100.1156, 3, 1, 99.9, ErrOverflow},
   188  		{1.36, 10, 2, 1.36, nil},
   189  	}
   190  
   191  	for _, t := range tbl {
   192  		f, err := TruncateFloat(t.Input, t.Flen, t.Decimal)
   193  		c.Assert(f, Equals, t.Expect)
   194  		c.Assert(terror.ErrorEqual(err, t.Err), IsTrue, Commentf("err %v", err))
   195  	}
   196  }