2018-02-04 19:54:51 +00:00
|
|
|
package util
|
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
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-02-15 15:35:49 +00:00
|
|
|
)
|
|
|
|
|
2018-05-09 05:20:16 +00:00
|
|
|
func TestUint256UnmarshalJSON(t *testing.T) {
|
|
|
|
str := "f037308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d"
|
2019-08-22 17:33:58 +00:00
|
|
|
expected, err := Uint256DecodeReverseString(str)
|
2018-11-26 15:56:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(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 Uint256
|
|
|
|
|
|
|
|
if err = u1.UnmarshalJSON([]byte(`"` + str + `"`)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
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()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// UnmarshalJSON decodes hex-strings prefixed by 0x
|
2018-11-26 15:56:45 +00:00
|
|
|
if err = u2.UnmarshalJSON(s); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assert.True(t, expected.Equals(u1))
|
2018-05-09 05:20:16 +00:00
|
|
|
}
|
|
|
|
|
2018-03-02 15:24:09 +00:00
|
|
|
func TestUint256DecodeString(t *testing.T) {
|
|
|
|
hexStr := "f037308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d"
|
2019-08-22 17:33:58 +00:00
|
|
|
val, err := Uint256DecodeReverseString(hexStr)
|
2018-03-02 15:24:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|
2019-08-22 17:33:58 +00:00
|
|
|
assert.Equal(t, hexStr, val.ReverseString())
|
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)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-08-22 17:33:58 +00:00
|
|
|
val, err := Uint256DecodeReverseBytes(b)
|
2018-03-02 15:24:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-08-22 17:33:58 +00:00
|
|
|
assert.Equal(t, hexStr, val.ReverseString())
|
2018-03-02 15:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUInt256Equals(t *testing.T) {
|
|
|
|
a := "f037308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d"
|
|
|
|
b := "e287c5b29a1b66092be6803c59c765308ac20287e1b4977fd399da5fc8f66ab5"
|
|
|
|
|
2019-08-22 17:33:58 +00:00
|
|
|
ua, err := Uint256DecodeReverseString(a)
|
2018-03-02 15:24:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-08-22 17:33:58 +00:00
|
|
|
ub, err := Uint256DecodeReverseString(b)
|
2018-03-02 15:24:09 +00:00
|
|
|
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)
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|
|
|
|
}
|