[#294] user: Implement Cmp() function for ID struct
Signed-off-by: Alexander Chuprov <a.chuprov@yadro.com>
This commit is contained in:
parent
bcb5fd22d4
commit
c3f7378887
2 changed files with 22 additions and 0 deletions
|
@ -4,6 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
||||||
"github.com/mr-tron/base58"
|
"github.com/mr-tron/base58"
|
||||||
|
@ -123,3 +124,9 @@ func (x *ID) setUserID(w []byte) error {
|
||||||
|
|
||||||
return nil
|
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())
|
||||||
|
}
|
||||||
|
|
|
@ -3,6 +3,8 @@ package user_test
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
|
"slices"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
"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.True(t, id3.Equals(id1)) // commutativity
|
||||||
require.False(t, id1.Equals(id2))
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue