1
0
mirror of https://github.com/google/uuid.git synced 2024-11-24 08:32:23 +02:00

feat: add Compare function (#163)

* feat: add Compare function

* fix comment
This commit is contained in:
MikeWang 2024-07-02 00:15:43 +08:00 committed by GitHub
parent 53dda83ebe
commit e8d82d30a3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 3 deletions

View File

@ -5,6 +5,7 @@
package uuid
import (
"bytes"
"io"
)
@ -41,3 +42,8 @@ func xtob(x1, x2 byte) (byte, bool) {
b2 := xvalues[x2]
return (b1 << 4) | b2, b1 != 255 && b2 != 255
}
// Compare returns an integer comparing two uuids lexicographically. The result will be 0 if a == b, -1 if a < b, and +1 if a > b.
func Compare(a, b UUID) int {
return bytes.Compare(a[:], b[:])
}

View File

@ -918,10 +918,10 @@ func TestVersion7MonotonicityStrict(t *testing.T) {
defer SetRand(nil)
length := 100000 // > 3906
u1 := Must(NewV7()).String()
u1 := Must(NewV7())
for i := 0; i < length; i++ {
u2 := Must(NewV7()).String()
if u2 <= u1 {
u2 := Must(NewV7())
if Compare(u1, u2) >= 0 {
t.Errorf("monotonicity failed at #%d: %s(next) < %s(before)", i, u2, u1)
break
}