[#294] user: Implement Cmp() function for ID struct
Signed-off-by: Alexander Chuprov <a.chuprov@yadro.com>
This commit is contained in:
parent
49fdcca449
commit
0f97a1e917
2 changed files with 22 additions and 0 deletions
|
@ -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()))
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue