uuid_test.go 3.2 KB

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