forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gas_test.go
36 lines (29 loc) · 878 Bytes
/
gas_test.go
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
package types
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestGasMeter(t *testing.T) {
cases := []struct {
limit Gas
usage []Gas
}{
{10, []Gas{1, 2, 3, 4}},
{1000, []Gas{40, 30, 20, 10, 900}},
{100000, []Gas{99999, 1}},
{100000000, []Gas{50000000, 40000000, 10000000}},
{65535, []Gas{32768, 32767}},
{65536, []Gas{32768, 32767, 1}},
}
for tcnum, tc := range cases {
meter := NewGasMeter(tc.limit)
used := int64(0)
for unum, usage := range tc.usage {
used += usage
require.NotPanics(t, func() { meter.ConsumeGas(usage, "") }, "Not exceeded limit but panicked. tc #%d, usage #%d", tcnum, unum)
require.Equal(t, used, meter.GasConsumed(), "Gas consumption not match. tc #%d, usage #%d", tcnum, unum)
}
require.Panics(t, func() { meter.ConsumeGas(1, "") }, "Exceeded but not panicked. tc #%d", tcnum)
break
}
}