github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/soliton/codec/decimal_test.go (about)

     1  // Copyright 2020 WHTCORPS INC, 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  	. "github.com/whtcorpsinc/check"
    18  	"github.com/whtcorpsinc/milevadb/types"
    19  	"github.com/whtcorpsinc/milevadb/soliton/testleak"
    20  )
    21  
    22  var _ = Suite(&testDecimalSuite{})
    23  
    24  type testDecimalSuite struct {
    25  }
    26  
    27  func (s *testDecimalSuite) TestDecimalCodec(c *C) {
    28  	defer testleak.AfterTest(c)()
    29  	inputs := []struct {
    30  		Input float64
    31  	}{
    32  		{float64(123400)},
    33  		{float64(1234)},
    34  		{float64(12.34)},
    35  		{float64(0.1234)},
    36  		{float64(0.01234)},
    37  		{float64(-0.1234)},
    38  		{float64(-0.01234)},
    39  		{float64(12.3400)},
    40  		{float64(-12.34)},
    41  		{float64(0.00000)},
    42  		{float64(0)},
    43  		{float64(-0.0)},
    44  		{float64(-0.000)},
    45  	}
    46  
    47  	for _, input := range inputs {
    48  		v := types.NewDecFromFloatForTest(input.Input)
    49  		causet := types.NewCauset(v)
    50  
    51  		b, err := EncodeDecimal([]byte{}, causet.GetMysqlDecimal(), causet.Length(), causet.Frac())
    52  		c.Assert(err, IsNil)
    53  		_, d, prec, frac, err := DecodeDecimal(b)
    54  		if causet.Length() != 0 {
    55  			c.Assert(prec, Equals, causet.Length())
    56  			c.Assert(frac, Equals, causet.Frac())
    57  		} else {
    58  			prec1, frac1 := causet.GetMysqlDecimal().PrecisionAndFrac()
    59  			c.Assert(prec, Equals, prec1)
    60  			c.Assert(frac, Equals, frac1)
    61  		}
    62  		c.Assert(err, IsNil)
    63  		c.Assert(v.Compare(d), Equals, 0)
    64  	}
    65  }
    66  
    67  func (s *testDecimalSuite) TestFrac(c *C) {
    68  	defer testleak.AfterTest(c)()
    69  	inputs := []struct {
    70  		Input *types.MyDecimal
    71  	}{
    72  		{types.NewDecFromInt(3)},
    73  		{types.NewDecFromFloatForTest(0.03)},
    74  	}
    75  	for _, v := range inputs {
    76  		testFrac(c, v.Input)
    77  	}
    78  }
    79  
    80  func testFrac(c *C, v *types.MyDecimal) {
    81  	var d1 types.Causet
    82  	d1.SetMysqlDecimal(v)
    83  
    84  	b, err := EncodeDecimal([]byte{}, d1.GetMysqlDecimal(), d1.Length(), d1.Frac())
    85  	c.Assert(err, IsNil)
    86  	_, dec, _, _, err := DecodeDecimal(b)
    87  	c.Assert(err, IsNil)
    88  	c.Assert(dec.String(), Equals, v.String())
    89  }