This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
package langext
|
||||
|
||||
import (
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/tst"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewUUIDLength(t *testing.T) {
|
||||
u, err := NewUUID()
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
tst.AssertEqual(t, len(u), 16)
|
||||
}
|
||||
|
||||
func TestNewUUIDVersionAndVariant(t *testing.T) {
|
||||
u, err := NewUUID()
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
// Version 4 is in upper nibble of byte 6
|
||||
tst.AssertEqual(t, u[6]&0xf0, byte(0x40))
|
||||
// Variant 10 in top two bits of byte 8
|
||||
tst.AssertEqual(t, u[8]&0xc0, byte(0x80))
|
||||
}
|
||||
|
||||
func TestNewUUIDRandomness(t *testing.T) {
|
||||
a, _ := NewUUID()
|
||||
b, _ := NewUUID()
|
||||
if a == b {
|
||||
t.Errorf("two UUIDs should not be equal")
|
||||
}
|
||||
}
|
||||
|
||||
var hexUUIDRegex = regexp.MustCompile(`^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$`)
|
||||
|
||||
func TestNewHexUUIDFormat(t *testing.T) {
|
||||
s, err := NewHexUUID()
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
tst.AssertEqual(t, len(s), 36)
|
||||
if !hexUUIDRegex.MatchString(s) {
|
||||
t.Errorf("not a valid hex UUID format: %q", s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMustHexUUID(t *testing.T) {
|
||||
s := MustHexUUID()
|
||||
tst.AssertEqual(t, len(s), 36)
|
||||
if !hexUUIDRegex.MatchString(s) {
|
||||
t.Errorf("not a valid hex UUID format: %q", s)
|
||||
}
|
||||
}
|
||||
|
||||
var upperHexRegex = regexp.MustCompile(`^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$`)
|
||||
|
||||
func TestNewUpperHexUUIDFormat(t *testing.T) {
|
||||
s, err := NewUpperHexUUID()
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
tst.AssertEqual(t, len(s), 36)
|
||||
tst.AssertEqual(t, s, strings.ToUpper(s))
|
||||
if !upperHexRegex.MatchString(s) {
|
||||
t.Errorf("not a valid upper-hex UUID format: %q", s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMustUpperHexUUID(t *testing.T) {
|
||||
s := MustUpperHexUUID()
|
||||
tst.AssertEqual(t, len(s), 36)
|
||||
}
|
||||
|
||||
func TestNewRawHexUUIDFormat(t *testing.T) {
|
||||
s, err := NewRawHexUUID()
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
tst.AssertEqual(t, len(s), 32)
|
||||
if strings.Contains(s, "-") {
|
||||
t.Errorf("raw hex should have no dashes: %q", s)
|
||||
}
|
||||
tst.AssertEqual(t, s, strings.ToUpper(s))
|
||||
}
|
||||
|
||||
func TestMustRawHexUUID(t *testing.T) {
|
||||
s := MustRawHexUUID()
|
||||
tst.AssertEqual(t, len(s), 32)
|
||||
}
|
||||
|
||||
func TestNewBracesUUID(t *testing.T) {
|
||||
s, err := NewBracesUUID()
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
tst.AssertEqual(t, len(s), 38)
|
||||
tst.AssertEqual(t, string(s[37]), "}")
|
||||
}
|
||||
|
||||
func TestMustBracesUUID(t *testing.T) {
|
||||
s := MustBracesUUID()
|
||||
tst.AssertEqual(t, len(s), 38)
|
||||
}
|
||||
|
||||
func TestNewParensUUID(t *testing.T) {
|
||||
s, err := NewParensUUID()
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
tst.AssertEqual(t, len(s), 38)
|
||||
tst.AssertEqual(t, string(s[37]), ")")
|
||||
}
|
||||
|
||||
func TestMustParensUUID(t *testing.T) {
|
||||
s := MustParensUUID()
|
||||
tst.AssertEqual(t, len(s), 38)
|
||||
}
|
||||
|
||||
func TestUUIDsAreUnique(t *testing.T) {
|
||||
const count = 100
|
||||
seen := make(map[string]bool, count)
|
||||
for range count {
|
||||
s := MustHexUUID()
|
||||
if seen[s] {
|
||||
t.Errorf("collision in UUID set: %q", s)
|
||||
}
|
||||
seen[s] = true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user