aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/string/unittests/format_ut.cpp
blob: 58c757be0d0a4a91e3fa6741c1e0e8cfa0b44729 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <library/cpp/testing/gtest/gtest.h>
 
#include <library/cpp/yt/string/format.h> 
 
#include <library/cpp/yt/small_containers/compact_vector.h>

#include <limits> 
 
namespace NYT { 
namespace { 
 
//////////////////////////////////////////////////////////////////////////////// 
 
// Some compile-time sanity checks. 
static_assert(TFormatTraits<int>::HasCustomFormatValue); 
static_assert(TFormatTraits<double>::HasCustomFormatValue); 
static_assert(TFormatTraits<void*>::HasCustomFormatValue); 
static_assert(TFormatTraits<const char*>::HasCustomFormatValue); 
static_assert(TFormatTraits<TStringBuf>::HasCustomFormatValue); 
static_assert(TFormatTraits<TString>::HasCustomFormatValue); 
static_assert(TFormatTraits<std::vector<int>>::HasCustomFormatValue); 

// N.B. TCompactVector<int, 1> is not buildable on Windows
static_assert(TFormatTraits<TCompactVector<int, 2>>::HasCustomFormatValue);
static_assert(TFormatTraits<std::set<int>>::HasCustomFormatValue); 
static_assert(TFormatTraits<std::map<int, int>>::HasCustomFormatValue); 
static_assert(TFormatTraits<std::multimap<int, int>>::HasCustomFormatValue); 
static_assert(TFormatTraits<THashSet<int>>::HasCustomFormatValue); 
static_assert(TFormatTraits<THashMap<int, int>>::HasCustomFormatValue); 
static_assert(TFormatTraits<THashMultiSet<int>>::HasCustomFormatValue); 
static_assert(TFormatTraits<std::pair<int, int>>::HasCustomFormatValue); 
static_assert(TFormatTraits<std::optional<int>>::HasCustomFormatValue); 
static_assert(TFormatTraits<TDuration>::HasCustomFormatValue); 
static_assert(TFormatTraits<TInstant>::HasCustomFormatValue); 
 
struct TUnformattable 
{ }; 
static_assert(!TFormatTraits<TUnformattable>::HasCustomFormatValue); 
 
//////////////////////////////////////////////////////////////////////////////// 
 
TEST(TFormatTest, Nothing) 
{ 
    EXPECT_EQ("abc", Format("a%nb%nc", 1, 2)); 
} 
 
TEST(TFormatTest, Verbatim) 
{ 
    EXPECT_EQ("", Format("")); 
    EXPECT_EQ("test", Format("test")); 
    EXPECT_EQ("%", Format("%%")); 
    EXPECT_EQ("%hello%world%", Format("%%hello%%world%%")); 
} 
 
TEST(TFormatTest, MultipleArgs) 
{ 
    EXPECT_EQ("2+2=4", Format("%v+%v=%v", 2, 2, 4)); 
} 
 
TEST(TFormatTest, Strings) 
{ 
    EXPECT_EQ("test", Format("%s", "test")); 
    EXPECT_EQ("test", Format("%s", TStringBuf("test"))); 
    EXPECT_EQ("test", Format("%s", TString("test"))); 
 
    EXPECT_EQ("   abc", Format("%6s", TString("abc"))); 
    EXPECT_EQ("abc   ", Format("%-6s", TString("abc"))); 
    EXPECT_EQ("       abc", Format("%10v", TString("abc"))); 
    EXPECT_EQ("abc       ", Format("%-10v", TString("abc"))); 
    EXPECT_EQ("abc", Format("%2s", TString("abc"))); 
    EXPECT_EQ("abc", Format("%-2s", TString("abc"))); 
    EXPECT_EQ("abc", Format("%0s", TString("abc"))); 
    EXPECT_EQ("abc", Format("%-0s", TString("abc"))); 
    EXPECT_EQ(100, std::ssize(Format("%100v", "abc")));
} 
 
TEST(TFormatTest, Integers) 
{ 
    EXPECT_EQ("123", Format("%d", 123)); 
    EXPECT_EQ("123", Format("%v", 123)); 
 
    EXPECT_EQ("042", Format("%03d", 42));
    EXPECT_EQ("42", Format("%01d", 42));

    EXPECT_EQ("2147483647", Format("%d", std::numeric_limits<i32>::max())); 
    EXPECT_EQ("-2147483648", Format("%d", std::numeric_limits<i32>::min())); 
 
    EXPECT_EQ("0", Format("%u", 0U)); 
    EXPECT_EQ("0", Format("%v", 0U)); 
    EXPECT_EQ("4294967295", Format("%u", std::numeric_limits<ui32>::max())); 
    EXPECT_EQ("4294967295", Format("%v", std::numeric_limits<ui32>::max())); 
 
    EXPECT_EQ("9223372036854775807", Format("%" PRId64, std::numeric_limits<i64>::max())); 
    EXPECT_EQ("9223372036854775807", Format("%v", std::numeric_limits<i64>::max())); 
    EXPECT_EQ("-9223372036854775808", Format("%" PRId64, std::numeric_limits<i64>::min())); 
    EXPECT_EQ("-9223372036854775808", Format("%v", std::numeric_limits<i64>::min())); 
 
    EXPECT_EQ("0", Format("%" PRIu64, 0ULL)); 
    EXPECT_EQ("0", Format("%v", 0ULL)); 
    EXPECT_EQ("18446744073709551615", Format("%" PRIu64, std::numeric_limits<ui64>::max())); 
    EXPECT_EQ("18446744073709551615", Format("%v", std::numeric_limits<ui64>::max())); 
} 
 
TEST(TFormatTest, Floats) 
{ 
    EXPECT_EQ("3.14", Format("%.2f", 3.1415F)); 
    EXPECT_EQ("3.14", Format("%.2v", 3.1415F)); 
    EXPECT_EQ("3.14", Format("%.2lf", 3.1415)); 
    EXPECT_EQ("3.14", Format("%.2v", 3.1415)); 
    EXPECT_EQ(TString(std::to_string(std::numeric_limits<double>::max())), 
            Format("%lF", std::numeric_limits<double>::max())); 
} 
 
TEST(TFormatTest, Bool) 
{ 
    EXPECT_EQ("True", Format("%v", true)); 
    EXPECT_EQ("False", Format("%v", false)); 
    EXPECT_EQ("true", Format("%lv", true)); 
    EXPECT_EQ("false", Format("%lv", false)); 
} 
 
TEST(TFormatTest, Quotes) 
{ 
    EXPECT_EQ("\"True\"", Format("%Qv", true)); 
    EXPECT_EQ("'False'", Format("%qv", false)); 
    EXPECT_EQ("'\\\'\"'", Format("%qv", "\'\"")); 
    EXPECT_EQ("\"\\x01\"", Format("%Qv", "\x1")); 
    EXPECT_EQ("'\\x1b'", Format("%qv", '\x1b')); 
} 
 
TEST(TFormatTest, Nullable) 
{ 
    EXPECT_EQ("1", Format("%v", std::make_optional<int>(1))); 
    EXPECT_EQ("<null>", Format("%v", std::nullopt)); 
    EXPECT_EQ("<null>", Format("%v", std::optional<int>())); 
    EXPECT_EQ("3.14", Format("%.2f", std::optional<double>(3.1415))); 
} 
 
TEST(TFormatTest, Pointers) 
{ 
    // No idea if pointer format is standardized, check against Sprintf. 
    auto p = reinterpret_cast<void*>(123); 
    EXPECT_EQ(Sprintf("%p", reinterpret_cast<void*>(123)), Format("%p", p)); 
} 
 
//////////////////////////////////////////////////////////////////////////////// 
 
} // namespace 
} // namespace NYT