uuid_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright (c) 2012 by Christoph Hack <christoph@tux21b.org>
  2. // All rights reserved. Distributed under the Simplified BSD License.
  3. package uuid
  4. import (
  5. "bytes"
  6. "testing"
  7. )
  8. func TestNil(t *testing.T) {
  9. var uuid UUID
  10. want, got := "00000000-0000-0000-0000-000000000000", uuid.String()
  11. if want != got {
  12. t.Fatalf("TestNil: expected %q got %q", want, got)
  13. }
  14. }
  15. var tests = []struct {
  16. input string
  17. variant int
  18. version int
  19. }{
  20. {"b4f00409-cef8-4822-802c-deb20704c365", VariantIETF, 4},
  21. {"f81d4fae-7dec-11d0-a765-00a0c91e6bf6", VariantIETF, 1},
  22. {"00000000-7dec-11d0-a765-00a0c91e6bf6", VariantIETF, 1},
  23. {"3051a8d7-aea7-1801-e0bf-bc539dd60cf3", VariantFuture, 1},
  24. {"3051a8d7-aea7-2801-e0bf-bc539dd60cf3", VariantFuture, 2},
  25. {"3051a8d7-aea7-3801-e0bf-bc539dd60cf3", VariantFuture, 3},
  26. {"3051a8d7-aea7-4801-e0bf-bc539dd60cf3", VariantFuture, 4},
  27. {"3051a8d7-aea7-3801-e0bf-bc539dd60cf3", VariantFuture, 5},
  28. {"d0e817e1-e4b1-1801-3fe6-b4b60ccecf9d", VariantNCSCompat, 0},
  29. {"d0e817e1-e4b1-1801-bfe6-b4b60ccecf9d", VariantIETF, 1},
  30. {"d0e817e1-e4b1-1801-dfe6-b4b60ccecf9d", VariantMicrosoft, 0},
  31. {"d0e817e1-e4b1-1801-ffe6-b4b60ccecf9d", VariantFuture, 0},
  32. }
  33. func TestPredefined(t *testing.T) {
  34. for i := range tests {
  35. uuid, err := ParseUUID(tests[i].input)
  36. if err != nil {
  37. t.Errorf("ParseUUID #%d: %v", i, err)
  38. continue
  39. }
  40. if str := uuid.String(); str != tests[i].input {
  41. t.Errorf("String #%d: expected %q got %q", i, tests[i].input, str)
  42. continue
  43. }
  44. if variant := uuid.Variant(); variant != tests[i].variant {
  45. t.Errorf("Variant #%d: expected %d got %d", i, tests[i].variant, variant)
  46. }
  47. if tests[i].variant == VariantIETF {
  48. if version := uuid.Version(); version != tests[i].version {
  49. t.Errorf("Version #%d: expected %d got %d", i, tests[i].version, version)
  50. }
  51. }
  52. }
  53. }
  54. func TestRandomUUID(t *testing.T) {
  55. for i := 0; i < 20; i++ {
  56. uuid := RandomUUID()
  57. if variant := uuid.Variant(); variant != VariantIETF {
  58. t.Errorf("wrong variant. expected %d got %d", VariantIETF, variant)
  59. }
  60. if version := uuid.Version(); version != 4 {
  61. t.Errorf("wrong version. expected %d got %d", 4, version)
  62. }
  63. }
  64. }
  65. func TestTimeUUID(t *testing.T) {
  66. var node []byte
  67. timestamp := uint64(0)
  68. for i := 0; i < 20; i++ {
  69. uuid := TimeUUID()
  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 != 1 {
  74. t.Errorf("wrong version. expected %d got %d", 1, version)
  75. }
  76. if n := uuid.Node(); !bytes.Equal(n, node) && i > 0 {
  77. t.Errorf("wrong node. expected %x, got %x", node, n)
  78. } else if i == 0 {
  79. node = n
  80. }
  81. ts := uuid.Timestamp()
  82. if ts < timestamp {
  83. t.Errorf("timestamps must grow")
  84. }
  85. timestamp = ts
  86. }
  87. }