[#294] user: Implement Cmp() function for ID struct
Some checks failed
DCO / DCO (pull_request) Failing after 30s
Code generation / Generate proto (pull_request) Successful in 38s
Tests and linters / Tests (pull_request) Successful in 50s
Tests and linters / Lint (pull_request) Successful in 1m35s

Signed-off-by: Alexander Chuprov <a.chuprov@yadro.com>
This commit is contained in:
Alexander Chuprov 2025-02-13 19:33:19 +03:00
parent 49fdcca449
commit 0f97a1e917
Signed by: achuprov
GPG key ID: 2D916FFD803B0EDD
2 changed files with 22 additions and 0 deletions

View file

@ -123,3 +123,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 a == b, -1 if a < b, and +1 if a > b.
func (x ID) Cmp(x2 ID) int {
return bytes.Compare([]byte(x.EncodeToString()), []byte(x2.EncodeToString()))
}

View file

@ -3,6 +3,7 @@ package user_test
import (
"bytes"
"crypto/rand"
"slices"
"testing"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
@ -132,3 +133,18 @@ 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++ {
if bytes.Compare([]byte(arr[i-1].EncodeToString()), []byte(arr[i].EncodeToString())) > 0 {
t.Fatalf("arrays are not sorted correctly")
}
}
}