aboutsummaryrefslogtreecommitdiffstats
path: root/core/vm/sqlvm/runtime/functions_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'core/vm/sqlvm/runtime/functions_test.go')
-rw-r--r--core/vm/sqlvm/runtime/functions_test.go57
1 files changed, 57 insertions, 0 deletions
diff --git a/core/vm/sqlvm/runtime/functions_test.go b/core/vm/sqlvm/runtime/functions_test.go
index fe8fa0791..a507403dc 100644
--- a/core/vm/sqlvm/runtime/functions_test.go
+++ b/core/vm/sqlvm/runtime/functions_test.go
@@ -92,3 +92,60 @@ func (s *FunctionSuite) TestFnBlockHash() {
}
}
}
+
+func (s *FunctionSuite) TestFnBlockNumber() {
+ type blockNumberCase struct {
+ Name string
+ RawNum *big.Int
+ Length uint64
+ ResNum decimal.Decimal
+ Err error
+ AsserPanic bool
+ }
+
+ testcases := []blockNumberCase{
+ {"number 1 with length 1", big.NewInt(1), 1, decimal.New(1, 0), nil, false},
+ {"number 10 with length 10", big.NewInt(10), 10, decimal.New(10, 0), nil, false},
+ {"number 1 with length 0", big.NewInt(1), 0, decimal.New(1, 0), nil, false},
+ {"panic on invalid context", nil, 0, decimal.New(1, 0), nil, true},
+ }
+
+ callFn := func(c blockNumberCase) (*Operand, error) {
+ return fnBlockNumber(
+ &common.Context{
+ Context: vm.Context{BlockNumber: c.RawNum},
+ },
+ nil,
+ c.Length)
+ }
+
+ meta := []ast.DataType{ast.ComposeDataType(ast.DataTypeMajorUint, 31)}
+
+ for idx, tCase := range testcases {
+ if tCase.AsserPanic {
+ s.Require().Panicsf(
+ func() { callFn(tCase) },
+ "Index: %v. Not Panic on '%v'", idx, tCase.Name,
+ )
+ } else {
+ r, err := callFn(tCase)
+ s.Require().Equal(
+ tCase.Err, err,
+ "Index: %v. Error not expected: %v != %v", idx, tCase.Err, err)
+ s.Require().Equal(
+ meta, r.Meta,
+ "Index: %v. Meta not equal: %v != %v", idx, meta, r.Meta)
+ s.Require().Equal(
+ uint64(len(r.Data)), tCase.Length,
+ "Index: %v. Length not equal: %v != %v", idx, len(r.Data), tCase.Length)
+
+ for i := 0; i < len(r.Data); i++ {
+ s.Require().True(
+ tCase.ResNum.Equal(r.Data[i][0].Value),
+ "Index: %v Data index: %v. Value not equal: %v != %v",
+ idx, i, tCase.ResNum, r.Data[i][0].Value)
+ }
+ }
+ }
+}
+