aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/string/unittests/guid_ut.cpp
blob: 4b5eebea16ec3b310289ca59ba47c9cddb1a03f0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <library/cpp/testing/gtest/gtest.h>

#include <library/cpp/yt/string/guid.h>
#include <library/cpp/yt/string/format.h>

#include <util/string/hex.h>

namespace NYT {
namespace {

////////////////////////////////////////////////////////////////////////////////

static_assert(TFormatTraits<TGuid>::HasCustomFormatValue);

TString CanonicalToString(TGuid value)
{
   return Sprintf("%x-%x-%x-%x",
        value.Parts32[3],
        value.Parts32[2],
        value.Parts32[1],
        value.Parts32[0]);
}

const ui32 TrickyValues[] = {
    0, 0x1, 0x12, 0x123, 0x1234, 0x12345, 0x123456, 0x1234567, 0x12345678
};

TEST(TGuidTest, FormatAllTricky)
{
    for (ui32 a : TrickyValues) {
        for (ui32 b : TrickyValues) {
            for (ui32 c : TrickyValues) {
                for (ui32 d : TrickyValues) {
                    auto value = TGuid(a, b, c, d);
                    EXPECT_EQ(CanonicalToString(value), ToString(value));
                }
            }
        }
    }
}

TEST(TGuidTest, FormatAllSymbols)
{
    const auto Value = TGuid::FromString("12345678-abcdef01-12345678-abcdef01");
    EXPECT_EQ(CanonicalToString(Value), ToString(Value));
}

TEST(TGuidTest, ByteOrder)
{
    auto guid = TGuid::FromStringHex32("12345678ABCDEF0112345678ABCDEF01");
    TString bytes{reinterpret_cast<const char*>(&(guid.Parts32[0])), 16};
    EXPECT_EQ(HexEncode(bytes), "01EFCDAB7856341201EFCDAB78563412");
}

////////////////////////////////////////////////////////////////////////////////

} // namespace
} // namespace NYT