PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/uuid_test.go

https://gitlab.com/aviz/go-uuid
Go | 158 lines | 142 code | 11 blank | 5 comment | 52 complexity | 566882f7d3da60e6a6807482fcf4f2f4 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. "fmt"
  9. "regexp"
  10. "testing"
  11. )
  12. const format = "^[a-z0-9]{8}-[a-z0-9]{4}-[1-5][a-z0-9]{3}-[a-z0-9]{4}-[a-z0-9]{12}$"
  13. func TestParse(t *testing.T) {
  14. _, err := Parse([]byte{1, 2, 3, 4, 5})
  15. if err == nil {
  16. t.Errorf("Expected error due to invalid UUID sequence")
  17. }
  18. base, _ := NewV4()
  19. u, err := Parse(base[:])
  20. if err != nil {
  21. t.Errorf("Expected to parse UUID sequence without problems")
  22. return
  23. }
  24. if u.String() != base.String() {
  25. t.Errorf("Expected parsed UUID to be the same as base, %s != %s", u.String(), base.String())
  26. }
  27. }
  28. func TestParseString(t *testing.T) {
  29. _, err := ParseHex("foo")
  30. if err == nil {
  31. t.Errorf("Expected error due to invalid UUID string")
  32. }
  33. base, _ := NewV4()
  34. u, err := ParseHex(base.String())
  35. if err != nil {
  36. t.Errorf("Expected to parse UUID sequence without problems")
  37. return
  38. }
  39. if u.String() != base.String() {
  40. t.Errorf("Expected parsed UUID to be the same as base, %s != %s", u.String(), base.String())
  41. }
  42. }
  43. func TestNewV3(t *testing.T) {
  44. u, err := NewV3(NamespaceURL, []byte("golang.org"))
  45. if err != nil {
  46. t.Errorf("Expected to generate UUID without problems, error thrown: %d", err.Error())
  47. return
  48. }
  49. if u.Version() != 3 {
  50. t.Errorf("Expected to generate UUIDv3, given %d", u.Version())
  51. }
  52. if u.Variant() != ReservedRFC4122 {
  53. t.Errorf("Expected to generate UUIDv3 RFC4122 variant, given %x", u.Variant())
  54. }
  55. re := regexp.MustCompile(format)
  56. if !re.MatchString(u.String()) {
  57. t.Errorf("Expected string representation to be valid, given %s", u.String())
  58. }
  59. u2, _ := NewV3(NamespaceURL, []byte("golang.org"))
  60. if u2.String() != u.String() {
  61. t.Errorf("Expected UUIDs generated of the same namespace and name to be the same")
  62. }
  63. u3, _ := NewV3(NamespaceDNS, []byte("golang.org"))
  64. if u3.String() == u.String() {
  65. t.Errorf("Expected UUIDs generated of different namespace and the same name to be different")
  66. }
  67. u4, _ := NewV3(NamespaceURL, []byte("code.google.com"))
  68. if u4.String() == u.String() {
  69. t.Errorf("Expected UUIDs generated of the same namespace and different names to be different")
  70. }
  71. }
  72. func TestNewV4(t *testing.T) {
  73. u, err := NewV4()
  74. if err != nil {
  75. t.Errorf("Expected to generate UUID without problems, error thrown: %s", err.Error())
  76. return
  77. }
  78. if u.Version() != 4 {
  79. t.Errorf("Expected to generate UUIDv4, given %d", u.Version())
  80. }
  81. if u.Variant() != ReservedRFC4122 {
  82. t.Errorf("Expected to generate UUIDv4 RFC4122 variant, given %x", u.Variant())
  83. }
  84. re := regexp.MustCompile(format)
  85. if !re.MatchString(u.String()) {
  86. t.Errorf("Expected string representation to be valid, given %s", u.String())
  87. }
  88. }
  89. func TestNewV5(t *testing.T) {
  90. u, err := NewV5(NamespaceURL, []byte("golang.org"))
  91. if err != nil {
  92. t.Errorf("Expected to generate UUID without problems, error thrown: %d", err.Error())
  93. return
  94. }
  95. if u.Version() != 5 {
  96. t.Errorf("Expected to generate UUIDv5, given %d", u.Version())
  97. }
  98. if u.Variant() != ReservedRFC4122 {
  99. t.Errorf("Expected to generate UUIDv5 RFC4122 variant, given %x", u.Variant())
  100. }
  101. re := regexp.MustCompile(format)
  102. if !re.MatchString(u.String()) {
  103. t.Errorf("Expected string representation to be valid, given %s", u.String())
  104. }
  105. u2, _ := NewV5(NamespaceURL, []byte("golang.org"))
  106. if u2.String() != u.String() {
  107. t.Errorf("Expected UUIDs generated of the same namespace and name to be the same")
  108. }
  109. u3, _ := NewV5(NamespaceDNS, []byte("golang.org"))
  110. if u3.String() == u.String() {
  111. t.Errorf("Expected UUIDs generated of different namespace and the same name to be different")
  112. }
  113. u4, _ := NewV5(NamespaceURL, []byte("code.google.com"))
  114. if u4.String() == u.String() {
  115. t.Errorf("Expected UUIDs generated of the same namespace and different names to be different")
  116. }
  117. }
  118. func TestString(t *testing.T) {
  119. if NamespaceDNS.String() != "6ba7b810-9dad-11d1-80b4-00c04fd430c8" {
  120. t.Error("Expected String returns unparsed version of UUID")
  121. }
  122. }
  123. func BenchmarkParseHex(b *testing.B) {
  124. s := "f3593cff-ee92-40df-4086-87825b523f13"
  125. for i := 0; i < b.N; i++ {
  126. _, err := ParseHex(s)
  127. if err != nil {
  128. b.Fatal(err)
  129. }
  130. }
  131. b.StopTimer()
  132. b.ReportAllocs()
  133. }
  134. func BenchmarkStringNew(b *testing.B) {
  135. for i := 0; i < b.N; i++ {
  136. _ = NamespaceDNS.String()
  137. }
  138. b.StopTimer()
  139. b.ReportAllocs()
  140. }
  141. func BenchmarkStringOld(b *testing.B) {
  142. for i := 0; i < b.N; i++ {
  143. _ = fmt.Sprintf("%x-%x-%x-%x-%x", NamespaceDNS[0:4], NamespaceDNS[4:6], NamespaceDNS[6:8], NamespaceDNS[8:10], NamespaceDNS[10:])
  144. }
  145. b.StopTimer()
  146. b.ReportAllocs()
  147. }