neo-go/pkg/core/state/nep5_test.go

74 lines
1.6 KiB
Go
Raw Normal View History

2020-03-05 07:45:50 +00:00
package state
import (
"math/big"
2020-03-05 07:45:50 +00:00
"math/rand"
"testing"
2020-03-05 12:16:03 +00:00
"time"
2020-03-05 07:45:50 +00:00
"github.com/nspcc-dev/neo-go/pkg/internal/random"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
2020-03-05 07:45:50 +00:00
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/require"
)
2020-03-05 12:16:03 +00:00
func TestNEP5TransferLog_Append(t *testing.T) {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
expected := []*NEP5Transfer{
randomTransfer(r),
randomTransfer(r),
randomTransfer(r),
randomTransfer(r),
2020-03-05 12:16:03 +00:00
}
lg := new(NEP5TransferLog)
for _, tr := range expected {
require.NoError(t, lg.Append(tr))
}
require.Equal(t, len(expected), lg.Size())
i := len(expected) - 1
cont, err := lg.ForEach(func(tr *NEP5Transfer) (bool, error) {
2020-03-05 12:16:03 +00:00
require.Equal(t, expected[i], tr)
i--
return true, nil
2020-03-05 12:16:03 +00:00
})
require.NoError(t, err)
require.True(t, cont)
2020-03-05 12:16:03 +00:00
}
2020-03-05 07:45:50 +00:00
func TestNEP5Tracker_EncodeBinary(t *testing.T) {
expected := &NEP5Tracker{
Balance: *big.NewInt(int64(rand.Uint64())),
2020-03-05 07:45:50 +00:00
LastUpdatedBlock: rand.Uint32(),
}
testserdes.EncodeDecodeBinary(t, expected, new(NEP5Tracker))
2020-03-05 07:45:50 +00:00
}
2020-03-05 14:11:58 +00:00
func TestNEP5Transfer_DecodeBinary(t *testing.T) {
expected := &NEP5Transfer{
Asset: 123,
2020-03-05 14:11:58 +00:00
From: util.Uint160{5, 6, 7},
To: util.Uint160{8, 9, 10},
Amount: *big.NewInt(42),
2020-03-05 14:11:58 +00:00
Block: 12345,
Timestamp: 54321,
Tx: util.Uint256{8, 5, 3},
}
testserdes.EncodeDecodeBinary(t, expected, new(NEP5Transfer))
2020-03-05 14:11:58 +00:00
}
func randomTransfer(r *rand.Rand) *NEP5Transfer {
return &NEP5Transfer{
Amount: *big.NewInt(int64(r.Uint64())),
2020-03-05 12:16:03 +00:00
Block: r.Uint32(),
Asset: int32(random.Int(10, 10000000)),
From: random.Uint160(),
To: random.Uint160(),
Tx: random.Uint256(),
2020-03-05 12:16:03 +00:00
}
}