From d40ccb3e573b396468d45ed11ca9f97c14feb043 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Mon, 26 Aug 2024 19:07:37 +0300 Subject: [PATCH] util: add Compare to Uint160 It's useful for slices-based sorting/finding. Signed-off-by: Roman Khimov --- pkg/util/uint160.go | 6 ++++++ pkg/util/uint160_test.go | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/pkg/util/uint160.go b/pkg/util/uint160.go index 451918107..9d1d362f3 100644 --- a/pkg/util/uint160.go +++ b/pkg/util/uint160.go @@ -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 diff --git a/pkg/util/uint160_test.go b/pkg/util/uint160_test.go index 4d3cb3a44..4d670101e 100644 --- a/pkg/util/uint160_test.go +++ b/pkg/util/uint160_test.go @@ -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) {