2021-07-17 15:37:33 +00:00
|
|
|
package util_test
|
2018-02-15 15:35:49 +00:00
|
|
|
|
|
|
|
import (
|
2018-03-02 15:24:09 +00:00
|
|
|
"encoding/hex"
|
2018-02-15 15:35:49 +00:00
|
|
|
"testing"
|
2018-03-04 13:56:49 +00:00
|
|
|
|
2020-11-23 11:09:00 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/internal/testserdes"
|
2021-07-17 15:37:33 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2018-03-04 13:56:49 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-11-14 08:07:23 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-02-15 15:35:49 +00:00
|
|
|
)
|
|
|
|
|
2018-05-09 05:20:16 +00:00
|
|
|
func TestUint256UnmarshalJSON(t *testing.T) {
|
|
|
|
str := "f037308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d"
|
2021-07-17 15:37:33 +00:00
|
|
|
expected, err := util.Uint256DecodeStringLE(str)
|
2019-11-24 14:55:50 +00:00
|
|
|
require.NoError(t, err)
|
2018-05-09 05:20:16 +00:00
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// UnmarshalJSON decodes hex-strings
|
2021-07-17 15:37:33 +00:00
|
|
|
var u1, u2 util.Uint256
|
2018-11-26 15:56:45 +00:00
|
|
|
|
2019-11-24 14:55:50 +00:00
|
|
|
require.NoError(t, u1.UnmarshalJSON([]byte(`"`+str+`"`)))
|
2018-05-09 05:20:16 +00:00
|
|
|
assert.True(t, expected.Equals(u1))
|
|
|
|
|
2020-03-26 14:43:24 +00:00
|
|
|
testserdes.MarshalUnmarshalJSON(t, &expected, &u2)
|
2019-11-25 08:51:34 +00:00
|
|
|
|
|
|
|
// UnmarshalJSON does not accepts numbers
|
|
|
|
assert.Error(t, u2.UnmarshalJSON([]byte("123")))
|
2018-05-09 05:20:16 +00:00
|
|
|
}
|
|
|
|
|
2018-03-02 15:24:09 +00:00
|
|
|
func TestUint256DecodeString(t *testing.T) {
|
|
|
|
hexStr := "f037308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d"
|
2021-07-17 15:37:33 +00:00
|
|
|
val, err := util.Uint256DecodeStringLE(hexStr)
|
2019-11-24 14:55:50 +00:00
|
|
|
require.NoError(t, err)
|
2019-11-27 09:23:18 +00:00
|
|
|
assert.Equal(t, hexStr, val.StringLE())
|
2019-11-25 08:51:34 +00:00
|
|
|
|
2021-07-17 15:37:33 +00:00
|
|
|
valBE, err := util.Uint256DecodeStringBE(hexStr)
|
2019-12-03 13:17:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, val, valBE.Reverse())
|
|
|
|
|
2019-11-25 08:51:34 +00:00
|
|
|
bs, err := hex.DecodeString(hexStr)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-07-17 15:37:33 +00:00
|
|
|
val1, err := util.Uint256DecodeBytesBE(bs)
|
2019-11-25 08:51:34 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, hexStr, val1.String())
|
|
|
|
assert.Equal(t, val, val1.Reverse())
|
|
|
|
|
2021-07-17 15:37:33 +00:00
|
|
|
_, err = util.Uint256DecodeStringLE(hexStr[1:])
|
2019-11-25 08:51:34 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
|
2021-07-17 15:37:33 +00:00
|
|
|
_, err = util.Uint256DecodeStringBE(hexStr[1:])
|
2019-12-03 13:17:04 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
|
2019-11-25 08:51:34 +00:00
|
|
|
hexStr = "zzz7308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d"
|
2021-07-17 15:37:33 +00:00
|
|
|
_, err = util.Uint256DecodeStringLE(hexStr)
|
2019-11-25 08:51:34 +00:00
|
|
|
assert.Error(t, err)
|
2019-12-03 13:17:04 +00:00
|
|
|
|
2021-07-17 15:37:33 +00:00
|
|
|
_, err = util.Uint256DecodeStringBE(hexStr)
|
2019-12-03 13:17:04 +00:00
|
|
|
assert.Error(t, err)
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|
|
|
|
|
2018-03-02 15:24:09 +00:00
|
|
|
func TestUint256DecodeBytes(t *testing.T) {
|
|
|
|
hexStr := "f037308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d"
|
|
|
|
b, err := hex.DecodeString(hexStr)
|
2019-11-24 14:55:50 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-07-17 15:37:33 +00:00
|
|
|
val, err := util.Uint256DecodeBytesLE(b)
|
2019-11-24 14:55:50 +00:00
|
|
|
require.NoError(t, err)
|
2019-11-27 09:23:18 +00:00
|
|
|
assert.Equal(t, hexStr, val.StringLE())
|
2019-11-25 08:51:34 +00:00
|
|
|
|
2021-07-17 15:37:33 +00:00
|
|
|
_, err = util.Uint256DecodeBytesBE(b[1:])
|
2019-11-25 08:51:34 +00:00
|
|
|
assert.Error(t, err)
|
2018-03-02 15:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUInt256Equals(t *testing.T) {
|
|
|
|
a := "f037308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d"
|
|
|
|
b := "e287c5b29a1b66092be6803c59c765308ac20287e1b4977fd399da5fc8f66ab5"
|
|
|
|
|
2021-07-17 15:37:33 +00:00
|
|
|
ua, err := util.Uint256DecodeStringLE(a)
|
2019-11-24 14:55:50 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-07-17 15:37:33 +00:00
|
|
|
ub, err := util.Uint256DecodeStringLE(b)
|
2019-11-24 14:55:50 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.False(t, ua.Equals(ub), "%s and %s cannot be equal", ua, ub)
|
|
|
|
assert.True(t, ua.Equals(ua), "%s and %s must be equal", ua, ua)
|
2024-08-26 17:42:09 +00:00
|
|
|
assert.Zero(t, ua.Compare(ua), "%s and %s must be equal", ua, ua)
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|
2019-11-14 08:07:23 +00:00
|
|
|
|
|
|
|
func TestUint256_Serializable(t *testing.T) {
|
2021-07-17 15:37:33 +00:00
|
|
|
a := util.Uint256{
|
2019-11-14 08:07:23 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2021-07-17 15:37:33 +00:00
|
|
|
var b util.Uint256
|
2020-03-26 14:43:24 +00:00
|
|
|
testserdes.EncodeDecodeBinary(t, &a, &b)
|
2019-11-14 08:07:23 +00:00
|
|
|
}
|
2021-08-17 13:50:26 +00:00
|
|
|
|
|
|
|
func BenchmarkUint256DecodeStringLE(b *testing.B) {
|
|
|
|
a := "f037308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d"
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
2024-08-30 18:41:02 +00:00
|
|
|
for range b.N {
|
2021-08-17 13:50:26 +00:00
|
|
|
_, err := util.Uint256DecodeStringLE(a)
|
|
|
|
if err != nil {
|
|
|
|
b.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|