github.com/mymmsc/gox@v1.3.33/util/uuid/generator_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 . "gopkg.in/check.v1" 26 ) 27 28 type genTestSuite struct{} 29 30 var _ = Suite(&genTestSuite{}) 31 32 func (s *genTestSuite) TestNewV1(c *C) { 33 u := NewV1() 34 c.Assert(u.Version(), Equals, V1) 35 c.Assert(u.Variant(), Equals, VariantRFC4122) 36 37 u1 := NewV1() 38 u2 := NewV1() 39 c.Assert(u1, Not(Equals), u2) 40 41 oldFunc := epochFunc 42 epochFunc = func() uint64 { return 0 } 43 44 u3 := NewV1() 45 u4 := NewV1() 46 c.Assert(u3, Not(Equals), u4) 47 48 epochFunc = oldFunc 49 } 50 51 func (s *genTestSuite) BenchmarkNewV1(c *C) { 52 for i := 0; i < c.N; i++ { 53 NewV1() 54 } 55 } 56 57 func (s *genTestSuite) TestNewV2(c *C) { 58 u1 := NewV2(DomainPerson) 59 c.Assert(u1.Version(), Equals, V2) 60 c.Assert(u1.Variant(), Equals, VariantRFC4122) 61 62 u2 := NewV2(DomainGroup) 63 c.Assert(u2.Version(), Equals, V2) 64 c.Assert(u2.Variant(), Equals, VariantRFC4122) 65 } 66 67 func (s *genTestSuite) BenchmarkNewV2(c *C) { 68 for i := 0; i < c.N; i++ { 69 NewV2(DomainPerson) 70 } 71 } 72 73 func (s *genTestSuite) TestNewV3(c *C) { 74 u := NewV3(NamespaceDNS, "www.example.com") 75 c.Assert(u.Version(), Equals, V3) 76 c.Assert(u.Variant(), Equals, VariantRFC4122) 77 c.Assert(u.String(), Equals, "5df41881-3aed-3515-88a7-2f4a814cf09e") 78 79 u = NewV3(NamespaceDNS, "python.org") 80 c.Assert(u.String(), Equals, "6fa459ea-ee8a-3ca4-894e-db77e160355e") 81 82 u1 := NewV3(NamespaceDNS, "golang.org") 83 u2 := NewV3(NamespaceDNS, "golang.org") 84 c.Assert(u1, Equals, u2) 85 86 u3 := NewV3(NamespaceDNS, "example.com") 87 c.Assert(u1, Not(Equals), u3) 88 89 u4 := NewV3(NamespaceURL, "golang.org") 90 c.Assert(u1, Not(Equals), u4) 91 } 92 93 func (s *genTestSuite) BenchmarkNewV3(c *C) { 94 for i := 0; i < c.N; i++ { 95 NewV3(NamespaceDNS, "www.example.com") 96 } 97 } 98 99 func (s *genTestSuite) TestNewV4(c *C) { 100 u := NewV4() 101 c.Assert(u.Version(), Equals, V4) 102 c.Assert(u.Variant(), Equals, VariantRFC4122) 103 } 104 105 func (s *genTestSuite) BenchmarkNewV4(c *C) { 106 for i := 0; i < c.N; i++ { 107 NewV4() 108 } 109 } 110 111 func (s *genTestSuite) TestNewV5(c *C) { 112 u := NewV5(NamespaceDNS, "www.example.com") 113 c.Assert(u.Version(), Equals, V5) 114 c.Assert(u.Variant(), Equals, VariantRFC4122) 115 116 u = NewV5(NamespaceDNS, "python.org") 117 c.Assert(u.String(), Equals, "886313e1-3b8a-5372-9b90-0c9aee199e5d") 118 119 u1 := NewV5(NamespaceDNS, "golang.org") 120 u2 := NewV5(NamespaceDNS, "golang.org") 121 c.Assert(u1, Equals, u2) 122 123 u3 := NewV5(NamespaceDNS, "example.com") 124 c.Assert(u1, Not(Equals), u3) 125 126 u4 := NewV5(NamespaceURL, "golang.org") 127 c.Assert(u1, Not(Equals), u4) 128 } 129 130 func (s *genTestSuite) BenchmarkNewV5(c *C) { 131 for i := 0; i < c.N; i++ { 132 NewV5(NamespaceDNS, "www.example.com") 133 } 134 }