util: add Compare to Uint160

It's useful for slices-based sorting/finding.

Signed-off-by: Roman Khimov <roman@nspcc.ru>
This commit is contained in:
Roman Khimov 2024-08-26 19:07:37 +03:00
parent dda3d8b284
commit d40ccb3e57
2 changed files with 10 additions and 0 deletions

View file

@ -1,6 +1,7 @@
package util
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
@ -119,6 +120,11 @@ func (u Uint160) Less(other Uint160) bool {
return false
}
// Compare performs three-way comparison of one Uint160 to another.
func (u Uint160) Compare(other Uint160) int {
return bytes.Compare(u[:], other[:])
}
// UnmarshalJSON implements the json unmarshaller interface.
func (u *Uint160) UnmarshalJSON(data []byte) (err error) {
var js string

View file

@ -114,6 +114,10 @@ func TestUInt160Less(t *testing.T) {
assert.Equal(t, true, ua.Less(ub))
assert.Equal(t, false, ua.Less(ua2))
assert.Equal(t, false, ub.Less(ua))
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))
}
func TestUInt160String(t *testing.T) {