codec_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // Copyright (C) 2013-2018 by Maxim Bublis <b@codemonkey.ru>
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining
  4. // a copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to
  8. // permit persons to whom the Software is furnished to do so, subject to
  9. // the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be
  12. // included in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. package uuid
  22. import (
  23. "bytes"
  24. . "gopkg.in/check.v1"
  25. )
  26. type codecTestSuite struct{}
  27. var _ = Suite(&codecTestSuite{})
  28. func (s *codecTestSuite) TestFromBytes(c *C) {
  29. u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
  30. b1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
  31. u1, err := FromBytes(b1)
  32. c.Assert(err, IsNil)
  33. c.Assert(u1, Equals, u)
  34. b2 := []byte{}
  35. _, err = FromBytes(b2)
  36. c.Assert(err, NotNil)
  37. }
  38. func (s *codecTestSuite) BenchmarkFromBytes(c *C) {
  39. bytes := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
  40. for i := 0; i < c.N; i++ {
  41. FromBytes(bytes)
  42. }
  43. }
  44. func (s *codecTestSuite) TestMarshalBinary(c *C) {
  45. u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
  46. b1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
  47. b2, err := u.MarshalBinary()
  48. c.Assert(err, IsNil)
  49. c.Assert(bytes.Equal(b1, b2), Equals, true)
  50. }
  51. func (s *codecTestSuite) BenchmarkMarshalBinary(c *C) {
  52. u := NewV4()
  53. for i := 0; i < c.N; i++ {
  54. u.MarshalBinary()
  55. }
  56. }
  57. func (s *codecTestSuite) TestUnmarshalBinary(c *C) {
  58. u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
  59. b1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
  60. u1 := UUID{}
  61. err := u1.UnmarshalBinary(b1)
  62. c.Assert(err, IsNil)
  63. c.Assert(u1, Equals, u)
  64. b2 := []byte{}
  65. u2 := UUID{}
  66. err = u2.UnmarshalBinary(b2)
  67. c.Assert(err, NotNil)
  68. }
  69. func (s *codecTestSuite) TestFromString(c *C) {
  70. u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
  71. s1 := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
  72. s2 := "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}"
  73. s3 := "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8"
  74. s4 := "6ba7b8109dad11d180b400c04fd430c8"
  75. s5 := "urn:uuid:6ba7b8109dad11d180b400c04fd430c8"
  76. _, err := FromString("")
  77. c.Assert(err, NotNil)
  78. u1, err := FromString(s1)
  79. c.Assert(err, IsNil)
  80. c.Assert(u1, Equals, u)
  81. u2, err := FromString(s2)
  82. c.Assert(err, IsNil)
  83. c.Assert(u2, Equals, u)
  84. u3, err := FromString(s3)
  85. c.Assert(err, IsNil)
  86. c.Assert(u3, Equals, u)
  87. u4, err := FromString(s4)
  88. c.Assert(err, IsNil)
  89. c.Assert(u4, Equals, u)
  90. u5, err := FromString(s5)
  91. c.Assert(err, IsNil)
  92. c.Assert(u5, Equals, u)
  93. }
  94. func (s *codecTestSuite) BenchmarkFromString(c *C) {
  95. str := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
  96. for i := 0; i < c.N; i++ {
  97. FromString(str)
  98. }
  99. }
  100. func (s *codecTestSuite) BenchmarkFromStringUrn(c *C) {
  101. str := "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8"
  102. for i := 0; i < c.N; i++ {
  103. FromString(str)
  104. }
  105. }
  106. func (s *codecTestSuite) BenchmarkFromStringWithBrackets(c *C) {
  107. str := "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}"
  108. for i := 0; i < c.N; i++ {
  109. FromString(str)
  110. }
  111. }
  112. func (s *codecTestSuite) TestFromStringShort(c *C) {
  113. // Invalid 35-character UUID string
  114. s1 := "6ba7b810-9dad-11d1-80b4-00c04fd430c"
  115. for i := len(s1); i >= 0; i-- {
  116. _, err := FromString(s1[:i])
  117. c.Assert(err, NotNil)
  118. }
  119. }
  120. func (s *codecTestSuite) TestFromStringLong(c *C) {
  121. // Invalid 37+ character UUID string
  122. strings := []string{
  123. "6ba7b810-9dad-11d1-80b4-00c04fd430c8=",
  124. "6ba7b810-9dad-11d1-80b4-00c04fd430c8}",
  125. "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}f",
  126. "6ba7b810-9dad-11d1-80b4-00c04fd430c800c04fd430c8",
  127. }
  128. for _, str := range strings {
  129. _, err := FromString(str)
  130. c.Assert(err, NotNil)
  131. }
  132. }
  133. func (s *codecTestSuite) TestFromStringInvalid(c *C) {
  134. // Invalid UUID string formats
  135. strings := []string{
  136. "6ba7b8109dad11d180b400c04fd430c86ba7b8109dad11d180b400c04fd430c8",
  137. "urn:uuid:{6ba7b810-9dad-11d1-80b4-00c04fd430c8}",
  138. "uuid:urn:6ba7b810-9dad-11d1-80b4-00c04fd430c8",
  139. "uuid:urn:6ba7b8109dad11d180b400c04fd430c8",
  140. "6ba7b8109-dad-11d1-80b4-00c04fd430c8",
  141. "6ba7b810-9dad1-1d1-80b4-00c04fd430c8",
  142. "6ba7b810-9dad-11d18-0b4-00c04fd430c8",
  143. "6ba7b810-9dad-11d1-80b40-0c04fd430c8",
  144. "6ba7b810+9dad+11d1+80b4+00c04fd430c8",
  145. "(6ba7b810-9dad-11d1-80b4-00c04fd430c8}",
  146. "{6ba7b810-9dad-11d1-80b4-00c04fd430c8>",
  147. "zba7b810-9dad-11d1-80b4-00c04fd430c8",
  148. "6ba7b810-9dad11d180b400c04fd430c8",
  149. "6ba7b8109dad-11d180b400c04fd430c8",
  150. "6ba7b8109dad11d1-80b400c04fd430c8",
  151. "6ba7b8109dad11d180b4-00c04fd430c8",
  152. }
  153. for _, str := range strings {
  154. _, err := FromString(str)
  155. c.Assert(err, NotNil)
  156. }
  157. }
  158. func (s *codecTestSuite) TestFromStringOrNil(c *C) {
  159. u := FromStringOrNil("")
  160. c.Assert(u, Equals, Nil)
  161. }
  162. func (s *codecTestSuite) TestFromBytesOrNil(c *C) {
  163. b := []byte{}
  164. u := FromBytesOrNil(b)
  165. c.Assert(u, Equals, Nil)
  166. }
  167. func (s *codecTestSuite) TestMarshalText(c *C) {
  168. u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
  169. b1 := []byte("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
  170. b2, err := u.MarshalText()
  171. c.Assert(err, IsNil)
  172. c.Assert(bytes.Equal(b1, b2), Equals, true)
  173. }
  174. func (s *codecTestSuite) BenchmarkMarshalText(c *C) {
  175. u := NewV4()
  176. for i := 0; i < c.N; i++ {
  177. u.MarshalText()
  178. }
  179. }
  180. func (s *codecTestSuite) TestUnmarshalText(c *C) {
  181. u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
  182. b1 := []byte("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
  183. u1 := UUID{}
  184. err := u1.UnmarshalText(b1)
  185. c.Assert(err, IsNil)
  186. c.Assert(u1, Equals, u)
  187. b2 := []byte("")
  188. u2 := UUID{}
  189. err = u2.UnmarshalText(b2)
  190. c.Assert(err, NotNil)
  191. }
  192. func (s *codecTestSuite) BenchmarkUnmarshalText(c *C) {
  193. bytes := []byte("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
  194. u := UUID{}
  195. for i := 0; i < c.N; i++ {
  196. u.UnmarshalText(bytes)
  197. }
  198. }
  199. var sink string
  200. func (s *codecTestSuite) BenchmarkMarshalToString(c *C) {
  201. u := NewV4()
  202. for i := 0; i < c.N; i++ {
  203. sink = u.String()
  204. }
  205. }