github.com/mymmsc/gox@v1.3.33/util/uuid/codec_test.go (about) 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 22 package uuid 23 24 import ( 25 "bytes" 26 27 . "gopkg.in/check.v1" 28 ) 29 30 type codecTestSuite struct{} 31 32 var _ = Suite(&codecTestSuite{}) 33 34 func (s *codecTestSuite) TestFromBytes(c *C) { 35 u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8} 36 b1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8} 37 38 u1, err := FromBytes(b1) 39 c.Assert(err, IsNil) 40 c.Assert(u1, Equals, u) 41 42 b2 := []byte{} 43 _, err = FromBytes(b2) 44 c.Assert(err, NotNil) 45 } 46 47 func (s *codecTestSuite) BenchmarkFromBytes(c *C) { 48 bytes := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8} 49 for i := 0; i < c.N; i++ { 50 FromBytes(bytes) 51 } 52 } 53 54 func (s *codecTestSuite) TestMarshalBinary(c *C) { 55 u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8} 56 b1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8} 57 58 b2, err := u.MarshalBinary() 59 c.Assert(err, IsNil) 60 c.Assert(bytes.Equal(b1, b2), Equals, true) 61 } 62 63 func (s *codecTestSuite) BenchmarkMarshalBinary(c *C) { 64 u := NewV4() 65 for i := 0; i < c.N; i++ { 66 u.MarshalBinary() 67 } 68 } 69 70 func (s *codecTestSuite) TestUnmarshalBinary(c *C) { 71 u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8} 72 b1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8} 73 74 u1 := UUID{} 75 err := u1.UnmarshalBinary(b1) 76 c.Assert(err, IsNil) 77 c.Assert(u1, Equals, u) 78 79 b2 := []byte{} 80 u2 := UUID{} 81 err = u2.UnmarshalBinary(b2) 82 c.Assert(err, NotNil) 83 } 84 85 func (s *codecTestSuite) TestFromString(c *C) { 86 u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8} 87 88 s1 := "6ba7b810-9dad-11d1-80b4-00c04fd430c8" 89 s2 := "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}" 90 s3 := "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8" 91 s4 := "6ba7b8109dad11d180b400c04fd430c8" 92 s5 := "urn:uuid:6ba7b8109dad11d180b400c04fd430c8" 93 94 _, err := FromString("") 95 c.Assert(err, NotNil) 96 97 u1, err := FromString(s1) 98 c.Assert(err, IsNil) 99 c.Assert(u1, Equals, u) 100 101 u2, err := FromString(s2) 102 c.Assert(err, IsNil) 103 c.Assert(u2, Equals, u) 104 105 u3, err := FromString(s3) 106 c.Assert(err, IsNil) 107 c.Assert(u3, Equals, u) 108 109 u4, err := FromString(s4) 110 c.Assert(err, IsNil) 111 c.Assert(u4, Equals, u) 112 113 u5, err := FromString(s5) 114 c.Assert(err, IsNil) 115 c.Assert(u5, Equals, u) 116 } 117 118 func (s *codecTestSuite) BenchmarkFromString(c *C) { 119 str := "6ba7b810-9dad-11d1-80b4-00c04fd430c8" 120 for i := 0; i < c.N; i++ { 121 FromString(str) 122 } 123 } 124 125 func (s *codecTestSuite) BenchmarkFromStringUrn(c *C) { 126 str := "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8" 127 for i := 0; i < c.N; i++ { 128 FromString(str) 129 } 130 } 131 132 func (s *codecTestSuite) BenchmarkFromStringWithBrackets(c *C) { 133 str := "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}" 134 for i := 0; i < c.N; i++ { 135 FromString(str) 136 } 137 } 138 139 func (s *codecTestSuite) TestFromStringShort(c *C) { 140 // Invalid 35-character UUID string 141 s1 := "6ba7b810-9dad-11d1-80b4-00c04fd430c" 142 143 for i := len(s1); i >= 0; i-- { 144 _, err := FromString(s1[:i]) 145 c.Assert(err, NotNil) 146 } 147 } 148 149 func (s *codecTestSuite) TestFromStringLong(c *C) { 150 // Invalid 37+ character UUID string 151 strings := []string{ 152 "6ba7b810-9dad-11d1-80b4-00c04fd430c8=", 153 "6ba7b810-9dad-11d1-80b4-00c04fd430c8}", 154 "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}f", 155 "6ba7b810-9dad-11d1-80b4-00c04fd430c800c04fd430c8", 156 } 157 158 for _, str := range strings { 159 _, err := FromString(str) 160 c.Assert(err, NotNil) 161 } 162 } 163 164 func (s *codecTestSuite) TestFromStringInvalid(c *C) { 165 // Invalid UUID string formats 166 strings := []string{ 167 "6ba7b8109dad11d180b400c04fd430c86ba7b8109dad11d180b400c04fd430c8", 168 "urn:uuid:{6ba7b810-9dad-11d1-80b4-00c04fd430c8}", 169 "uuid:urn:6ba7b810-9dad-11d1-80b4-00c04fd430c8", 170 "uuid:urn:6ba7b8109dad11d180b400c04fd430c8", 171 "6ba7b8109-dad-11d1-80b4-00c04fd430c8", 172 "6ba7b810-9dad1-1d1-80b4-00c04fd430c8", 173 "6ba7b810-9dad-11d18-0b4-00c04fd430c8", 174 "6ba7b810-9dad-11d1-80b40-0c04fd430c8", 175 "6ba7b810+9dad+11d1+80b4+00c04fd430c8", 176 "(6ba7b810-9dad-11d1-80b4-00c04fd430c8}", 177 "{6ba7b810-9dad-11d1-80b4-00c04fd430c8>", 178 "zba7b810-9dad-11d1-80b4-00c04fd430c8", 179 "6ba7b810-9dad11d180b400c04fd430c8", 180 "6ba7b8109dad-11d180b400c04fd430c8", 181 "6ba7b8109dad11d1-80b400c04fd430c8", 182 "6ba7b8109dad11d180b4-00c04fd430c8", 183 } 184 185 for _, str := range strings { 186 _, err := FromString(str) 187 c.Assert(err, NotNil) 188 } 189 } 190 191 func (s *codecTestSuite) TestFromStringOrNil(c *C) { 192 u := FromStringOrNil("") 193 c.Assert(u, Equals, Nil) 194 } 195 196 func (s *codecTestSuite) TestFromBytesOrNil(c *C) { 197 b := []byte{} 198 u := FromBytesOrNil(b) 199 c.Assert(u, Equals, Nil) 200 } 201 202 func (s *codecTestSuite) TestMarshalText(c *C) { 203 u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8} 204 b1 := []byte("6ba7b810-9dad-11d1-80b4-00c04fd430c8") 205 206 b2, err := u.MarshalText() 207 c.Assert(err, IsNil) 208 c.Assert(bytes.Equal(b1, b2), Equals, true) 209 } 210 211 func (s *codecTestSuite) BenchmarkMarshalText(c *C) { 212 u := NewV4() 213 for i := 0; i < c.N; i++ { 214 u.MarshalText() 215 } 216 } 217 218 func (s *codecTestSuite) TestUnmarshalText(c *C) { 219 u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8} 220 b1 := []byte("6ba7b810-9dad-11d1-80b4-00c04fd430c8") 221 222 u1 := UUID{} 223 err := u1.UnmarshalText(b1) 224 c.Assert(err, IsNil) 225 c.Assert(u1, Equals, u) 226 227 b2 := []byte("") 228 u2 := UUID{} 229 err = u2.UnmarshalText(b2) 230 c.Assert(err, NotNil) 231 } 232 233 func (s *codecTestSuite) BenchmarkUnmarshalText(c *C) { 234 bytes := []byte("6ba7b810-9dad-11d1-80b4-00c04fd430c8") 235 u := UUID{} 236 for i := 0; i < c.N; i++ { 237 u.UnmarshalText(bytes) 238 } 239 } 240 241 var sink string 242 243 func (s *codecTestSuite) BenchmarkMarshalToString(c *C) { 244 u := NewV4() 245 for i := 0; i < c.N; i++ { 246 sink = u.String() 247 } 248 }