uuid_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. json, err := uuid.MarshalJSON()
  55. if err != nil {
  56. t.Errorf("MarshalJSON #%d: %v", i, err)
  57. }
  58. expectedJson := `"` + testsUUID[i].input + `"`
  59. if string(json) != expectedJson {
  60. t.Errorf("MarshalJSON #%d: expected %d got %d", i, expectedJson, string(json))
  61. }
  62. }
  63. }
  64. func TestRandomUUID(t *testing.T) {
  65. for i := 0; i < 20; i++ {
  66. uuid, err := RandomUUID()
  67. if err != nil {
  68. t.Errorf("RandomUUID: %v", err)
  69. }
  70. if variant := uuid.Variant(); variant != VariantIETF {
  71. t.Errorf("wrong variant. expected %d got %d", VariantIETF, variant)
  72. }
  73. if version := uuid.Version(); version != 4 {
  74. t.Errorf("wrong version. expected %d got %d", 4, version)
  75. }
  76. }
  77. }
  78. func TestUUIDFromTime(t *testing.T) {
  79. date := time.Date(1982, 5, 5, 12, 34, 56, 0, time.UTC)
  80. uuid := UUIDFromTime(date)
  81. if uuid.Time() != date {
  82. t.Errorf("embedded time incorrect. Expected %v got %v", date, uuid.Time())
  83. }
  84. }
  85. func TestParseUUID(t *testing.T) {
  86. uuid, _ := ParseUUID("486f3a88-775b-11e3-ae07-d231feb1dc81")
  87. if uuid.Time().Truncate(time.Second) != time.Date(2014, 1, 7, 5, 19, 29, 0, time.UTC) {
  88. t.Errorf("Expected date of 1/7/2014 at 5:19:29, got %v", uuid.Time())
  89. }
  90. }
  91. func TestTimeUUID(t *testing.T) {
  92. var node []byte
  93. timestamp := int64(0)
  94. for i := 0; i < 20; i++ {
  95. uuid := TimeUUID()
  96. if variant := uuid.Variant(); variant != VariantIETF {
  97. t.Errorf("wrong variant. expected %d got %d", VariantIETF, variant)
  98. }
  99. if version := uuid.Version(); version != 1 {
  100. t.Errorf("wrong version. expected %d got %d", 1, version)
  101. }
  102. if n := uuid.Node(); !bytes.Equal(n, node) && i > 0 {
  103. t.Errorf("wrong node. expected %x, got %x", node, n)
  104. } else if i == 0 {
  105. node = n
  106. }
  107. ts := uuid.Timestamp()
  108. if ts < timestamp {
  109. t.Errorf("timestamps must grow")
  110. }
  111. timestamp = ts
  112. }
  113. }