1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
package runtime
import (
"math/big"
"testing"
"github.com/dexon-foundation/decimal"
"github.com/stretchr/testify/suite"
dexCommon "github.com/dexon-foundation/dexon/common"
"github.com/dexon-foundation/dexon/core/vm"
"github.com/dexon-foundation/dexon/core/vm/sqlvm/ast"
"github.com/dexon-foundation/dexon/core/vm/sqlvm/common"
)
func TestFunction(t *testing.T) {
suite.Run(t, new(FunctionSuite))
}
type FunctionSuite struct {
suite.Suite
}
var (
hash1 = dexCommon.BigToHash(big.NewInt(1))
hash255 = dexCommon.BigToHash(big.NewInt(255))
)
var mockNumberHashTable = map[uint64]dexCommon.Hash{1: hash1, 255: hash255}
func mockGetHashFunc(u uint64) dexCommon.Hash { return mockNumberHashTable[u] }
func (s *FunctionSuite) TestFnBlockHash() {
type blockHashCase struct {
Name string
Ops []*Operand
Length uint64
Res [][]byte
Cur *big.Int
Err error
}
testcases := []blockHashCase{
{"Immediate OP", []*Operand{
{IsImmediate: true, Meta: nil, Data: []Tuple{{&Raw{Value: decimal.New(1, 0)}}}},
}, 2, [][]byte{hash1.Bytes(), hash1.Bytes()}, big.NewInt(255), nil},
{"OP", []*Operand{
{IsImmediate: false, Meta: nil, Data: []Tuple{
{&Raw{Value: decimal.New(255, 0)}},
{&Raw{Value: decimal.New(515, 0)}},
}},
}, 2, [][]byte{hash255.Bytes(), make([]byte, 32)}, big.NewInt(256), nil},
{"Older than 257 block", []*Operand{
{IsImmediate: false, Meta: nil, Data: []Tuple{
{&Raw{Value: decimal.New(1, 0)}},
}},
}, 1, [][]byte{make([]byte, 32)}, big.NewInt(512), nil},
}
callFn := func(c blockHashCase) (*Operand, error) {
return fnBlockHash(
&common.Context{
Context: vm.Context{
GetHash: mockGetHashFunc,
BlockNumber: c.Cur,
},
},
c.Ops,
c.Length,
)
}
meta := []ast.DataType{ast.ComposeDataType(ast.DataTypeMajorFixedBytes, 3)}
for idx, tCase := range testcases {
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().Equal(
tCase.Res[i], r.Data[i][0].Bytes,
"TestCase Index: %v. Data Index: %v. Value not equal: %v != %v",
idx, i, tCase.Res[i], r.Data[i][0].Bytes)
}
}
}
|