[#294] object: Implement Cmp() function for ID struct

Signed-off-by: Alexander Chuprov <a.chuprov@yadro.com>
This commit is contained in:
Alexander Chuprov 2025-02-17 11:49:10 +03:00 committed by Dmitrii Stepanov
parent 56892e48ac
commit 148a341b6f
2 changed files with 22 additions and 0 deletions

View file

@ -4,6 +4,7 @@ import (
"crypto/ecdsa"
"crypto/sha256"
"fmt"
"strings"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
frostfscrypto "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/crypto"
@ -167,3 +168,9 @@ func (id *ID) UnmarshalJSON(data []byte) error {
return nil
}
// Cmp returns an integer comparing two base58 encoded object ID lexicographically.
// The result will be 0 if id1 == id2, -1 if id1 < id2, and +1 if id1 > id2.
func (id ID) Cmp(id2 ID) int {
return strings.Compare(id.EncodeToString(), id2.EncodeToString())
}

View file

@ -3,7 +3,9 @@ package oid
import (
"crypto/rand"
"crypto/sha256"
"slices"
"strconv"
"strings"
"testing"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
@ -180,3 +182,16 @@ func TestID_Encode(t *testing.T) {
require.Equal(t, emptyID, id.EncodeToString())
})
}
func TestID_Cmp(t *testing.T) {
id1 := randID(t)
id2 := randID(t)
id3 := randID(t)
arr := []ID{id1, id2, id3}
slices.SortFunc(arr, ID.Cmp)
for i := 1; i < len(arr); i++ {
require.NotEqual(t, strings.Compare(arr[i-1].EncodeToString(), arr[i].EncodeToString()), 1, "array is not sorted correctly")
}
}