Merge pull request #515 from nspcc-dev/feat/util_coverage
util: add missing tests
This commit is contained in:
commit
bbbd0db17c
4 changed files with 106 additions and 76 deletions
|
@ -1,26 +1,23 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestArrayEvenReverse(t *testing.T) {
|
||||
arr := []byte{0x01, 0x02, 0x03, 0x04}
|
||||
have := ArrayReverse(arr)
|
||||
want := []byte{0x04, 0x03, 0x02, 0x01}
|
||||
if !bytes.Equal(have, want) {
|
||||
t.Fatalf("expected %v got %v", want, have)
|
||||
}
|
||||
require.Equal(t, want, have)
|
||||
}
|
||||
|
||||
func TestArrayOddReverse(t *testing.T) {
|
||||
arr := []byte{0x01, 0x02, 0x03, 0x04, 0x05}
|
||||
have := ArrayReverse(arr)
|
||||
want := []byte{0x05, 0x04, 0x03, 0x02, 0x01}
|
||||
if !bytes.Equal(have, want) {
|
||||
t.Fatalf("expected %v got %v", want, have)
|
||||
}
|
||||
require.Equal(t, want, have)
|
||||
}
|
||||
|
||||
// This tests a bug that occurred with arrays of size 1
|
||||
|
@ -28,7 +25,5 @@ func TestArrayReverseLen2(t *testing.T) {
|
|||
arr := []byte{0x01}
|
||||
have := ArrayReverse(arr)
|
||||
want := []byte{0x01}
|
||||
if !bytes.Equal(have, want) {
|
||||
t.Fatalf("expected %v got %v", want, have)
|
||||
}
|
||||
require.Equal(t, want, have)
|
||||
}
|
||||
|
|
|
@ -64,6 +64,15 @@ func TestFixed8FromString(t *testing.T) {
|
|||
n, err = Fixed8FromString(val)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, Fixed8(90123410000), n)
|
||||
|
||||
// Fixed8FromString with errors
|
||||
val = "90n1"
|
||||
_, err = Fixed8FromString(val)
|
||||
assert.Error(t, err)
|
||||
|
||||
val = "90.1s"
|
||||
_, err = Fixed8FromString(val)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestSatoshi(t *testing.T) {
|
||||
|
@ -93,4 +102,35 @@ func TestFixed8UnmarshalJSON(t *testing.T) {
|
|||
assert.Nil(t, json.Unmarshal(s, &u2))
|
||||
assert.Equal(t, expected, u2)
|
||||
}
|
||||
|
||||
errorCases := []string{
|
||||
`"123.u"`,
|
||||
"13.j",
|
||||
}
|
||||
|
||||
for _, tc := range errorCases {
|
||||
var u Fixed8
|
||||
assert.Error(t, u.UnmarshalJSON([]byte(tc)))
|
||||
}
|
||||
}
|
||||
|
||||
func TestFixed8_MarshalJSON(t *testing.T) {
|
||||
u, err := Fixed8FromString("123.4")
|
||||
assert.NoError(t, err)
|
||||
|
||||
s, err := json.Marshal(u)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []byte(`"123.4"`), s)
|
||||
}
|
||||
|
||||
func TestFixed8_Arith(t *testing.T) {
|
||||
u1 := Fixed8FromInt64(3)
|
||||
u2 := Fixed8FromInt64(8)
|
||||
|
||||
assert.True(t, u1.LessThan(u2))
|
||||
assert.True(t, u2.GreaterThan(u1))
|
||||
assert.True(t, u1.Equal(u1))
|
||||
assert.NotZero(t, u1.CompareTo(u2))
|
||||
assert.Zero(t, u1.CompareTo(u1))
|
||||
assert.EqualValues(t, Fixed8(2), u2.Div(3))
|
||||
}
|
||||
|
|
|
@ -5,55 +5,55 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestUint160UnmarshalJSON(t *testing.T) {
|
||||
str := "2d3b96ae1bcc5a585e075e3b81920210dec16302"
|
||||
expected, err := Uint160DecodeString(str)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
|
||||
// UnmarshalJSON decodes hex-strings
|
||||
var u1, u2 Uint160
|
||||
|
||||
if err = u1.UnmarshalJSON([]byte(`"` + str + `"`)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.NoError(t, u1.UnmarshalJSON([]byte(`"`+str+`"`)))
|
||||
assert.True(t, expected.Equals(u1))
|
||||
|
||||
s, err := expected.MarshalJSON()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
// UnmarshalJSON decodes hex-strings prefixed by 0x
|
||||
if err = u2.UnmarshalJSON(s); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.NoError(t, u2.UnmarshalJSON(s))
|
||||
assert.True(t, expected.Equals(u1))
|
||||
|
||||
assert.Error(t, u2.UnmarshalJSON([]byte(`123`)))
|
||||
}
|
||||
|
||||
func TestUInt160DecodeString(t *testing.T) {
|
||||
hexStr := "2d3b96ae1bcc5a585e075e3b81920210dec16302"
|
||||
val, err := Uint160DecodeString(hexStr)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, hexStr, val.String())
|
||||
|
||||
_, err = Uint160DecodeString(hexStr[1:])
|
||||
assert.Error(t, err)
|
||||
|
||||
hexStr = "zz3b96ae1bcc5a585e075e3b81920210dec16302"
|
||||
_, err = Uint160DecodeString(hexStr)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestUint160DecodeBytes(t *testing.T) {
|
||||
hexStr := "2d3b96ae1bcc5a585e075e3b81920210dec16302"
|
||||
b, err := hex.DecodeString(hexStr)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
val, err := Uint160DecodeBytes(b)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, hexStr, val.String())
|
||||
|
||||
_, err = Uint160DecodeBytes(b[1:])
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestUInt160Equals(t *testing.T) {
|
||||
|
@ -61,19 +61,12 @@ func TestUInt160Equals(t *testing.T) {
|
|||
b := "4d3b96ae1bcc5a585e075e3b81920210dec16302"
|
||||
|
||||
ua, err := Uint160DecodeString(a)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
ub, err := Uint160DecodeString(b)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ua.Equals(ub) {
|
||||
t.Fatalf("%s and %s cannot be equal", ua, ub)
|
||||
}
|
||||
if !ua.Equals(ua) {
|
||||
t.Fatalf("%s and %s must be equal", ua, ua)
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
func TestUInt160Less(t *testing.T) {
|
||||
|
|
|
@ -12,50 +12,58 @@ import (
|
|||
func TestUint256UnmarshalJSON(t *testing.T) {
|
||||
str := "f037308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d"
|
||||
expected, err := Uint256DecodeReverseString(str)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
// UnmarshalJSON decodes hex-strings
|
||||
var u1, u2 Uint256
|
||||
|
||||
if err = u1.UnmarshalJSON([]byte(`"` + str + `"`)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, u1.UnmarshalJSON([]byte(`"`+str+`"`)))
|
||||
assert.True(t, expected.Equals(u1))
|
||||
|
||||
s, err := expected.MarshalJSON()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
// UnmarshalJSON decodes hex-strings prefixed by 0x
|
||||
if err = u2.UnmarshalJSON(s); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, u2.UnmarshalJSON(s))
|
||||
assert.True(t, expected.Equals(u1))
|
||||
|
||||
// UnmarshalJSON does not accepts numbers
|
||||
assert.Error(t, u2.UnmarshalJSON([]byte("123")))
|
||||
}
|
||||
|
||||
func TestUint256DecodeString(t *testing.T) {
|
||||
hexStr := "f037308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d"
|
||||
val, err := Uint256DecodeReverseString(hexStr)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, hexStr, val.ReverseString())
|
||||
|
||||
bs, err := hex.DecodeString(hexStr)
|
||||
require.NoError(t, err)
|
||||
|
||||
val1, err := Uint256DecodeBytes(bs)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, hexStr, val1.String())
|
||||
assert.Equal(t, val, val1.Reverse())
|
||||
|
||||
_, err = Uint256DecodeReverseString(hexStr[1:])
|
||||
assert.Error(t, err)
|
||||
|
||||
hexStr = "zzz7308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d"
|
||||
_, err = Uint256DecodeReverseString(hexStr)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestUint256DecodeBytes(t *testing.T) {
|
||||
hexStr := "f037308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d"
|
||||
b, err := hex.DecodeString(hexStr)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
val, err := Uint256DecodeReverseBytes(b)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, hexStr, val.ReverseString())
|
||||
|
||||
_, err = Uint256DecodeBytes(b[1:])
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestUInt256Equals(t *testing.T) {
|
||||
|
@ -63,19 +71,13 @@ func TestUInt256Equals(t *testing.T) {
|
|||
b := "e287c5b29a1b66092be6803c59c765308ac20287e1b4977fd399da5fc8f66ab5"
|
||||
|
||||
ua, err := Uint256DecodeReverseString(a)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
ub, err := Uint256DecodeReverseString(b)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ua.Equals(ub) {
|
||||
t.Fatalf("%s and %s cannot be equal", ua, ub)
|
||||
}
|
||||
if !ua.Equals(ua) {
|
||||
t.Fatalf("%s and %s must be equal", ua, ua)
|
||||
}
|
||||
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)
|
||||
assert.Zero(t, ua.CompareTo(ua), "%s and %s must be equal", ua, ua)
|
||||
}
|
||||
|
||||
func TestUint256_Serializable(t *testing.T) {
|
||||
|
|
Loading…
Reference in a new issue