guid_ut.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <library/cpp/testing/gtest/gtest.h>
  2. #include <library/cpp/yt/string/guid.h>
  3. #include <library/cpp/yt/string/format.h>
  4. #include <util/string/hex.h>
  5. namespace NYT {
  6. namespace {
  7. ////////////////////////////////////////////////////////////////////////////////
  8. static_assert(TFormatTraits<TGuid>::HasCustomFormatValue);
  9. TString CanonicalToString(TGuid value)
  10. {
  11. return Sprintf("%x-%x-%x-%x",
  12. value.Parts32[3],
  13. value.Parts32[2],
  14. value.Parts32[1],
  15. value.Parts32[0]);
  16. }
  17. const ui32 TrickyValues[] = {
  18. 0, 0x1, 0x12, 0x123, 0x1234, 0x12345, 0x123456, 0x1234567, 0x12345678
  19. };
  20. TEST(TGuidTest, FormatAllTricky)
  21. {
  22. for (ui32 a : TrickyValues) {
  23. for (ui32 b : TrickyValues) {
  24. for (ui32 c : TrickyValues) {
  25. for (ui32 d : TrickyValues) {
  26. auto value = TGuid(a, b, c, d);
  27. EXPECT_EQ(CanonicalToString(value), ToString(value));
  28. }
  29. }
  30. }
  31. }
  32. }
  33. TEST(TGuidTest, FormatAllSymbols)
  34. {
  35. const auto Value = TGuid::FromString("12345678-abcdef01-12345678-abcdef01");
  36. EXPECT_EQ(CanonicalToString(Value), ToString(Value));
  37. }
  38. TEST(TGuidTest, ByteOrder)
  39. {
  40. auto guid = TGuid::FromStringHex32("12345678ABCDEF0112345678ABCDEF01");
  41. TString bytes{reinterpret_cast<const char*>(&(guid.Parts32[0])), 16};
  42. EXPECT_EQ(HexEncode(bytes), "01EFCDAB7856341201EFCDAB78563412");
  43. }
  44. ////////////////////////////////////////////////////////////////////////////////
  45. } // namespace
  46. } // namespace NYT