forked from TrueCloudLab/frostfs-sdk-go
[#294] container: Implement Cmp() function for ID struct
Signed-off-by: Alexander Chuprov <a.chuprov@yadro.com>
This commit is contained in:
parent
51b68055ee
commit
523626635e
2 changed files with 23 additions and 0 deletions
|
@ -3,6 +3,7 @@ package cid
|
||||||
import (
|
import (
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"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"
|
||||||
|
@ -113,3 +114,9 @@ func (id *ID) DecodeString(s string) error {
|
||||||
func (id ID) String() string {
|
func (id ID) String() string {
|
||||||
return id.EncodeToString()
|
return id.EncodeToString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cmp returns an integer comparing two base58 encoded container 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())
|
||||||
|
}
|
||||||
|
|
|
@ -3,6 +3,8 @@ package cid_test
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
|
"slices"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
||||||
|
@ -106,3 +108,17 @@ func TestID_Encode(t *testing.T) {
|
||||||
require.Equal(t, emptyID, id.EncodeToString())
|
require.Equal(t, emptyID, id.EncodeToString())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestID_Cmp(t *testing.T) {
|
||||||
|
var arr []cid.ID
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
checksum := randSHA256Checksum()
|
||||||
|
arr = append(arr, cidtest.IDWithChecksum(checksum))
|
||||||
|
}
|
||||||
|
|
||||||
|
slices.SortFunc(arr, cid.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