[#294] user: 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:50:05 +03:00 committed by Dmitrii Stepanov
parent bcb5fd22d4
commit c3f7378887
2 changed files with 22 additions and 0 deletions

View file

@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"fmt"
"strings"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
"github.com/mr-tron/base58"
@ -123,3 +124,9 @@ func (x *ID) setUserID(w []byte) error {
return nil
}
// Cmp returns an integer comparing two base58 encoded user ID lexicographically.
// The result will be 0 if id1 == id2, -1 if id1 < id2, and +1 if id1 > id2.
func (x ID) Cmp(x2 ID) int {
return strings.Compare(x.EncodeToString(), x2.EncodeToString())
}

View file

@ -3,6 +3,8 @@ package user_test
import (
"bytes"
"crypto/rand"
"slices"
"strings"
"testing"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
@ -132,3 +134,16 @@ func TestID_Equal(t *testing.T) {
require.True(t, id3.Equals(id1)) // commutativity
require.False(t, id1.Equals(id2))
}
func TestID_Cmp(t *testing.T) {
id1 := usertest.ID()
id2 := usertest.ID()
id3 := usertest.ID()
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")
}
}