// Benchmarks the Variant1() UUIDs. func BenchmarkVariant1(b *testing.B) { // Generate the first UUID away from the benchmark timer so we can get // the mac address detection out of the way before the benchmark starts. b.StopTimer() uuid.Variant1() b.StartTimer() for i := 0; i < b.N; i++ { uuid.Variant1() } }
// Documents a simple use case for a generating and comparing UUIDs. func Example() { u1 := uuid.Variant1() u2 := uuid.Variant4() fmt.Println("Text representation of a uuid: ", u1.String()) if u1.Equal(u2) { fmt.Println("UUIDs shouldn't ever be equal so this is not reached.") } }
// Benchmark the fmt.Sprintf() based string function. This is simply to // demonstrate the relative speed of this approach vs the slightly janky // but much faster approach taken in the default String() function. func BenchmarkStringSlowSprintf(b *testing.B) { // Generate a UUID outside of the timer. b.StopTimer() u := uuid.Variant1() b.StartTimer() for i := 0; i < b.N; i++ { stringSlowSprintf(u) } }
// Benchmark the FromString() function. func BenchmarkFromString(b *testing.B) { // Generate a UUID outside of the timer. b.StopTimer() u := uuid.Variant1().String() b.StartTimer() for i := 0; i < b.N; i++ { uuid.FromString(u) } }
// Verifies that Variant1() never generates duplicate UUIDs. This is not so much // an absolute proof as it is a simple check to ensure that basic functionality // is not broken. This will catch very basic screw ups and is easy to implement. func TestVariant1(t *testing.T) { previous := make(map[string]bool) for i := 0; i < 10000; i++ { u := uuid.Variant1().String() if _, exists := previous[u]; exists == true { t.Fatal("Duplicate UUIDs generated from Variant1(): ", u) } previous[u] = true } }
// End to end test, generate, then convert to string, then UUID and back and // verify that it comes out looking correct. func TestEndToEndConversion(t *testing.T) { initial := uuid.Variant1() to_string := initial.String() from_string, err := uuid.FromString(to_string) if err != nil { t.Fatal("Parsing error for an unknown reason: " + err.Error()) } if !initial.Equal(from_string) { t.Fatal("Result of Variant1() -> String() -> FromString are not equal.") } }