mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2025-02-22 11:17:35 +00:00
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:
parent
dda3d8b284
commit
d40ccb3e57
2 changed files with 10 additions and 0 deletions
|
@ -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
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Add table
Reference in a new issue