blob: 3ec9a3aaaaf5a940bf9e3e69a1c4c9ab11024f47 [file] [log] [blame]
// Copyright 2016 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package syncbase_test
import (
"regexp"
"sort"
"testing"
"v.io/v23/syncbase"
)
const (
uuidLoopIncovations int = 100
)
func TestUUIDFormat(t *testing.T) {
uuid := syncbase.UUID()
regexp := regexp.MustCompile("^v[\\$0-9A-Za-z_]{19}$")
if !regexp.MatchString(uuid) {
t.Error("Incorrect UUID format")
}
}
func TestUUIDCollisions(t *testing.T) {
uuidChannel := make(chan string)
createUUID := func() {
uuidChannel <- syncbase.UUID()
}
for i := 0; i < uuidLoopIncovations; i++ {
go createUUID()
}
uuidMap := make(map[string]bool)
for i := 0; i < uuidLoopIncovations; i++ {
uuidMap[<-uuidChannel] = true
}
if len(uuidMap) != uuidLoopIncovations {
t.Errorf("UUID collision for %d UUIDs", uuidLoopIncovations-len(uuidMap))
}
}
func TestUUIDSequential(t *testing.T) {
unsorted := make([]string, 100)
sorted := make([]string, 100)
for i := 0; i < uuidLoopIncovations; i++ {
uuid := syncbase.UUID()
unsorted[i] = uuid
sorted[i] = uuid
}
sort.Strings(sorted)
for i := 0; i < uuidLoopIncovations; i++ {
if unsorted[i] != sorted[i] {
t.Errorf("UUID not sorted at position %d", i)
}
}
}