summaryrefslogtreecommitdiff
path: root/Tests/AK/TestFormat.cpp
blob: 28696cce28fe44463893b303de92e00bfd612ccf (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
 * Copyright (c) 2020, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibTest/TestCase.h>

#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <AK/Vector.h>

TEST_CASE(is_integral_works_properly)
{
    EXPECT(!IsIntegral<const char*>);
    EXPECT(IsIntegral<unsigned long>);
}

TEST_CASE(format_string_literals)
{
    EXPECT_EQ(String::formatted("prefix-{}-suffix", "abc"), "prefix-abc-suffix");
    EXPECT_EQ(String::formatted("{}{}{}", "a", "b", "c"), "abc");
}

TEST_CASE(format_integers)
{
    EXPECT_EQ(String::formatted("{}", 42u), "42");
    EXPECT_EQ(String::formatted("{:4}", 42u), "  42");
    EXPECT_EQ(String::formatted("{:08}", 42u), "00000042");
    EXPECT_EQ(String::formatted("{:7}", -17), "    -17");
    EXPECT_EQ(String::formatted("{}", -17), "-17");
    EXPECT_EQ(String::formatted("{:04}", 13), "0013");
    EXPECT_EQ(String::formatted("{:08x}", 4096), "00001000");
    EXPECT_EQ(String::formatted("{:x}", 0x1111222233334444ull), "1111222233334444");
    EXPECT_EQ(String::formatted("{:4}", 12345678), "12345678");
}

TEST_CASE(reorder_format_arguments)
{
    EXPECT_EQ(String::formatted("{1}{0}", "a", "b"), "ba");
    EXPECT_EQ(String::formatted("{0}{1}", "a", "b"), "ab");
    // Compiletime check bypass: ignoring a passed argument.
    EXPECT_EQ(String::formatted("{0}{0}{0}"sv, "a", "b"), "aaa");
    // Compiletime check bypass: ignoring a passed argument.
    EXPECT_EQ(String::formatted("{1}{}{0}"sv, "a", "b", "c"), "baa");
}

TEST_CASE(escape_braces)
{
    EXPECT_EQ(String::formatted("{{{}", "foo"), "{foo");
    EXPECT_EQ(String::formatted("{}}}", "bar"), "bar}");
}

TEST_CASE(everything)
{
    EXPECT_EQ(String::formatted("{{{:04}/{}/{0:8}/{1}", 42u, "foo"), "{0042/foo/      42/foo");
}

TEST_CASE(string_builder)
{
    StringBuilder builder;
    builder.appendff(" {}  ", 42);
    builder.appendff("{1}{0} ", 1, 2);

    EXPECT_EQ(builder.to_string(), " 42  21 ");
}

TEST_CASE(format_without_arguments)
{
    EXPECT_EQ(String::formatted("foo"), "foo");
}

TEST_CASE(format_upper_case_integer)
{
    EXPECT_EQ(String::formatted("{:4X}", 0xff), "  FF");
    EXPECT_EQ(String::formatted("{:#4X}", 0xff), "0XFF");

    EXPECT_EQ(String::formatted("{:b}", 0xff), "11111111");
    EXPECT_EQ(String::formatted("{:B}", 0xff), "11111111");
    EXPECT_EQ(String::formatted("{:#b}", 0xff), "0b11111111");
}

TEST_CASE(format_aligned)
{
    EXPECT_EQ(String::formatted("{:*<8}", 13), "13******");
    EXPECT_EQ(String::formatted("{:*^8}", 13), "***13***");
    EXPECT_EQ(String::formatted("{:*>8}", 13), "******13");
    EXPECT_EQ(String::formatted("{:*>+8}", 13), "*****+13");
    EXPECT_EQ(String::formatted("{:*^ 8}", 13), "** 13***");
}

TEST_CASE(format_octal)
{
    EXPECT_EQ(String::formatted("{:o}", 0744), "744");
    EXPECT_EQ(String::formatted("{:#o}", 0744), "0744");
}

TEST_CASE(zero_pad)
{
    EXPECT_EQ(String::formatted("{: <010}", 42), "42        ");
    EXPECT_EQ(String::formatted("{:010}", 42), "0000000042");
    EXPECT_EQ(String::formatted("{:/^010}", 42), "////42////");
    EXPECT_EQ(String::formatted("{:04x}", -32), "-0020");
    EXPECT_EQ(String::formatted("{:#06x}", -64), "-0x000040");
}

TEST_CASE(replacement_field)
{
    EXPECT_EQ(String::formatted("{:*>{1}}", 13, static_cast<size_t>(10)), "********13");
    EXPECT_EQ(String::formatted("{:*<{1}}", 7, 4), "7***");
    // Compiletime check bypass: intentionally ignoring extra arguments
    EXPECT_EQ(String::formatted("{:{2}}"sv, -5, 8, 16), "              -5");
    EXPECT_EQ(String::formatted("{{{:*^{1}}}}", 1, 3), "{*1*}");
    EXPECT_EQ(String::formatted("{:0{}}", 1, 3), "001");
}

TEST_CASE(replacement_field_regression)
{
    // FIXME: Compiletime check bypass: cannot parse '}}' correctly.
    EXPECT_EQ(String::formatted("{:{}}"sv, "", static_cast<unsigned long>(6)), "      ");
}

TEST_CASE(complex_string_specifiers)
{
    EXPECT_EQ(String::formatted("{:.8}", "123456789"), "12345678");
    EXPECT_EQ(String::formatted("{:9}", "abcd"), "abcd     ");
    EXPECT_EQ(String::formatted("{:>9}", "abcd"), "     abcd");
    EXPECT_EQ(String::formatted("{:^9}", "abcd"), "  abcd   ");
    EXPECT_EQ(String::formatted("{:4.6}", "a"), "a   ");
    EXPECT_EQ(String::formatted("{:4.6}", "abcdef"), "abcdef");
    EXPECT_EQ(String::formatted("{:4.6}", "abcdefghi"), "abcdef");
}

TEST_CASE(cast_integer_to_character)
{
    EXPECT_EQ(String::formatted("{:c}", static_cast<int>('a')), "a");
    EXPECT_EQ(String::formatted("{:c}", static_cast<unsigned int>('f')), "f");
}

TEST_CASE(boolean_values)
{
    EXPECT_EQ(String::formatted("{}", true), "true");
    EXPECT_EQ(String::formatted("{}", false), "false");
    EXPECT_EQ(String::formatted("{:6}", true), "true  ");
    EXPECT_EQ(String::formatted("{:>4}", false), "false");
    EXPECT_EQ(String::formatted("{:d}", false), "0");
    EXPECT_EQ(String::formatted("{:d}", true), "1");
    EXPECT_EQ(String::formatted("{:#08x}", true), "0x00000001");
}

TEST_CASE(pointers)
{
    void* ptr = reinterpret_cast<void*>(0x4000);

    if (sizeof(void*) == 4) {
        EXPECT_EQ(String::formatted("{:p}", 32), "0x00000020");
        EXPECT_EQ(String::formatted("{:p}", ptr), "0x00004000");
        EXPECT_EQ(String::formatted("{}", ptr), "0x00004000");
    } else if (sizeof(void*) == 8) {
        EXPECT_EQ(String::formatted("{:p}", 32), "0x0000000000000020");
        EXPECT_EQ(String::formatted("{:p}", ptr), "0x0000000000004000");
        EXPECT_EQ(String::formatted("{}", ptr), "0x0000000000004000");
    } else {
        VERIFY_NOT_REACHED();
    }
}

// If the format implementation did absolutely nothing, all tests would pass. This
// is because when a test fails we only write "FAIL" to stdout using format.
//
// This is a bit scary, thus this test. At least this test should fail in this case.
TEST_CASE(ensure_that_format_works)
{
    if (String::formatted("FAIL") != "FAIL") {
        fprintf(stderr, "FAIL\n");
        exit(1);
    }

    if (String::formatted("{} FAIL {}", 1, 2) != "1 FAIL 2") {
        fprintf(stderr, "FAIL\n");
        exit(1);
    }
}

TEST_CASE(format_string_literal_as_pointer)
{
    const char* literal = "abc";
    EXPECT_EQ(String::formatted("{:p}", literal), String::formatted("{:p}", reinterpret_cast<FlatPtr>(literal)));
}

TEST_CASE(format_character)
{
    char a = 'a';
    EXPECT_EQ(String::formatted("{}", true ? a : 'b'), "a");
}

struct A {
};
struct B {
};
template<>
struct AK::Formatter<B> : Formatter<StringView> {
    void format(FormatBuilder& builder, B)
    {
        Formatter<StringView>::format(builder, "B");
    }
};

TEST_CASE(format_if_supported)
{
    EXPECT_EQ(String::formatted("{}", FormatIfSupported { A {} }), "?");
    EXPECT_EQ(String::formatted("{}", FormatIfSupported { B {} }), "B");
}

TEST_CASE(file_descriptor)
{
    char filename[] = "/tmp/test-file-descriptor-XXXXXX";

    int fd = mkstemp(filename);
    FILE* file = fdopen(fd, "w+");

    outln(file, "{}", "Hello, World!");
    out(file, "foo");
    outln(file, "bar");

    rewind(file);

    Array<u8, 256> buffer;
    const auto nread = fread(buffer.data(), 1, buffer.size(), file);

    EXPECT_EQ("Hello, World!\nfoobar\n"sv, StringView { buffer.span().trim(nread) });

    fclose(file);
}

TEST_CASE(floating_point_numbers)
{
    EXPECT_EQ(String::formatted("{}", 1.12), "1.12");
    EXPECT_EQ(String::formatted("{}", 1.), "1");
    EXPECT_EQ(String::formatted("{:.3}", 1.12), "1.12");
    EXPECT_EQ(String::formatted("{:.1}", 1.12), "1.1");
    EXPECT_EQ(String::formatted("{}", -1.12), "-1.12");

    // FIXME: There is always the question what we mean with the width field. Do we mean significant digits?
    //        Do we mean the whole width? This is what was the simplest to implement:
    EXPECT_EQ(String::formatted("{:x>5.1}", 1.12), "xx1.1");
}

TEST_CASE(no_precision_no_trailing_number)
{
    EXPECT_EQ(String::formatted("{:.0}", 0.1), "0");
}

TEST_CASE(yay_this_implementation_sucks)
{
    EXPECT_EQ(String::formatted("{:.0}", .99999999999), "0");
}

TEST_CASE(precision_with_trailing_zeros)
{
    EXPECT_EQ(String::formatted("{:0.3}", 1.12), "1.120");
    EXPECT_EQ(String::formatted("{:0.1}", 1.12), "1.1");
}

TEST_CASE(magnitude_less_than_zero)
{
    EXPECT_EQ(String::formatted("{}", -0.654), "-0.654");
    EXPECT_EQ(String::formatted("{}", 0.654), "0.654");
}

TEST_CASE(format_nullptr)
{
    EXPECT_EQ(String::formatted("{}", nullptr), String::formatted("{:p}", static_cast<FlatPtr>(0)));
}

struct C {
    int i;
};
template<>
struct AK::Formatter<C> : AK::Formatter<FormatString> {
    void format(FormatBuilder& builder, C c)
    {
        return AK::Formatter<FormatString>::format(builder, "C(i={})", c.i);
    }
};

TEST_CASE(use_format_string_formatter)
{
    EXPECT_EQ(String::formatted("{:*<10}", C { 42 }), "C(i=42)***");
}

TEST_CASE(long_long_regression)
{
    EXPECT_EQ(String::formatted("{}", 0x0123456789abcdefLL), "81985529216486895");

    StringBuilder builder;
    AK::FormatBuilder fmtbuilder { builder };
    fmtbuilder.put_i64(0x0123456789abcdefLL);

    EXPECT_EQ(builder.string_view(), "81985529216486895");
}

TEST_CASE(hex_dump)
{
    EXPECT_EQ(String::formatted("{:hex-dump}", "0000"), "30303030");
    EXPECT_EQ(String::formatted("{:>4hex-dump}", "0000"), "30303030    0000");
    EXPECT_EQ(String::formatted("{:>2hex-dump}", "0000"), "3030    00\n3030    00");
    EXPECT_EQ(String::formatted("{:*>4hex-dump}", "0000"), "30303030****0000");
}

TEST_CASE(vector_format)
{
    {
        Vector<int> v { 1, 2, 3, 4 };
        EXPECT_EQ(String::formatted("{}", v), "[ 1, 2, 3, 4 ]");
    }
    {
        Vector<StringView> v { "1"sv, "2"sv, "3"sv, "4"sv };
        EXPECT_EQ(String::formatted("{}", v), "[ 1, 2, 3, 4 ]");
    }
    {
        Vector<Vector<String>> v { { "1"sv, "2"sv }, { "3"sv, "4"sv } };
        EXPECT_EQ(String::formatted("{}", v), "[ [ 1, 2 ], [ 3, 4 ] ]");
    }
}