uuid_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright (c) 2012 The gocql Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gocql
  5. import (
  6. "bytes"
  7. "testing"
  8. "time"
  9. )
  10. func TestUUIDNil(t *testing.T) {
  11. var uuid UUID
  12. want, got := "00000000-0000-0000-0000-000000000000", uuid.String()
  13. if want != got {
  14. t.Fatalf("TestNil: expected %q got %q", want, got)
  15. }
  16. }
  17. var testsUUID = []struct {
  18. input string
  19. variant int
  20. version int
  21. }{
  22. {"b4f00409-cef8-4822-802c-deb20704c365", VariantIETF, 4},
  23. {"f81d4fae-7dec-11d0-a765-00a0c91e6bf6", VariantIETF, 1},
  24. {"00000000-7dec-11d0-a765-00a0c91e6bf6", VariantIETF, 1},
  25. {"3051a8d7-aea7-1801-e0bf-bc539dd60cf3", VariantFuture, 1},
  26. {"3051a8d7-aea7-2801-e0bf-bc539dd60cf3", VariantFuture, 2},
  27. {"3051a8d7-aea7-3801-e0bf-bc539dd60cf3", VariantFuture, 3},
  28. {"3051a8d7-aea7-4801-e0bf-bc539dd60cf3", VariantFuture, 4},
  29. {"3051a8d7-aea7-3801-e0bf-bc539dd60cf3", VariantFuture, 5},
  30. {"d0e817e1-e4b1-1801-3fe6-b4b60ccecf9d", VariantNCSCompat, 0},
  31. {"d0e817e1-e4b1-1801-bfe6-b4b60ccecf9d", VariantIETF, 1},
  32. {"d0e817e1-e4b1-1801-dfe6-b4b60ccecf9d", VariantMicrosoft, 0},
  33. {"d0e817e1-e4b1-1801-ffe6-b4b60ccecf9d", VariantFuture, 0},
  34. }
  35. func TestPredefinedUUID(t *testing.T) {
  36. for i := range testsUUID {
  37. uuid, err := ParseUUID(testsUUID[i].input)
  38. if err != nil {
  39. t.Errorf("ParseUUID #%d: %v", i, err)
  40. continue
  41. }
  42. if str := uuid.String(); str != testsUUID[i].input {
  43. t.Errorf("String #%d: expected %q got %q", i, testsUUID[i].input, str)
  44. continue
  45. }
  46. if variant := uuid.Variant(); variant != testsUUID[i].variant {
  47. t.Errorf("Variant #%d: expected %d got %d", i, testsUUID[i].variant, variant)
  48. }
  49. if testsUUID[i].variant == VariantIETF {
  50. if version := uuid.Version(); version != testsUUID[i].version {
  51. t.Errorf("Version #%d: expected %d got %d", i, testsUUID[i].version, version)
  52. }
  53. }
  54. }
  55. }
  56. func TestRandomUUID(t *testing.T) {
  57. for i := 0; i < 20; i++ {
  58. uuid, err := RandomUUID()
  59. if err != nil {
  60. t.Errorf("RandomUUID: %v", err)
  61. }
  62. if variant := uuid.Variant(); variant != VariantIETF {
  63. t.Errorf("wrong variant. expected %d got %d", VariantIETF, variant)
  64. }
  65. if version := uuid.Version(); version != 4 {
  66. t.Errorf("wrong version. expected %d got %d", 4, version)
  67. }
  68. }
  69. }
  70. func TestUUIDFromTime(t *testing.T) {
  71. date := time.Date(1982, 5, 5, 12, 34, 56, 0, time.UTC)
  72. uuid := UUIDFromTime(date)
  73. if uuid.Time() != date {
  74. t.Errorf("embedded time incorrect. Expected %v got %v", date, uuid.Time())
  75. }
  76. }
  77. func TestParseUUID(t *testing.T) {
  78. uuid, _ := ParseUUID("486f3a88-775b-11e3-ae07-d231feb1dc81")
  79. if uuid.Time().Truncate(time.Second) != time.Date(2014, 1, 7, 5, 19, 29, 0, time.UTC) {
  80. t.Errorf("Expected date of 1/7/2014 at 5:19:29, got %v", uuid.Time())
  81. }
  82. }
  83. func TestTimeUUID(t *testing.T) {
  84. var node []byte
  85. timestamp := int64(0)
  86. for i := 0; i < 20; i++ {
  87. uuid := TimeUUID()
  88. if variant := uuid.Variant(); variant != VariantIETF {
  89. t.Errorf("wrong variant. expected %d got %d", VariantIETF, variant)
  90. }
  91. if version := uuid.Version(); version != 1 {
  92. t.Errorf("wrong version. expected %d got %d", 1, version)
  93. }
  94. if n := uuid.Node(); !bytes.Equal(n, node) && i > 0 {
  95. t.Errorf("wrong node. expected %x, got %x", node, n)
  96. } else if i == 0 {
  97. node = n
  98. }
  99. ts := uuid.Timestamp()
  100. if ts < timestamp {
  101. t.Errorf("timestamps must grow")
  102. }
  103. timestamp = ts
  104. }
  105. }