Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion roaring64/bsi64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,42 @@ func TestSetAndGetUUIDValue(t *testing.T) {
bv, _ := bsi.GetBigValue(1)
assert.Equal(t, bigUUID, bv)

newUUID, err := uuid.FromBytes(bv.Bytes())
// big.Int.Bytes() drops leading zero bytes; use FillBytes to restore the
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// original length so uuid.FromBytes receives exactly len(b) bytes.
bvBytes := make([]byte, len(b))
bv.FillBytes(bvBytes)
newUUID, err := uuid.FromBytes(bvBytes)
assert.Nil(t, err)

assert.Equal(t, uuidVal.String(), newUUID.String())
}

// TestSetAndGetUUIDValueLeadingZero is a deterministic regression test for the
// case where the UUID binary representation has a leading zero byte, which
// big.Int.Bytes() would silently drop.
func TestSetAndGetUUIDValueLeadingZero(t *testing.T) {
// Construct a UUID whose MarshalBinary starts with 0x00.
var uuidVal uuid.UUID
uuidVal[0] = 0x00
uuidVal[6] = 0x40 // version 4
uuidVal[8] = 0x80 // variant bits
b, errx := uuidVal.MarshalBinary()
require.Nil(t, errx)
require.Equal(t, 0x00, int(b[0]), "test requires leading zero byte")

bigUUID := new(big.Int)
bigUUID.SetBytes(b)
bsi := NewDefaultBSI()
bsi.SetBigValue(1, bigUUID)
bv, _ := bsi.GetBigValue(1)

bvBytes := make([]byte, len(b))
bv.FillBytes(bvBytes)
newUUID, err := uuid.FromBytes(bvBytes)
assert.Nil(t, err)
assert.Equal(t, uuidVal.String(), newUUID.String())
}

func secondsAndNanosToBigInt(seconds int64, nanos int32) *big.Int {
b := make([]byte, 12)
binary.BigEndian.PutUint64(b[:8], uint64(seconds))
Expand Down
Loading