Skip to content

Commit

Permalink
feat: add Compare function (#163)
Browse files Browse the repository at this point in the history
* feat: add Compare function

* fix comment
  • Loading branch information
it512 authored and tkarrass committed Sep 11, 2024
1 parent 9bd7b9c commit 27f465a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 6 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package uuid

import (
"bytes"
"io"
)

Expand Down Expand Up @@ -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[:])
}
6 changes: 3 additions & 3 deletions uuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 27f465a

Please sign in to comment.