PageRenderTime 84ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/internal/github.com/nu7hatch/gouuid/uuid_test.go

https://gitlab.com/JamesClonk/bosh-init
Go | 135 lines | 122 code | 8 blank | 5 comment | 48 complexity | 86504654e1cff6fc99b61f7b00c4218f MD5 | raw file
  1. // This package provides immutable UUID structs and the functions
  2. // NewV3, NewV4, NewV5 and Parse() for generating versions 3, 4
  3. // and 5 UUIDs as specified in RFC 4122.
  4. //
  5. // Copyright (C) 2011 by Krzysztof Kowalik <chris@nu7hat.ch>
  6. package uuid
  7. import (
  8. "regexp"
  9. "testing"
  10. )
  11. const format = "^[a-z0-9]{8}-[a-z0-9]{4}-[1-5][a-z0-9]{3}-[a-z0-9]{4}-[a-z0-9]{12}$"
  12. func TestParse(t *testing.T) {
  13. _, err := Parse([]byte{1, 2, 3, 4, 5})
  14. if err == nil {
  15. t.Errorf("Expected error due to invalid UUID sequence")
  16. }
  17. base, _ := NewV4()
  18. u, err := Parse(base[:])
  19. if err != nil {
  20. t.Errorf("Expected to parse UUID sequence without problems")
  21. return
  22. }
  23. if u.String() != base.String() {
  24. t.Errorf("Expected parsed UUID to be the same as base, %s != %s", u.String(), base.String())
  25. }
  26. }
  27. func TestParseString(t *testing.T) {
  28. _, err := ParseHex("foo")
  29. if err == nil {
  30. t.Errorf("Expected error due to invalid UUID string")
  31. }
  32. base, _ := NewV4()
  33. u, err := ParseHex(base.String())
  34. if err != nil {
  35. t.Errorf("Expected to parse UUID sequence without problems")
  36. return
  37. }
  38. if u.String() != base.String() {
  39. t.Errorf("Expected parsed UUID to be the same as base, %s != %s", u.String(), base.String())
  40. }
  41. }
  42. func TestNewV3(t *testing.T) {
  43. u, err := NewV3(NamespaceURL, []byte("golang.org"))
  44. if err != nil {
  45. t.Errorf("Expected to generate UUID without problems, error thrown: %d", err.Error())
  46. return
  47. }
  48. if u.Version() != 3 {
  49. t.Errorf("Expected to generate UUIDv3, given %d", u.Version())
  50. }
  51. if u.Variant() != ReservedRFC4122 {
  52. t.Errorf("Expected to generate UUIDv3 RFC4122 variant, given %x", u.Variant())
  53. }
  54. re := regexp.MustCompile(format)
  55. if !re.MatchString(u.String()) {
  56. t.Errorf("Expected string representation to be valid, given %s", u.String())
  57. }
  58. u2, _ := NewV3(NamespaceURL, []byte("golang.org"))
  59. if u2.String() != u.String() {
  60. t.Errorf("Expected UUIDs generated of the same namespace and name to be the same")
  61. }
  62. u3, _ := NewV3(NamespaceDNS, []byte("golang.org"))
  63. if u3.String() == u.String() {
  64. t.Errorf("Expected UUIDs generated of different namespace and the same name to be different")
  65. }
  66. u4, _ := NewV3(NamespaceURL, []byte("code.google.com"))
  67. if u4.String() == u.String() {
  68. t.Errorf("Expected UUIDs generated of the same namespace and different names to be different")
  69. }
  70. }
  71. func TestNewV4(t *testing.T) {
  72. u, err := NewV4()
  73. if err != nil {
  74. t.Errorf("Expected to generate UUID without problems, error thrown: %s", err.Error())
  75. return
  76. }
  77. if u.Version() != 4 {
  78. t.Errorf("Expected to generate UUIDv4, given %d", u.Version())
  79. }
  80. if u.Variant() != ReservedRFC4122 {
  81. t.Errorf("Expected to generate UUIDv4 RFC4122 variant, given %x", u.Variant())
  82. }
  83. re := regexp.MustCompile(format)
  84. if !re.MatchString(u.String()) {
  85. t.Errorf("Expected string representation to be valid, given %s", u.String())
  86. }
  87. }
  88. func TestNewV5(t *testing.T) {
  89. u, err := NewV5(NamespaceURL, []byte("golang.org"))
  90. if err != nil {
  91. t.Errorf("Expected to generate UUID without problems, error thrown: %d", err.Error())
  92. return
  93. }
  94. if u.Version() != 5 {
  95. t.Errorf("Expected to generate UUIDv5, given %d", u.Version())
  96. }
  97. if u.Variant() != ReservedRFC4122 {
  98. t.Errorf("Expected to generate UUIDv5 RFC4122 variant, given %x", u.Variant())
  99. }
  100. re := regexp.MustCompile(format)
  101. if !re.MatchString(u.String()) {
  102. t.Errorf("Expected string representation to be valid, given %s", u.String())
  103. }
  104. u2, _ := NewV5(NamespaceURL, []byte("golang.org"))
  105. if u2.String() != u.String() {
  106. t.Errorf("Expected UUIDs generated of the same namespace and name to be the same")
  107. }
  108. u3, _ := NewV5(NamespaceDNS, []byte("golang.org"))
  109. if u3.String() == u.String() {
  110. t.Errorf("Expected UUIDs generated of different namespace and the same name to be different")
  111. }
  112. u4, _ := NewV5(NamespaceURL, []byte("code.google.com"))
  113. if u4.String() == u.String() {
  114. t.Errorf("Expected UUIDs generated of the same namespace and different names to be different")
  115. }
  116. }
  117. func BenchmarkParseHex(b *testing.B) {
  118. s := "f3593cff-ee92-40df-4086-87825b523f13"
  119. for i := 0; i < b.N; i++ {
  120. _, err := ParseHex(s)
  121. if err != nil {
  122. b.Fatal(err)
  123. }
  124. }
  125. b.StopTimer()
  126. b.ReportAllocs()
  127. }