2018-03-02 15:24:09 +00:00
|
|
|
package util
|
2018-03-25 10:45:54 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-11-24 14:55:50 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-03-25 10:45:54 +00:00
|
|
|
)
|
|
|
|
|
2018-05-09 05:20:16 +00:00
|
|
|
func TestUint160UnmarshalJSON(t *testing.T) {
|
|
|
|
str := "2d3b96ae1bcc5a585e075e3b81920210dec16302"
|
2018-11-26 15:56:45 +00:00
|
|
|
expected, err := Uint160DecodeString(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
|
2018-11-26 15:56:45 +00:00
|
|
|
var u1, u2 Uint160
|
|
|
|
|
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))
|
|
|
|
|
2018-11-26 15:56:45 +00:00
|
|
|
s, err := expected.MarshalJSON()
|
2019-11-24 14:55:50 +00:00
|
|
|
require.NoError(t, err)
|
2018-11-26 15:56:45 +00:00
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// UnmarshalJSON decodes hex-strings prefixed by 0x
|
2019-11-24 14:55:50 +00:00
|
|
|
assert.NoError(t, u2.UnmarshalJSON(s))
|
2018-11-26 15:56:45 +00:00
|
|
|
assert.True(t, expected.Equals(u1))
|
2019-11-25 08:43:13 +00:00
|
|
|
|
|
|
|
assert.Error(t, u2.UnmarshalJSON([]byte(`123`)))
|
2018-05-09 05:20:16 +00:00
|
|
|
}
|
|
|
|
|
2018-03-25 10:45:54 +00:00
|
|
|
func TestUInt160DecodeString(t *testing.T) {
|
|
|
|
hexStr := "2d3b96ae1bcc5a585e075e3b81920210dec16302"
|
|
|
|
val, err := Uint160DecodeString(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
|
|
|
|
|
|
|
_, err = Uint160DecodeString(hexStr[1:])
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
hexStr = "zz3b96ae1bcc5a585e075e3b81920210dec16302"
|
|
|
|
_, err = Uint160DecodeString(hexStr)
|
|
|
|
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)
|
|
|
|
|
2018-03-25 10:45:54 +00:00
|
|
|
val, err := Uint160DecodeBytes(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
|
|
|
|
|
|
|
_, err = Uint160DecodeBytes(b[1:])
|
|
|
|
assert.Error(t, err)
|
2018-03-25 10:45:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUInt160Equals(t *testing.T) {
|
|
|
|
a := "2d3b96ae1bcc5a585e075e3b81920210dec16302"
|
|
|
|
b := "4d3b96ae1bcc5a585e075e3b81920210dec16302"
|
|
|
|
|
|
|
|
ua, err := Uint160DecodeString(a)
|
2019-11-24 14:55:50 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-03-25 10:45:54 +00:00
|
|
|
ub, err := Uint160DecodeString(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"
|
|
|
|
|
|
|
|
ua, err := Uint160DecodeString(a)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
ua2, err := Uint160DecodeString(a)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
ub, err := Uint160DecodeString(b)
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2019-08-22 12:35:18 +00:00
|
|
|
func TestUInt160String(t *testing.T) {
|
|
|
|
hexStr := "b28427088a3729b2536d10122960394e8be6721f"
|
|
|
|
hexRevStr := "1f72e68b4e39602912106d53b229378a082784b2"
|
|
|
|
|
|
|
|
val, err := Uint160DecodeString(hexStr)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, hexStr, val.String())
|
|
|
|
assert.Equal(t, hexRevStr, val.ReverseString())
|
|
|
|
}
|