guid_ut.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include <library/cpp/testing/unittest/registar.h>
  2. #include "guid.h"
  3. Y_UNIT_TEST_SUITE(TGuidTest) {
  4. //TODO - make real constructor
  5. static TGUID Construct(ui32 d1, ui32 d2, ui32 d3, ui32 d4) {
  6. TGUID ret;
  7. ret.dw[0] = d1;
  8. ret.dw[1] = d2;
  9. ret.dw[2] = d3;
  10. ret.dw[3] = d4;
  11. return ret;
  12. }
  13. struct TTest {
  14. TGUID G;
  15. TString S;
  16. };
  17. Y_UNIT_TEST(Test1) {
  18. for (size_t i = 0; i < 1000; ++i) {
  19. TGUID g;
  20. CreateGuid(&g);
  21. UNIT_ASSERT_EQUAL(g, GetGuid(GetGuidAsString(g)));
  22. }
  23. }
  24. Y_UNIT_TEST(Test2) {
  25. const TTest tests[] = {
  26. {Construct(1, 1, 1, 1), "1-1-1-1"},
  27. {Construct(0, 0, 0, 0), "0-0-0-0"},
  28. {TGUID(), "H-0-0-0"},
  29. {TGUID(), "0-H-0-0"},
  30. {TGUID(), "0-0-H-0"},
  31. {TGUID(), "0-0-0-H"},
  32. {Construct(0x8cf813d9U, 0xc098da90U, 0x7ef58954U, 0x636d04dU), "8cf813d9-c098da90-7ef58954-636d04d"},
  33. {Construct(0x8cf813d9U, 0xc098da90U, 0x7ef58954U, 0x636d04dU), "8CF813D9-C098DA90-7EF58954-636D04D"},
  34. {Construct(0x12345678U, 0x90abcdefU, 0xfedcba09U, 0x87654321U), "12345678-90abcdef-FEDCBA09-87654321"},
  35. {Construct(0x1, 0x2, 0xabcdef, 0x400), "01-002-00ABCDEF-000400"},
  36. {TGUID(), "-1-1-1"}, // empty parts
  37. {TGUID(), "--1-1-1"},
  38. {TGUID(), "1--1-1"},
  39. {TGUID(), "1-1"}, // unexpected end
  40. {TGUID(), "1-1-"},
  41. {TGUID(), "1-1-1"},
  42. {TGUID(), "1-1-1-"},
  43. {TGUID(), "1-1-1-1-"},
  44. {TGUID(), "1-1-1-1-1"},
  45. {TGUID(), "1+1-1-1"}, // bad char
  46. {TGUID(), "1-1:3-1-1"},
  47. {Construct(0xffffffffU, 0xffffffffU, 0xffffffffU, 0xffffffffU), "FFFFFFFF-FFFFFFFF-FFFFFFFF-FFFFFFFF"}, // overflow
  48. {TGUID(), "FFFFFFFFA-FFFFFFFF-FFFFFFFF-FFFFFFFF"},
  49. {TGUID(), "100000000-0-0-0"},
  50. {Construct(1, 1, 1, 1), "0000001-0000000000000000000000000000000000000001-0001-00000001"},
  51. {Construct(0, 0, 0, 0), "000000000000-000000000000000000000000000000000000000-000-0"},
  52. };
  53. for (const auto& t : tests) {
  54. UNIT_ASSERT_EQUAL(t.G, GetGuid(t.S));
  55. }
  56. }
  57. Y_UNIT_TEST(Test3) {
  58. //if this test failed, please, fix buffer size in GetGuidAsString()
  59. TGUID max = Construct(Max<ui32>(), Max<ui32>(), Max<ui32>(), Max<ui32>());
  60. UNIT_ASSERT_EQUAL(GetGuidAsString(max).length(), 35);
  61. }
  62. Y_UNIT_TEST(Test4) {
  63. UNIT_ASSERT_VALUES_EQUAL(GetGuidAsString(Construct(1, 2, 3, 4)), "1-2-3-4");
  64. UNIT_ASSERT_VALUES_EQUAL(GetGuidAsString(Construct(1, 2, 0xFFFFFF, 4)), "1-2-ffffff-4");
  65. UNIT_ASSERT_VALUES_EQUAL(GetGuidAsString(Construct(0xFAFA, 2, 3, 4)), "fafa-2-3-4");
  66. UNIT_ASSERT_VALUES_EQUAL(GetGuidAsString(Construct(1, 0xADE, 3, 4)), "1-ade-3-4");
  67. UNIT_ASSERT_VALUES_EQUAL(GetGuidAsString(Construct(1, 2, 3, 0xDEAD)), "1-2-3-dead");
  68. }
  69. Y_UNIT_TEST(Test5) {
  70. const TTest tests[] = {
  71. {TGUID(), "1-1-1-1-1"},
  72. {TGUID(), "00000001-0001-0001-0001-00000000001-"},
  73. {Construct(0x10000001U, 0x10011001U, 0x10011001U, 0x10000001U), "10000001-1001-1001-1001-100110000001"},
  74. {Construct(0x550e8400U, 0xe29b41d4U, 0xa7164466U, 0x55440000U), "550e8400-e29b-41d4-a716-446655440000"},
  75. {Construct(0xffffffffU, 0xffffffffU, 0xffffffffU, 0xffffffffU), "ffffffff-ffff-ffff-ffff-ffffffffffff"},
  76. {TGUID(), "ffffffff-ffffff-ff-ffff-ffffffffffff"},
  77. {TGUID(), "ffffffff-ffff-ffff-ff-ffffffffffffff"}};
  78. for (const auto& t : tests) {
  79. UNIT_ASSERT_EQUAL(t.G, GetUuid(t.S));
  80. }
  81. }
  82. Y_UNIT_TEST(DoubleConvert) {
  83. /**
  84. * test print and parsing RFC4122 GUID, which described in
  85. * https://en.wikipedia.org/wiki/Universally_unique_identifier
  86. * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  87. **/
  88. auto guid = TGUID::Create();
  89. auto printed = guid.AsUuidString();
  90. TGUID read;
  91. UNIT_ASSERT(GetUuid(printed, read));
  92. UNIT_ASSERT_VALUES_EQUAL(guid.dw[0], read.dw[0]);
  93. UNIT_ASSERT_VALUES_EQUAL(guid.dw[1], read.dw[1]);
  94. UNIT_ASSERT_VALUES_EQUAL(guid.dw[2], read.dw[2]);
  95. UNIT_ASSERT_VALUES_EQUAL(guid.dw[3], read.dw[3]);
  96. }
  97. Y_UNIT_TEST(OutputFormat) {
  98. TGUID guid = Construct(0x00005612U, 0x12000000U, 0x00000123U, 0x00000000U);
  99. UNIT_ASSERT_VALUES_EQUAL(guid.AsGuidString(), "5612-12000000-123-0");
  100. UNIT_ASSERT_VALUES_EQUAL(guid.AsUuidString(), "00005612-1200-0000-0000-012300000000");
  101. }
  102. Y_UNIT_TEST(TimeBased) {
  103. TString guid = TGUID::CreateTimebased().AsUuidString();
  104. UNIT_ASSERT(!guid.empty());
  105. UNIT_ASSERT_EQUAL(guid[14], '1');
  106. }
  107. }