mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-17 18:30:45 +00:00
test: updated test cases to cover hex and number support for XDPoS_getV2BlockByNumber
This commit is contained in:
parent
8d0e24b2e2
commit
78e82ac8a9
3 changed files with 30 additions and 11 deletions
|
|
@ -185,7 +185,6 @@ func EncodeBig(bigint *big.Int) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add more comprehensive check to validate a hex string
|
|
||||||
func IsValidHexString(input string) bool {
|
func IsValidHexString(input string) bool {
|
||||||
if !has0xPrefix(input) {
|
if !has0xPrefix(input) {
|
||||||
return false
|
return false
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,11 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type hexValidityTest struct {
|
||||||
|
input string
|
||||||
|
want bool
|
||||||
|
}
|
||||||
|
|
||||||
type marshalTest struct {
|
type marshalTest struct {
|
||||||
input interface{}
|
input interface{}
|
||||||
want string
|
want string
|
||||||
|
|
@ -134,6 +139,12 @@ var (
|
||||||
{input: `0xbbb`, want: uint64(0xbbb)},
|
{input: `0xbbb`, want: uint64(0xbbb)},
|
||||||
{input: `0xffffffffffffffff`, want: uint64(0xffffffffffffffff)},
|
{input: `0xffffffffffffffff`, want: uint64(0xffffffffffffffff)},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hexStringValidityTest = []hexValidityTest{
|
||||||
|
{"0x", false},
|
||||||
|
{"asdcc", false},
|
||||||
|
{"0x00000102", true},
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestEncode(t *testing.T) {
|
func TestEncode(t *testing.T) {
|
||||||
|
|
@ -202,6 +213,15 @@ func TestDecodeUint64(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIsValidHexString(t *testing.T) {
|
||||||
|
for _, test := range hexStringValidityTest {
|
||||||
|
actual := IsValidHexString(test.input)
|
||||||
|
if actual != test.want {
|
||||||
|
t.Errorf("input %s: value mismatch: got %t, want %t", test.input, actual, test.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkEncodeBig(b *testing.B) {
|
func BenchmarkEncodeBig(b *testing.B) {
|
||||||
for _, bench := range encodeBigTests {
|
for _, bench := range encodeBigTests {
|
||||||
b.Run(bench.want, func(b *testing.B) {
|
b.Run(bench.want, func(b *testing.B) {
|
||||||
|
|
|
||||||
|
|
@ -42,16 +42,16 @@ func TestBlockNumberJSONUnmarshal(t *testing.T) {
|
||||||
6: {`"0x12"`, false, BlockNumber(18)},
|
6: {`"0x12"`, false, BlockNumber(18)},
|
||||||
7: {`"0x7fffffffffffffff"`, false, BlockNumber(math.MaxInt64)},
|
7: {`"0x7fffffffffffffff"`, false, BlockNumber(math.MaxInt64)},
|
||||||
8: {`"0x8000000000000000"`, true, BlockNumber(0)},
|
8: {`"0x8000000000000000"`, true, BlockNumber(0)},
|
||||||
9: {"0", true, BlockNumber(0)},
|
9: {`"ff"`, true, BlockNumber(0)},
|
||||||
10: {`"ff"`, true, BlockNumber(0)},
|
10: {`"pending"`, false, PendingBlockNumber},
|
||||||
11: {`"pending"`, false, PendingBlockNumber},
|
11: {`"latest"`, false, LatestBlockNumber},
|
||||||
12: {`"latest"`, false, LatestBlockNumber},
|
12: {`"earliest"`, false, EarliestBlockNumber},
|
||||||
13: {`"earliest"`, false, EarliestBlockNumber},
|
13: {`"committed"`, false, CommittedBlockNumber},
|
||||||
14: {`"committed"`, false, CommittedBlockNumber},
|
14: {`"finalized"`, false, CommittedBlockNumber},
|
||||||
15: {`"finalized"`, false, CommittedBlockNumber},
|
15: {`someString`, true, BlockNumber(0)},
|
||||||
16: {`someString`, true, BlockNumber(0)},
|
16: {`""`, true, BlockNumber(0)},
|
||||||
17: {`""`, true, BlockNumber(0)},
|
17: {``, true, BlockNumber(0)},
|
||||||
18: {``, true, BlockNumber(0)},
|
18: {`88439993`, false, BlockNumber(88439993)},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, test := range tests {
|
for i, test := range tests {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue