2021-07-17 15:37:33 +00:00
|
|
|
package util_test
|
2018-03-25 10:45:54 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"testing"
|
|
|
|
|
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-25 10:45:54 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-11-24 14:55:50 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-09-25 10:09:55 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2018-03-25 10:45:54 +00:00
|
|
|
)
|
|
|
|
|
2018-05-09 05:20:16 +00:00
|
|
|
func TestUint160UnmarshalJSON(t *testing.T) {
|
2020-03-23 09:46:42 +00:00
|
|
|
str := "0263c1de100292813b5e075e585acc1bae963b2d"
|
2021-07-17 15:37:33 +00:00
|
|
|
expected, err := util.Uint160DecodeStringLE(str)
|
2019-11-24 14:55:50 +00:00
|
|
|
assert.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.Uint160
|
2018-11-26 15:56:45 +00:00
|
|
|
|
2019-11-24 14:55:50 +00:00
|
|
|
assert.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:43:13 +00:00
|
|
|
|
|
|
|
assert.Error(t, u2.UnmarshalJSON([]byte(`123`)))
|
2018-05-09 05:20:16 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 10:30:01 +00:00
|
|
|
func TestUint160UnmarshalYAML(t *testing.T) {
|
|
|
|
str := "0263c1de100292813b5e075e585acc1bae963b2d"
|
|
|
|
expected, err := util.Uint160DecodeStringLE(str)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
var u1, u2 util.Uint160
|
|
|
|
require.NoError(t, yaml.Unmarshal([]byte(`"`+str+`"`), &u1))
|
|
|
|
require.Equal(t, expected, u1)
|
|
|
|
|
|
|
|
data, err := yaml.Marshal(u1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, yaml.Unmarshal(data, &u2))
|
|
|
|
require.Equal(t, expected, u2)
|
|
|
|
|
|
|
|
require.Error(t, yaml.Unmarshal([]byte(`[]`), &u1))
|
|
|
|
}
|
|
|
|
|
2018-03-25 10:45:54 +00:00
|
|
|
func TestUInt160DecodeString(t *testing.T) {
|
|
|
|
hexStr := "2d3b96ae1bcc5a585e075e3b81920210dec16302"
|
2021-07-17 15:37:33 +00:00
|
|
|
val, err := util.Uint160DecodeStringBE(hexStr)
|
2019-11-24 14:55:50 +00:00
|
|
|
assert.NoError(t, err)
|
2018-03-25 10:45:54 +00:00
|
|
|
assert.Equal(t, hexStr, val.String())
|
2019-11-25 08:43:13 +00:00
|
|
|
|
2021-07-17 15:37:33 +00:00
|
|
|
valLE, err := util.Uint160DecodeStringLE(hexStr)
|
2019-12-03 13:14:44 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, val, valLE.Reverse())
|
|
|
|
|
2021-07-17 15:37:33 +00:00
|
|
|
_, err = util.Uint160DecodeStringBE(hexStr[1:])
|
2019-11-25 08:43:13 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
|
2021-07-17 15:37:33 +00:00
|
|
|
_, err = util.Uint160DecodeStringLE(hexStr[1:])
|
2019-12-03 13:14:44 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
|
2019-11-25 08:43:13 +00:00
|
|
|
hexStr = "zz3b96ae1bcc5a585e075e3b81920210dec16302"
|
2021-07-17 15:37:33 +00:00
|
|
|
_, err = util.Uint160DecodeStringBE(hexStr)
|
2019-11-25 08:43:13 +00:00
|
|
|
assert.Error(t, err)
|
2019-12-03 13:14:44 +00:00
|
|
|
|
2021-07-17 15:37:33 +00:00
|
|
|
_, err = util.Uint160DecodeStringLE(hexStr)
|
2019-12-03 13:14:44 +00:00
|
|
|
assert.Error(t, err)
|
2018-03-25 10:45:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUint160DecodeBytes(t *testing.T) {
|
|
|
|
hexStr := "2d3b96ae1bcc5a585e075e3b81920210dec16302"
|
|
|
|
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.Uint160DecodeBytesBE(b)
|
2019-11-24 14:55:50 +00:00
|
|
|
assert.NoError(t, err)
|
2018-03-25 10:45:54 +00:00
|
|
|
assert.Equal(t, hexStr, val.String())
|
2019-11-25 08:43:13 +00:00
|
|
|
|
2021-07-17 15:37:33 +00:00
|
|
|
valLE, err := util.Uint160DecodeBytesLE(b)
|
2019-12-03 13:12:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, val, valLE.Reverse())
|
|
|
|
|
2021-07-17 15:37:33 +00:00
|
|
|
_, err = util.Uint160DecodeBytesLE(b[1:])
|
2019-12-03 13:12:26 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
|
2021-07-17 15:37:33 +00:00
|
|
|
_, err = util.Uint160DecodeBytesBE(b[1:])
|
2019-11-25 08:43:13 +00:00
|
|
|
assert.Error(t, err)
|
2018-03-25 10:45:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUInt160Equals(t *testing.T) {
|
|
|
|
a := "2d3b96ae1bcc5a585e075e3b81920210dec16302"
|
|
|
|
b := "4d3b96ae1bcc5a585e075e3b81920210dec16302"
|
|
|
|
|
2021-07-17 15:37:33 +00:00
|
|
|
ua, err := util.Uint160DecodeStringBE(a)
|
2019-11-24 14:55:50 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-07-17 15:37:33 +00:00
|
|
|
ub, err := util.Uint160DecodeStringBE(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)
|
2018-03-25 10:45:54 +00:00
|
|
|
}
|
2019-08-22 12:35:18 +00:00
|
|
|
|
2019-09-30 14:39:42 +00:00
|
|
|
func TestUInt160Less(t *testing.T) {
|
|
|
|
a := "2d3b96ae1bcc5a585e075e3b81920210dec16302"
|
|
|
|
b := "2d3b96ae1bcc5a585e075e3b81920210dec16303"
|
|
|
|
|
2021-07-17 15:37:33 +00:00
|
|
|
ua, err := util.Uint160DecodeStringBE(a)
|
2019-09-30 14:39:42 +00:00
|
|
|
assert.Nil(t, err)
|
2021-07-17 15:37:33 +00:00
|
|
|
ua2, err := util.Uint160DecodeStringBE(a)
|
2019-09-30 14:39:42 +00:00
|
|
|
assert.Nil(t, err)
|
2021-07-17 15:37:33 +00:00
|
|
|
ub, err := util.Uint160DecodeStringBE(b)
|
2019-09-30 14:39:42 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, true, ua.Less(ub))
|
|
|
|
assert.Equal(t, false, ua.Less(ua2))
|
|
|
|
assert.Equal(t, false, ub.Less(ua))
|
2024-08-26 16:07:37 +00:00
|
|
|
assert.Equal(t, -1, ua.Compare(ub))
|
|
|
|
assert.Equal(t, 1, ub.Compare(ua))
|
|
|
|
assert.Equal(t, 0, ua.Compare(ua))
|
|
|
|
assert.Equal(t, 0, ub.Compare(ub))
|
2019-09-30 14:39:42 +00:00
|
|
|
}
|
|
|
|
|
2019-08-22 12:35:18 +00:00
|
|
|
func TestUInt160String(t *testing.T) {
|
|
|
|
hexStr := "b28427088a3729b2536d10122960394e8be6721f"
|
|
|
|
hexRevStr := "1f72e68b4e39602912106d53b229378a082784b2"
|
|
|
|
|
2021-07-17 15:37:33 +00:00
|
|
|
val, err := util.Uint160DecodeStringBE(hexStr)
|
2019-08-22 12:35:18 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, hexStr, val.String())
|
2019-11-27 09:20:31 +00:00
|
|
|
assert.Equal(t, hexRevStr, val.StringLE())
|
2019-08-22 12:35:18 +00:00
|
|
|
}
|
2019-12-03 13:07:15 +00:00
|
|
|
|
|
|
|
func TestUint160_Reverse(t *testing.T) {
|
|
|
|
hexStr := "b28427088a3729b2536d10122960394e8be6721f"
|
2021-07-17 15:37:33 +00:00
|
|
|
val, err := util.Uint160DecodeStringBE(hexStr)
|
2019-12-03 13:07:15 +00:00
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, hexStr, val.Reverse().StringLE())
|
|
|
|
assert.Equal(t, val, val.Reverse().Reverse())
|
|
|
|
}
|