github.com/mymmsc/gox@v1.3.33/util/uuid/uuid_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  	"testing"
    27  
    28  	. "gopkg.in/check.v1"
    29  )
    30  
    31  // Hook up gocheck into the "go test" runner.
    32  func TestUUID(t *testing.T) { TestingT(t) }
    33  
    34  type testSuite struct{}
    35  
    36  var _ = Suite(&testSuite{})
    37  
    38  func (s *testSuite) TestBytes(c *C) {
    39  	u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
    40  
    41  	bytes1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
    42  
    43  	c.Assert(bytes.Equal(u.Bytes(), bytes1), Equals, true)
    44  }
    45  
    46  func (s *testSuite) TestString(c *C) {
    47  	c.Assert(NamespaceDNS.String(), Equals, "6ba7b810-9dad-11d1-80b4-00c04fd430c8")
    48  }
    49  
    50  func (s *testSuite) TestEqual(c *C) {
    51  	c.Assert(Equal(NamespaceDNS, NamespaceDNS), Equals, true)
    52  	c.Assert(Equal(NamespaceDNS, NamespaceURL), Equals, false)
    53  }
    54  
    55  func (s *testSuite) TestVersion(c *C) {
    56  	u := UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
    57  	c.Assert(u.Version(), Equals, V1)
    58  }
    59  
    60  func (s *testSuite) TestSetVersion(c *C) {
    61  	u := UUID{}
    62  	u.SetVersion(4)
    63  	c.Assert(u.Version(), Equals, V4)
    64  }
    65  
    66  func (s *testSuite) TestVariant(c *C) {
    67  	u1 := UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
    68  	c.Assert(u1.Variant(), Equals, VariantNCS)
    69  
    70  	u2 := UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
    71  	c.Assert(u2.Variant(), Equals, VariantRFC4122)
    72  
    73  	u3 := UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
    74  	c.Assert(u3.Variant(), Equals, VariantMicrosoft)
    75  
    76  	u4 := UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
    77  	c.Assert(u4.Variant(), Equals, VariantFuture)
    78  }
    79  
    80  func (s *testSuite) TestSetVariant(c *C) {
    81  	u := UUID{}
    82  	u.SetVariant(VariantNCS)
    83  	c.Assert(u.Variant(), Equals, VariantNCS)
    84  	u.SetVariant(VariantRFC4122)
    85  	c.Assert(u.Variant(), Equals, VariantRFC4122)
    86  	u.SetVariant(VariantMicrosoft)
    87  	c.Assert(u.Variant(), Equals, VariantMicrosoft)
    88  	u.SetVariant(VariantFuture)
    89  	c.Assert(u.Variant(), Equals, VariantFuture)
    90  }