diff options
Diffstat (limited to 'core/vm/sqlvm/ast/types_test.go')
-rw-r--r-- | core/vm/sqlvm/ast/types_test.go | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/core/vm/sqlvm/ast/types_test.go b/core/vm/sqlvm/ast/types_test.go index aa726c7ba..8280cf73f 100644 --- a/core/vm/sqlvm/ast/types_test.go +++ b/core/vm/sqlvm/ast/types_test.go @@ -3,6 +3,7 @@ package ast import ( "testing" + "github.com/dexon-foundation/dexon/common" "github.com/shopspring/decimal" "github.com/stretchr/testify/suite" ) @@ -152,6 +153,41 @@ func (s *TypesTestSuite) TestEncodeAndDecodeDecimal() { 3) } +func (s *TypesTestSuite) TestGetMinMax() { + decAddressMax := decimal.New(2, 0).Pow(decimal.New(common.AddressLength*8, 0)).Sub(decOne) + testcases := []struct { + Name string + In DataType + Min, Max decimal.Decimal + Err error + }{ + {"Bool", ComposeDataType(DataTypeMajorBool, 0), decFalse, decTrue, nil}, + {"Address", ComposeDataType(DataTypeMajorAddress, 0), decimal.Zero, decAddressMax, nil}, + {"Int8", ComposeDataType(DataTypeMajorInt, 0), decimal.New(-128, 0), decimal.New(127, 0), nil}, + {"Int16", ComposeDataType(DataTypeMajorInt, 1), decimal.New(-32768, 0), decimal.New(32767, 0), nil}, + {"UInt8", ComposeDataType(DataTypeMajorUint, 0), decimal.Zero, decimal.New(255, 0), nil}, + {"UInt16", ComposeDataType(DataTypeMajorUint, 1), decimal.Zero, decimal.New(65535, 0), nil}, + {"Bytes1", ComposeDataType(DataTypeMajorFixedBytes, 0), decimal.Zero, decimal.New(255, 0), nil}, + {"Bytes2", ComposeDataType(DataTypeMajorFixedBytes, 1), decimal.Zero, decimal.New(65535, 0), nil}, + {"Dynamic Bytes", ComposeDataType(DataTypeMajorDynamicBytes, 0), decimal.Zero, decimal.Zero, ErrGetMinMax}, + } + + var ( + min, max decimal.Decimal + err error + ) + for _, t := range testcases { + min, max, err = GetMinMax(t.In) + s.Require().Equal(t.Err, err, "Case: %v. Error not equal: %v != %v", t.Name, t.Err, err) + if t.Err != nil { + continue + } + + s.Require().True(t.Min.Equal(min), "Case: %v. Min not equal: %v != %v", t.Name, t.Min, min) + s.Require().True(t.Max.Equal(max), "Case: %v. Max not equal: %v != %v", t.Name, t.Max, max) + } +} + func TestTypes(t *testing.T) { suite.Run(t, new(TypesTestSuite)) } |