-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* fix: shift timestamp 1 bit to right * test: make RandomReader and Timestamp mockable
- Loading branch information
Showing
5 changed files
with
76 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package iid | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// Writes the provided t into the first 33 bit of b | ||
func writeTime(b []byte, t uint32) { | ||
if len(b) < 5 { | ||
panic(errors.New("length of b is less than 5")) | ||
} | ||
|
||
b[0] = byte(t >> 25) | ||
b[1] = byte(t >> 17) | ||
b[2] = byte(t >> 9) | ||
b[3] = byte(t >> 1) | ||
// unset the first bit | ||
b[4] = b[4] << 1 >> 1 | ||
// set the first bit to the value of the last bit of t | ||
b[4] = b[4] | byte(t << 7) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package iid | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestWrite(t *testing.T) { | ||
var b uint8 = 0xFF | ||
buf := make([]byte, 5) | ||
writeTime(buf, 0xFFFFFFFF) | ||
|
||
assert.Equal(t, b >> 1, buf[0]) | ||
assert.Equal(t, b, buf[1]) | ||
assert.Equal(t, b, buf[2]) | ||
assert.Equal(t, b, buf[3]) | ||
assert.Equal(t, b << 7, buf[4]) | ||
} |