github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/util/codec/codec_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 codec
    15  
    16  import (
    17  	"bytes"
    18  	"math"
    19  	"testing"
    20  
    21  	. "github.com/insionng/yougam/libraries/pingcap/check"
    22  	"github.com/insionng/yougam/libraries/pingcap/tidb/mysql"
    23  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/testleak"
    24  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/types"
    25  )
    26  
    27  func TestT(t *testing.T) {
    28  	TestingT(t)
    29  }
    30  
    31  var _ = Suite(&testCodecSuite{})
    32  
    33  type testCodecSuite struct {
    34  }
    35  
    36  func (s *testCodecSuite) TestCodecKey(c *C) {
    37  	defer testleak.AfterTest(c)()
    38  	table := []struct {
    39  		Input  []types.Datum
    40  		Expect []types.Datum
    41  	}{
    42  		{
    43  			types.MakeDatums(int64(1)),
    44  			types.MakeDatums(int64(1)),
    45  		},
    46  
    47  		{
    48  			types.MakeDatums(float32(1), float64(3.15), []byte("123"), "123"),
    49  			types.MakeDatums(float64(1), float64(3.15), []byte("123"), []byte("123")),
    50  		},
    51  		{
    52  			types.MakeDatums(uint64(1), float64(3.15), []byte("123"), int64(-1)),
    53  			types.MakeDatums(uint64(1), float64(3.15), []byte("123"), int64(-1)),
    54  		},
    55  
    56  		{
    57  			types.MakeDatums(true, false),
    58  			types.MakeDatums(int64(1), int64(0)),
    59  		},
    60  
    61  		{
    62  			types.MakeDatums(nil),
    63  			types.MakeDatums(nil),
    64  		},
    65  
    66  		{
    67  			types.MakeDatums(mysql.Hex{Value: 100}, mysql.Bit{Value: 100, Width: 8}),
    68  			types.MakeDatums(int64(100), uint64(100)),
    69  		},
    70  
    71  		{
    72  			types.MakeDatums(mysql.Enum{Name: "a", Value: 1}, mysql.Set{Name: "a", Value: 1}),
    73  			types.MakeDatums(uint64(1), uint64(1)),
    74  		},
    75  	}
    76  
    77  	for i, t := range table {
    78  		comment := Commentf("%d %v", i, t)
    79  		b, err := EncodeKey(nil, t.Input...)
    80  		c.Assert(err, IsNil, comment)
    81  		args, err := Decode(b)
    82  		c.Assert(err, IsNil)
    83  		c.Assert(args, DeepEquals, t.Expect)
    84  
    85  		b, err = EncodeValue(nil, t.Input...)
    86  		c.Assert(err, IsNil)
    87  		args, err = Decode(b)
    88  		c.Assert(err, IsNil)
    89  		c.Assert(args, DeepEquals, t.Expect)
    90  	}
    91  }
    92  
    93  func (s *testCodecSuite) TestCodecKeyCompare(c *C) {
    94  	defer testleak.AfterTest(c)()
    95  	table := []struct {
    96  		Left   []types.Datum
    97  		Right  []types.Datum
    98  		Expect int
    99  	}{
   100  		{
   101  			types.MakeDatums(1),
   102  			types.MakeDatums(1),
   103  			0,
   104  		},
   105  		{
   106  			types.MakeDatums(-1),
   107  			types.MakeDatums(1),
   108  			-1,
   109  		},
   110  		{
   111  			types.MakeDatums(3.15),
   112  			types.MakeDatums(3.12),
   113  			1,
   114  		},
   115  		{
   116  			types.MakeDatums("abc"),
   117  			types.MakeDatums("abcd"),
   118  			-1,
   119  		},
   120  		{
   121  			types.MakeDatums("abcdefgh"),
   122  			types.MakeDatums("abcdefghi"),
   123  			-1,
   124  		},
   125  		{
   126  			types.MakeDatums(1, "abc"),
   127  			types.MakeDatums(1, "abcd"),
   128  			-1,
   129  		},
   130  		{
   131  			types.MakeDatums(1, "abc", "def"),
   132  			types.MakeDatums(1, "abcd", "af"),
   133  			-1,
   134  		},
   135  		{
   136  			types.MakeDatums(3.12, "ebc", "def"),
   137  			types.MakeDatums(2.12, "abcd", "af"),
   138  			1,
   139  		},
   140  		{
   141  			types.MakeDatums([]byte{0x01, 0x00}, []byte{0xFF}),
   142  			types.MakeDatums([]byte{0x01, 0x00, 0xFF}),
   143  			-1,
   144  		},
   145  		{
   146  			types.MakeDatums([]byte{0x01}, uint64(0xFFFFFFFFFFFFFFF)),
   147  			types.MakeDatums([]byte{0x01, 0x10}, 0),
   148  			-1,
   149  		},
   150  		{
   151  			types.MakeDatums(0),
   152  			types.MakeDatums(nil),
   153  			1,
   154  		},
   155  		{
   156  			types.MakeDatums([]byte{0x00}),
   157  			types.MakeDatums(nil),
   158  			1,
   159  		},
   160  		{
   161  			types.MakeDatums(math.SmallestNonzeroFloat64),
   162  			types.MakeDatums(nil),
   163  			1,
   164  		},
   165  		{
   166  			types.MakeDatums(int64(math.MinInt64)),
   167  			types.MakeDatums(nil),
   168  			1,
   169  		},
   170  		{
   171  			types.MakeDatums(1, int64(math.MinInt64), nil),
   172  			types.MakeDatums(1, nil, uint64(math.MaxUint64)),
   173  			1,
   174  		},
   175  		{
   176  			types.MakeDatums(1, []byte{}, nil),
   177  			types.MakeDatums(1, nil, 123),
   178  			1,
   179  		},
   180  		{
   181  			types.MakeDatums(parseTime(c, "2011-11-11 00:00:00"), 1),
   182  			types.MakeDatums(parseTime(c, "2011-11-11 00:00:00"), 0),
   183  			1,
   184  		},
   185  		{
   186  			types.MakeDatums(parseDuration(c, "00:00:00"), 1),
   187  			types.MakeDatums(parseDuration(c, "00:00:01"), 0),
   188  			-1,
   189  		},
   190  	}
   191  
   192  	for _, t := range table {
   193  		b1, err := EncodeKey(nil, t.Left...)
   194  		c.Assert(err, IsNil)
   195  
   196  		b2, err := EncodeKey(nil, t.Right...)
   197  		c.Assert(err, IsNil)
   198  
   199  		c.Assert(bytes.Compare(b1, b2), Equals, t.Expect, Commentf("%v - %v - %v - %v - %v", t.Left, t.Right, b1, b2, t.Expect))
   200  	}
   201  }
   202  
   203  func (s *testCodecSuite) TestNumberCodec(c *C) {
   204  	defer testleak.AfterTest(c)()
   205  	tblInt64 := []int64{
   206  		math.MinInt64,
   207  		math.MinInt32,
   208  		math.MinInt16,
   209  		math.MinInt8,
   210  		0,
   211  		math.MaxInt8,
   212  		math.MaxInt16,
   213  		math.MaxInt32,
   214  		math.MaxInt64,
   215  		1<<47 - 1,
   216  		-1 << 47,
   217  		1<<23 - 1,
   218  		-1 << 23,
   219  		1<<55 - 1,
   220  		-1 << 55,
   221  		1,
   222  		-1,
   223  	}
   224  
   225  	for _, t := range tblInt64 {
   226  		b := EncodeInt(nil, t)
   227  		_, v, err := DecodeInt(b)
   228  		c.Assert(err, IsNil)
   229  		c.Assert(v, Equals, t)
   230  
   231  		b = EncodeIntDesc(nil, t)
   232  		_, v, err = DecodeIntDesc(b)
   233  		c.Assert(err, IsNil)
   234  		c.Assert(v, Equals, t)
   235  
   236  		b = EncodeVarint(nil, t)
   237  		_, v, err = DecodeVarint(b)
   238  		c.Assert(err, IsNil)
   239  		c.Assert(v, Equals, t)
   240  	}
   241  
   242  	tblUint64 := []uint64{
   243  		0,
   244  		math.MaxUint8,
   245  		math.MaxUint16,
   246  		math.MaxUint32,
   247  		math.MaxUint64,
   248  		1<<24 - 1,
   249  		1<<48 - 1,
   250  		1<<56 - 1,
   251  		1,
   252  		math.MaxInt16,
   253  		math.MaxInt8,
   254  		math.MaxInt32,
   255  		math.MaxInt64,
   256  	}
   257  
   258  	for _, t := range tblUint64 {
   259  		b := EncodeUint(nil, t)
   260  		_, v, err := DecodeUint(b)
   261  		c.Assert(err, IsNil)
   262  		c.Assert(v, Equals, t)
   263  
   264  		b = EncodeUintDesc(nil, t)
   265  		_, v, err = DecodeUintDesc(b)
   266  		c.Assert(err, IsNil)
   267  		c.Assert(v, Equals, t)
   268  
   269  		b = EncodeUvarint(nil, t)
   270  		_, v, err = DecodeUvarint(b)
   271  		c.Assert(err, IsNil)
   272  		c.Assert(v, Equals, t)
   273  	}
   274  }
   275  
   276  func (s *testCodecSuite) TestNumberOrder(c *C) {
   277  	defer testleak.AfterTest(c)()
   278  	tblInt64 := []struct {
   279  		Arg1 int64
   280  		Arg2 int64
   281  		Ret  int
   282  	}{
   283  		{-1, 1, -1},
   284  		{math.MaxInt64, math.MinInt64, 1},
   285  		{math.MaxInt64, math.MaxInt32, 1},
   286  		{math.MinInt32, math.MaxInt16, -1},
   287  		{math.MinInt64, math.MaxInt8, -1},
   288  		{0, math.MaxInt8, -1},
   289  		{math.MinInt8, 0, -1},
   290  		{math.MinInt16, math.MaxInt16, -1},
   291  		{1, -1, 1},
   292  		{1, 0, 1},
   293  		{-1, 0, -1},
   294  		{0, 0, 0},
   295  		{math.MaxInt16, math.MaxInt16, 0},
   296  	}
   297  
   298  	for _, t := range tblInt64 {
   299  		b1 := EncodeInt(nil, t.Arg1)
   300  		b2 := EncodeInt(nil, t.Arg2)
   301  
   302  		ret := bytes.Compare(b1, b2)
   303  		c.Assert(ret, Equals, t.Ret)
   304  
   305  		b1 = EncodeIntDesc(nil, t.Arg1)
   306  		b2 = EncodeIntDesc(nil, t.Arg2)
   307  
   308  		ret = bytes.Compare(b1, b2)
   309  		c.Assert(ret, Equals, -t.Ret)
   310  	}
   311  
   312  	tblUint64 := []struct {
   313  		Arg1 uint64
   314  		Arg2 uint64
   315  		Ret  int
   316  	}{
   317  		{0, 0, 0},
   318  		{1, 0, 1},
   319  		{0, 1, -1},
   320  		{math.MaxInt8, math.MaxInt16, -1},
   321  		{math.MaxUint32, math.MaxInt32, 1},
   322  		{math.MaxUint8, math.MaxInt8, 1},
   323  		{math.MaxUint16, math.MaxInt32, -1},
   324  		{math.MaxUint64, math.MaxInt64, 1},
   325  		{math.MaxInt64, math.MaxUint32, 1},
   326  		{math.MaxUint64, 0, 1},
   327  		{0, math.MaxUint64, -1},
   328  	}
   329  
   330  	for _, t := range tblUint64 {
   331  		b1 := EncodeUint(nil, t.Arg1)
   332  		b2 := EncodeUint(nil, t.Arg2)
   333  
   334  		ret := bytes.Compare(b1, b2)
   335  		c.Assert(ret, Equals, t.Ret)
   336  
   337  		b1 = EncodeUintDesc(nil, t.Arg1)
   338  		b2 = EncodeUintDesc(nil, t.Arg2)
   339  
   340  		ret = bytes.Compare(b1, b2)
   341  		c.Assert(ret, Equals, -t.Ret)
   342  	}
   343  }
   344  
   345  func (s *testCodecSuite) TestFloatCodec(c *C) {
   346  	defer testleak.AfterTest(c)()
   347  	tblFloat := []float64{
   348  		-1,
   349  		0,
   350  		1,
   351  		math.MaxFloat64,
   352  		math.MaxFloat32,
   353  		math.SmallestNonzeroFloat32,
   354  		math.SmallestNonzeroFloat64,
   355  		math.Inf(-1),
   356  		math.Inf(1),
   357  	}
   358  
   359  	for _, t := range tblFloat {
   360  		b := EncodeFloat(nil, t)
   361  		_, v, err := DecodeFloat(b)
   362  		c.Assert(err, IsNil)
   363  		c.Assert(v, Equals, t)
   364  
   365  		b = EncodeFloatDesc(nil, t)
   366  		_, v, err = DecodeFloatDesc(b)
   367  		c.Assert(err, IsNil)
   368  		c.Assert(v, Equals, t)
   369  	}
   370  
   371  	tblCmp := []struct {
   372  		Arg1 float64
   373  		Arg2 float64
   374  		Ret  int
   375  	}{
   376  		{1, -1, 1},
   377  		{1, 0, 1},
   378  		{0, -1, 1},
   379  		{0, 0, 0},
   380  		{math.MaxFloat64, 1, 1},
   381  		{math.MaxFloat32, math.MaxFloat64, -1},
   382  		{math.MaxFloat64, 0, 1},
   383  		{math.MaxFloat64, math.SmallestNonzeroFloat64, 1},
   384  		{math.Inf(-1), 0, -1},
   385  		{math.Inf(1), 0, 1},
   386  		{math.Inf(-1), math.Inf(1), -1},
   387  	}
   388  
   389  	for _, t := range tblCmp {
   390  		b1 := EncodeFloat(nil, t.Arg1)
   391  		b2 := EncodeFloat(nil, t.Arg2)
   392  
   393  		ret := bytes.Compare(b1, b2)
   394  		c.Assert(ret, Equals, t.Ret)
   395  
   396  		b1 = EncodeFloatDesc(nil, t.Arg1)
   397  		b2 = EncodeFloatDesc(nil, t.Arg2)
   398  
   399  		ret = bytes.Compare(b1, b2)
   400  		c.Assert(ret, Equals, -t.Ret)
   401  	}
   402  }
   403  
   404  func (s *testCodecSuite) TestBytes(c *C) {
   405  	defer testleak.AfterTest(c)()
   406  	tblBytes := [][]byte{
   407  		{},
   408  		{0x00, 0x01},
   409  		{0xff, 0xff},
   410  		{0x01, 0x00},
   411  		[]byte("abc"),
   412  		[]byte("hello world"),
   413  	}
   414  
   415  	for _, t := range tblBytes {
   416  		b := EncodeBytes(nil, t)
   417  		_, v, err := DecodeBytes(b)
   418  		c.Assert(err, IsNil)
   419  		c.Assert(t, DeepEquals, v, Commentf("%v - %v - %v", t, b, v))
   420  
   421  		b = EncodeBytesDesc(nil, t)
   422  		_, v, err = DecodeBytesDesc(b)
   423  		c.Assert(err, IsNil)
   424  		c.Assert(t, DeepEquals, v, Commentf("%v - %v - %v", t, b, v))
   425  
   426  		b = EncodeCompactBytes(nil, t)
   427  		_, v, err = DecodeCompactBytes(b)
   428  		c.Assert(err, IsNil)
   429  		c.Assert(t, DeepEquals, v, Commentf("%v - %v - %v", t, b, v))
   430  	}
   431  
   432  	tblCmp := []struct {
   433  		Arg1 []byte
   434  		Arg2 []byte
   435  		Ret  int
   436  	}{
   437  		{[]byte{}, []byte{0x00}, -1},
   438  		{[]byte{0x00}, []byte{0x00}, 0},
   439  		{[]byte{0xFF}, []byte{0x00}, 1},
   440  		{[]byte{0xFF}, []byte{0xFF, 0x00}, -1},
   441  		{[]byte("a"), []byte("b"), -1},
   442  		{[]byte("a"), []byte{0x00}, 1},
   443  		{[]byte{0x00}, []byte{0x01}, -1},
   444  		{[]byte{0x00, 0x01}, []byte{0x00, 0x00}, 1},
   445  		{[]byte{0x00, 0x00, 0x00}, []byte{0x00, 0x00}, 1},
   446  		{[]byte{0x00, 0x00, 0x00}, []byte{0x00, 0x00}, 1},
   447  		{[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, -1},
   448  		{[]byte{0x01, 0x02, 0x03, 0x00}, []byte{0x01, 0x02, 0x03}, 1},
   449  		{[]byte{0x01, 0x03, 0x03, 0x04}, []byte{0x01, 0x03, 0x03, 0x05}, -1},
   450  		{[]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, -1},
   451  		{[]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}, []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, 1},
   452  		{[]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00}, []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, 1},
   453  	}
   454  
   455  	for _, t := range tblCmp {
   456  		b1 := EncodeBytes(nil, t.Arg1)
   457  		b2 := EncodeBytes(nil, t.Arg2)
   458  
   459  		ret := bytes.Compare(b1, b2)
   460  		c.Assert(ret, Equals, t.Ret)
   461  
   462  		b1 = EncodeBytesDesc(nil, t.Arg1)
   463  		b2 = EncodeBytesDesc(nil, t.Arg2)
   464  
   465  		ret = bytes.Compare(b1, b2)
   466  		c.Assert(ret, Equals, -t.Ret)
   467  	}
   468  }
   469  
   470  func parseTime(c *C, s string) mysql.Time {
   471  	m, err := mysql.ParseTime(s, mysql.TypeDatetime, mysql.DefaultFsp)
   472  	c.Assert(err, IsNil)
   473  	return m
   474  }
   475  
   476  func parseDuration(c *C, s string) mysql.Duration {
   477  	m, err := mysql.ParseDuration(s, mysql.DefaultFsp)
   478  	c.Assert(err, IsNil)
   479  	return m
   480  }
   481  
   482  func (s *testCodecSuite) TestTime(c *C) {
   483  	defer testleak.AfterTest(c)()
   484  	tbl := []string{
   485  		"2011-01-01 00:00:00",
   486  		"2011-01-01 00:00:00",
   487  		"0001-01-01 00:00:00",
   488  	}
   489  
   490  	for _, t := range tbl {
   491  		m := types.NewDatum(parseTime(c, t))
   492  
   493  		b, err := EncodeKey(nil, m)
   494  		c.Assert(err, IsNil)
   495  		v, err := Decode(b)
   496  		c.Assert(err, IsNil)
   497  		c.Assert(v, DeepEquals, types.MakeDatums([]byte(t)))
   498  	}
   499  
   500  	tblCmp := []struct {
   501  		Arg1 string
   502  		Arg2 string
   503  		Ret  int
   504  	}{
   505  		{"2011-10-10 00:00:00", "2000-12-12 11:11:11", 1},
   506  		{"2000-10-10 00:00:00", "2001-10-10 00:00:00", -1},
   507  		{"2000-10-10 00:00:00", "2000-10-10 00:00:00", 0},
   508  	}
   509  
   510  	for _, t := range tblCmp {
   511  		m1 := types.NewDatum(parseTime(c, t.Arg1))
   512  		m2 := types.NewDatum(parseTime(c, t.Arg2))
   513  
   514  		b1, err := EncodeKey(nil, m1)
   515  		c.Assert(err, IsNil)
   516  		b2, err := EncodeKey(nil, m2)
   517  		c.Assert(err, IsNil)
   518  
   519  		ret := bytes.Compare(b1, b2)
   520  		c.Assert(ret, Equals, t.Ret)
   521  	}
   522  }
   523  
   524  func (s *testCodecSuite) TestDuration(c *C) {
   525  	defer testleak.AfterTest(c)()
   526  	tbl := []string{
   527  		"11:11:11",
   528  		"00:00:00",
   529  		"1 11:11:11",
   530  	}
   531  
   532  	for _, t := range tbl {
   533  		m := parseDuration(c, t)
   534  
   535  		b, err := EncodeKey(nil, types.NewDatum(m))
   536  		c.Assert(err, IsNil)
   537  		v, err := Decode(b)
   538  		c.Assert(err, IsNil)
   539  		m.Fsp = mysql.MaxFsp
   540  		c.Assert(v, DeepEquals, types.MakeDatums(m))
   541  	}
   542  
   543  	tblCmp := []struct {
   544  		Arg1 string
   545  		Arg2 string
   546  		Ret  int
   547  	}{
   548  		{"20:00:00", "11:11:11", 1},
   549  		{"00:00:00", "00:00:01", -1},
   550  		{"00:00:00", "00:00:00", 0},
   551  	}
   552  
   553  	for _, t := range tblCmp {
   554  		m1 := parseDuration(c, t.Arg1)
   555  		m2 := parseDuration(c, t.Arg2)
   556  
   557  		b1, err := EncodeKey(nil, types.NewDatum(m1))
   558  		c.Assert(err, IsNil)
   559  		b2, err := EncodeKey(nil, types.NewDatum(m2))
   560  		c.Assert(err, IsNil)
   561  
   562  		ret := bytes.Compare(b1, b2)
   563  		c.Assert(ret, Equals, t.Ret)
   564  	}
   565  }
   566  
   567  func (s *testCodecSuite) TestDecimal(c *C) {
   568  	defer testleak.AfterTest(c)()
   569  	tbl := []string{
   570  		"1234.00",
   571  		"1234",
   572  		"12.34",
   573  		"12.340",
   574  		"0.1234",
   575  		"0.0",
   576  		"0",
   577  		"-0.0",
   578  		"-0.0000",
   579  		"-1234.00",
   580  		"-1234",
   581  		"-12.34",
   582  		"-12.340",
   583  		"-0.1234"}
   584  
   585  	for _, t := range tbl {
   586  		m, err := mysql.ParseDecimal(t)
   587  		c.Assert(err, IsNil)
   588  		b, err := EncodeKey(nil, types.NewDatum(m))
   589  		c.Assert(err, IsNil)
   590  		v, err := Decode(b)
   591  		c.Assert(err, IsNil)
   592  		c.Assert(v, HasLen, 1)
   593  		vv := v[0].GetMysqlDecimal()
   594  		c.Assert(vv.Equals(m), IsTrue)
   595  	}
   596  
   597  	tblCmp := []struct {
   598  		Arg1 interface{}
   599  		Arg2 interface{}
   600  		Ret  int
   601  	}{
   602  		// Test for float type decimal.
   603  		{"1234", "123400", -1},
   604  		{"12340", "123400", -1},
   605  		{"1234", "1234.5", -1},
   606  		{"1234", "1234.0000", 0},
   607  		{"1234", "12.34", 1},
   608  		{"12.34", "12.35", -1},
   609  		{"0.12", "0.1234", -1},
   610  		{"0.1234", "12.3400", -1},
   611  		{"0.1234", "0.1235", -1},
   612  		{"0.123400", "12.34", -1},
   613  		{"12.34000", "12.34", 0},
   614  		{"0.01234", "0.01235", -1},
   615  		{"0.1234", "0", 1},
   616  		{"0.0000", "0", 0},
   617  		{"0.0001", "0", 1},
   618  		{"0.0001", "0.0000", 1},
   619  		{"0", "-0.0000", 0},
   620  		{"-0.0001", "0", -1},
   621  		{"-0.1234", "0", -1},
   622  		{"-0.1234", "-0.12", -1},
   623  		{"-0.12", "-0.1234", 1},
   624  		{"-0.12", "-0.1200", 0},
   625  		{"-0.1234", "0.1234", -1},
   626  		{"-1.234", "-12.34", 1},
   627  		{"-0.1234", "-12.34", 1},
   628  		{"-12.34", "1234", -1},
   629  		{"-12.34", "-12.35", 1},
   630  		{"-0.01234", "-0.01235", 1},
   631  		{"-1234", "-123400", 1},
   632  		{"-12340", "-123400", 1},
   633  
   634  		// Test for int type decimal.
   635  		{int64(-1), int64(1), -1},
   636  		{int64(math.MaxInt64), int64(math.MinInt64), 1},
   637  		{int64(math.MaxInt64), int64(math.MaxInt32), 1},
   638  		{int64(math.MinInt32), int64(math.MaxInt16), -1},
   639  		{int64(math.MinInt64), int64(math.MaxInt8), -1},
   640  		{int64(0), int64(math.MaxInt8), -1},
   641  		{int64(math.MinInt8), int64(0), -1},
   642  		{int64(math.MinInt16), int64(math.MaxInt16), -1},
   643  		{int64(1), int64(-1), 1},
   644  		{int64(1), int64(0), 1},
   645  		{int64(-1), int64(0), -1},
   646  		{int64(0), int64(0), 0},
   647  		{int64(math.MaxInt16), int64(math.MaxInt16), 0},
   648  
   649  		// Test for uint type decimal.
   650  		{uint64(0), uint64(0), 0},
   651  		{uint64(1), uint64(0), 1},
   652  		{uint64(0), uint64(1), -1},
   653  		{uint64(math.MaxInt8), uint64(math.MaxInt16), -1},
   654  		{uint64(math.MaxUint32), uint64(math.MaxInt32), 1},
   655  		{uint64(math.MaxUint8), uint64(math.MaxInt8), 1},
   656  		{uint64(math.MaxUint16), uint64(math.MaxInt32), -1},
   657  		{uint64(math.MaxUint64), uint64(math.MaxInt64), 1},
   658  		{uint64(math.MaxInt64), uint64(math.MaxUint32), 1},
   659  		{uint64(math.MaxUint64), uint64(0), 1},
   660  		{uint64(0), uint64(math.MaxUint64), -1},
   661  	}
   662  
   663  	for _, t := range tblCmp {
   664  		m1, err := mysql.ConvertToDecimal(t.Arg1)
   665  		c.Assert(err, IsNil)
   666  		m2, err := mysql.ConvertToDecimal(t.Arg2)
   667  		c.Assert(err, IsNil)
   668  
   669  		b1, err := EncodeKey(nil, types.NewDatum(m1))
   670  		c.Assert(err, IsNil)
   671  		b2, err := EncodeKey(nil, types.NewDatum(m2))
   672  		c.Assert(err, IsNil)
   673  
   674  		ret := bytes.Compare(b1, b2)
   675  		c.Assert(ret, Equals, t.Ret)
   676  	}
   677  
   678  	floats := []float64{-123.45, -123.40, -23.45, -1.43, -0.93, -0.4333, -0.068,
   679  		-0.0099, 0, 0.001, 0.0012, 0.12, 1.2, 1.23, 123.3, 2424.242424}
   680  	var decs [][]byte
   681  	for i := range floats {
   682  		dec := mysql.NewDecimalFromFloat(floats[i])
   683  		decs = append(decs, EncodeDecimal(nil, dec))
   684  	}
   685  	for i := 0; i < len(decs)-1; i++ {
   686  		cmp := bytes.Compare(decs[i], decs[i+1])
   687  		c.Assert(cmp, LessEqual, 0)
   688  	}
   689  }