forked from TrueCloudLab/frostfs-sdk-go
[#294] object: Implement Cmp() function for ID struct
Signed-off-by: Alexander Chuprov <a.chuprov@yadro.com>
This commit is contained in:
parent
7bdc78f2b5
commit
51b68055ee
2 changed files with 22 additions and 0 deletions
|
@ -4,6 +4,7 @@ import (
|
||||||
"crypto/ecdsa"
|
"crypto/ecdsa"
|
||||||
"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"
|
||||||
frostfscrypto "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/crypto"
|
frostfscrypto "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/crypto"
|
||||||
|
@ -167,3 +168,9 @@ func (id *ID) UnmarshalJSON(data []byte) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cmp returns an integer comparing two base58 encoded object 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,7 +3,9 @@ package oid
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
||||||
|
@ -180,3 +182,16 @@ func TestID_Encode(t *testing.T) {
|
||||||
require.Equal(t, emptyID, id.EncodeToString())
|
require.Equal(t, emptyID, id.EncodeToString())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestID_Cmp(t *testing.T) {
|
||||||
|
id1 := randID(t)
|
||||||
|
id2 := randID(t)
|
||||||
|
id3 := randID(t)
|
||||||
|
|
||||||
|
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