/uuid_test.go

https://gitlab.com/JWLAB/uuid · Go · 43 lines · 37 code · 6 blank · 0 comment · 2 complexity · 84a50e87a11b662c0f1fd88a9a3820a7 MD5 · raw file

  1. package uuid
  2. import (
  3. "fmt"
  4. "regexp"
  5. "testing"
  6. . "github.com/smartystreets/goconvey/convey"
  7. )
  8. func Test_New(t *testing.T) {
  9. Convey("Given new UUID is generated.", t, func() {
  10. uuid, _ := New()
  11. match, _ := regexp.MatchString("^[0-9a-f]{8}-[0-9a-f]{4}-[4][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$", uuid)
  12. fmt.Println(uuid)
  13. Convey("A valid v4 UUID is generated.", func() {
  14. So(match, ShouldBeTrue)
  15. })
  16. })
  17. }
  18. func Test_NewUnique(t *testing.T) {
  19. Convey("Given 100000 UUIDs are generated.", t, func() {
  20. umap := make(map[string]bool)
  21. for i := 0; i < 100000; i++ {
  22. id, _ := New()
  23. umap[id] = true
  24. }
  25. Convey("Then 100000 valid v4 UUIDs are generated.", func() {
  26. So(len(umap), ShouldEqual, 100000)
  27. })
  28. })
  29. }
  30. var result string
  31. func Benchmark_New(b *testing.B) {
  32. var uuid string
  33. for n := 0; n < b.N; n++ {
  34. uuid, _ = New()
  35. }
  36. result = uuid
  37. }