2020-09-19 11:47:35 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-09-19 11:47:35 +00:00
|
|
|
*/
|
|
|
|
|
2021-04-25 05:53:23 +00:00
|
|
|
#include <LibTest/TestCase.h>
|
2020-09-19 11:47:35 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
#include <AK/ByteString.h>
|
2020-09-23 11:21:18 +00:00
|
|
|
#include <AK/StringBuilder.h>
|
2021-07-17 22:27:12 +00:00
|
|
|
#include <AK/Vector.h>
|
2021-10-30 08:43:21 +00:00
|
|
|
#include <math.h>
|
2022-01-13 23:20:01 +00:00
|
|
|
#include <unistd.h>
|
2020-09-19 11:47:35 +00:00
|
|
|
|
2020-12-20 16:00:50 +00:00
|
|
|
TEST_CASE(is_integral_works_properly)
|
|
|
|
{
|
2022-04-01 17:58:27 +00:00
|
|
|
EXPECT(!IsIntegral<char const*>);
|
2021-04-10 13:59:06 +00:00
|
|
|
EXPECT(IsIntegral<unsigned long>);
|
2020-12-20 16:00:50 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 11:47:35 +00:00
|
|
|
TEST_CASE(format_string_literals)
|
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("prefix-{}-suffix", "abc"), "prefix-abc-suffix");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{}{}{}", "a", "b", "c"), "abc");
|
2020-09-19 11:47:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(format_integers)
|
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{}", 42u), "42");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:4}", 42u), " 42");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:08}", 42u), "00000042");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:7}", -17), " -17");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{}", -17), "-17");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:04}", 13), "0013");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:08x}", 4096), "00001000");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:x}", 0x1111222233334444ull), "1111222233334444");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:4}", 12345678), "12345678");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{}", AK::NumericLimits<i64>::min()), "-9223372036854775808");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:x}", AK::NumericLimits<i64>::min()), "-8000000000000000");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:'}", 0), "0");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:'}", 4096), "4,096");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:'}", 16777216), "16,777,216");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:'}", AK::NumericLimits<u64>::max()), "18,446,744,073,709,551,615");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:'}", AK::NumericLimits<u64>::max()), "18,446,744,073,709,551,615");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:'}", AK::NumericLimits<i64>::min() + 1), "-9,223,372,036,854,775,807");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:'x}", 0), "0");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:'x}", 16777216), "1,000,000");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:'x}", AK::NumericLimits<u64>::max()), "f,fff,fff,fff,fff,fff");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:'x}", AK::NumericLimits<i64>::min() + 1), "-7,fff,fff,fff,fff,fff");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:'b}", AK::NumericLimits<u64>::max()), "1,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111");
|
2020-09-19 11:47:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(reorder_format_arguments)
|
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{1}{0}", "a", "b"), "ba");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{0}{1}", "a", "b"), "ab");
|
2021-02-23 06:28:15 +00:00
|
|
|
// Compiletime check bypass: ignoring a passed argument.
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{0}{0}{0}"sv, "a", "b"), "aaa");
|
2021-02-23 06:28:15 +00:00
|
|
|
// Compiletime check bypass: ignoring a passed argument.
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{1}{}{0}"sv, "a", "b", "c"), "baa");
|
2020-09-19 11:47:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(escape_braces)
|
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{{{}", "foo"), "{foo");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{}}}", "bar"), "bar}");
|
2020-09-19 11:47:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(everything)
|
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{{{:04}/{}/{0:8}/{1}", 42u, "foo"), "{0042/foo/ 42/foo");
|
2020-09-19 11:47:35 +00:00
|
|
|
}
|
|
|
|
|
2020-09-22 15:47:22 +00:00
|
|
|
TEST_CASE(string_builder)
|
|
|
|
{
|
|
|
|
StringBuilder builder;
|
|
|
|
builder.appendff(" {} ", 42);
|
|
|
|
builder.appendff("{1}{0} ", 1, 2);
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(builder.to_byte_string(), " 42 21 ");
|
2020-09-22 15:47:22 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 09:01:32 +00:00
|
|
|
TEST_CASE(format_without_arguments)
|
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("foo"), "foo");
|
2020-09-23 09:01:32 +00:00
|
|
|
}
|
|
|
|
|
2020-09-26 13:26:14 +00:00
|
|
|
TEST_CASE(format_upper_case_integer)
|
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{:4X}", 0xff), " FF");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:#4X}", 0xff), "0XFF");
|
2020-09-26 13:26:14 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{:b}", 0xff), "11111111");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:B}", 0xff), "11111111");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:#b}", 0xff), "0b11111111");
|
2020-09-26 13:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(format_aligned)
|
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{:*<8}", 13), "13******");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:*^8}", 13), "***13***");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:*>8}", 13), "******13");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:*>+8}", 13), "*****+13");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:*^ 8}", 13), "** 13***");
|
2020-09-26 13:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(format_octal)
|
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{:o}", 0744), "744");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:#o}", 0744), "0744");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:'o}", 054321), "54,321");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:'o}", 0567012340), "567,012,340");
|
2020-09-26 13:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(zero_pad)
|
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{: <010}", 42), "42 ");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:010}", 42), "0000000042");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:/^010}", 42), "////42////");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:04x}", -32), "-0020");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:#06x}", -64), "-0x000040");
|
2020-09-26 13:26:14 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 07:19:36 +00:00
|
|
|
TEST_CASE(replacement_field)
|
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{:*>{1}}", 13, static_cast<size_t>(10)), "********13");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:*<{1}}", 7, 4), "7***");
|
2021-06-01 18:35:27 +00:00
|
|
|
// Compiletime check bypass: intentionally ignoring extra arguments
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{:{2}}"sv, -5, 8, 16), " -5");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{{{:*^{1}}}}", 1, 3), "{*1*}");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:0{}}", 1, 3), "001");
|
2020-09-28 07:19:36 +00:00
|
|
|
}
|
|
|
|
|
2020-12-20 16:00:50 +00:00
|
|
|
TEST_CASE(replacement_field_regression)
|
|
|
|
{
|
2021-02-23 06:28:15 +00:00
|
|
|
// FIXME: Compiletime check bypass: cannot parse '}}' correctly.
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{:{}}"sv, "", static_cast<unsigned long>(6)), " ");
|
2020-12-20 16:00:50 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 11:55:58 +00:00
|
|
|
TEST_CASE(complex_string_specifiers)
|
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{:.8}", "123456789"), "12345678");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:9}", "abcd"), "abcd ");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:>9}", "abcd"), " abcd");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:^9}", "abcd"), " abcd ");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:4.6}", "a"), "a ");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:4.6}", "abcdef"), "abcdef");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:4.6}", "abcdefghi"), "abcdef");
|
2020-09-29 11:55:58 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 12:07:39 +00:00
|
|
|
TEST_CASE(cast_integer_to_character)
|
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{:c}", static_cast<int>('a')), "a");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:c}", static_cast<unsigned int>('f')), "f");
|
2020-09-29 12:07:39 +00:00
|
|
|
}
|
|
|
|
|
2020-09-30 11:26:24 +00:00
|
|
|
TEST_CASE(boolean_values)
|
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{}", true), "true");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{}", false), "false");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:6}", true), "true ");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:>4}", false), "false");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:d}", false), "0");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:d}", true), "1");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:#08x}", true), "0x00000001");
|
2020-09-30 11:26:24 +00:00
|
|
|
}
|
|
|
|
|
2020-09-30 12:38:47 +00:00
|
|
|
TEST_CASE(pointers)
|
|
|
|
{
|
|
|
|
void* ptr = reinterpret_cast<void*>(0x4000);
|
|
|
|
|
|
|
|
if (sizeof(void*) == 4) {
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{:p}", 32), "0x00000020");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:p}", ptr), "0x00004000");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{}", ptr), "0x00004000");
|
2020-09-30 12:38:47 +00:00
|
|
|
} else if (sizeof(void*) == 8) {
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{:p}", 32), "0x0000000000000020");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:p}", ptr), "0x0000000000004000");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{}", ptr), "0x0000000000004000");
|
2020-09-30 12:38:47 +00:00
|
|
|
} else {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-09-30 12:38:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-02 13:21:30 +00:00
|
|
|
// 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)
|
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
if (ByteString::formatted("FAIL") != "FAIL") {
|
2020-10-02 13:21:30 +00:00
|
|
|
fprintf(stderr, "FAIL\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
if (ByteString::formatted("{} FAIL {}", 1, 2) != "1 FAIL 2") {
|
2020-10-02 13:21:30 +00:00
|
|
|
fprintf(stderr, "FAIL\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 11:57:20 +00:00
|
|
|
TEST_CASE(format_string_literal_as_pointer)
|
|
|
|
{
|
2022-04-01 17:58:27 +00:00
|
|
|
char const* literal = "abc";
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{:p}", literal), ByteString::formatted("{:p}", reinterpret_cast<FlatPtr>(literal)));
|
2020-10-06 11:57:20 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 12:16:35 +00:00
|
|
|
TEST_CASE(format_character)
|
|
|
|
{
|
|
|
|
char a = 'a';
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{}", true ? a : 'b'), "a");
|
2020-10-06 12:16:35 +00:00
|
|
|
}
|
|
|
|
|
2020-10-07 12:01:59 +00:00
|
|
|
struct A {
|
|
|
|
};
|
|
|
|
struct B {
|
|
|
|
};
|
|
|
|
template<>
|
|
|
|
struct AK::Formatter<B> : Formatter<StringView> {
|
2021-11-16 00:15:21 +00:00
|
|
|
ErrorOr<void> format(FormatBuilder& builder, B)
|
2020-10-07 12:01:59 +00:00
|
|
|
{
|
2022-07-11 17:32:29 +00:00
|
|
|
return Formatter<StringView>::format(builder, "B"sv);
|
2020-10-07 12:01:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_CASE(format_if_supported)
|
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{}", FormatIfSupported { A {} }), "?");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{}", FormatIfSupported { B {} }), "B");
|
2020-10-07 12:01:59 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 11:46:00 +00:00
|
|
|
TEST_CASE(file_descriptor)
|
|
|
|
{
|
|
|
|
char filename[] = "/tmp/test-file-descriptor-XXXXXX";
|
|
|
|
|
|
|
|
int fd = mkstemp(filename);
|
|
|
|
FILE* file = fdopen(fd, "w+");
|
|
|
|
|
|
|
|
outln(file, "{}", "Hello, World!");
|
2020-11-09 09:47:19 +00:00
|
|
|
out(file, "foo");
|
2020-10-15 11:46:00 +00:00
|
|
|
outln(file, "bar");
|
|
|
|
|
|
|
|
rewind(file);
|
|
|
|
|
|
|
|
Array<u8, 256> buffer;
|
2022-04-01 17:58:27 +00:00
|
|
|
auto const nread = fread(buffer.data(), 1, buffer.size(), file);
|
2020-10-15 11:46:00 +00:00
|
|
|
|
2021-07-04 09:08:46 +00:00
|
|
|
EXPECT_EQ("Hello, World!\nfoobar\n"sv, StringView { buffer.span().trim(nread) });
|
2020-10-15 11:46:00 +00:00
|
|
|
|
|
|
|
fclose(file);
|
2022-01-13 23:20:01 +00:00
|
|
|
|
|
|
|
unlink(filename);
|
2020-10-15 11:46:00 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 10:34:44 +00:00
|
|
|
TEST_CASE(floating_point_numbers)
|
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{}", 1.12), "1.12");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{}", 1.), "1");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:.3}", 1.12), "1.12");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:.1}", 1.12), "1.1");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{}", -1.12), "-1.12");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:'.4}", 1234.5678), "1,234.5678");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:'.4}", -1234.5678), "-1,234.5678");
|
2020-11-09 10:34:44 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{:.30f}", 1.0), "1.000000000000000000000000000000");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:.30f}", 1.5), "1.500000000000000000000000000000");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:.30f}", -2.0), "-2.000000000000000000000000000000");
|
2023-10-15 14:58:30 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{:.0f}", 1.4), "1");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:.0f}", 1.5), "2");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:.0f}", -1.9), "-2");
|
2023-10-15 14:58:30 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{:.1f}", 1.4), "1.4");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:.1f}", 1.99), "2.0");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:.1f}", 9.999), "10.0");
|
2023-10-15 14:58:30 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{}", NAN), "nan");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{}", INFINITY), "inf");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{}", -INFINITY), "-inf");
|
2021-10-30 08:43:21 +00:00
|
|
|
|
2020-11-09 10:34:44 +00:00
|
|
|
// 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:
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{:x>5.1}", 1.12), "xx1.1");
|
2020-11-09 10:34:44 +00:00
|
|
|
}
|
|
|
|
|
2023-10-30 20:01:47 +00:00
|
|
|
TEST_CASE(floating_point_default_precision)
|
|
|
|
{
|
|
|
|
#define EXPECT_FORMAT(lit, value) \
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{}", lit), value##sv)
|
2023-10-30 20:01:47 +00:00
|
|
|
|
|
|
|
EXPECT_FORMAT(10.3f, "10.3");
|
|
|
|
EXPECT_FORMAT(123e4f, "1230000");
|
|
|
|
EXPECT_FORMAT(1.23e4f, "12300");
|
|
|
|
EXPECT_FORMAT(134232544.4365f, "134232540");
|
|
|
|
EXPECT_FORMAT(1.43e24, "1.43e+24");
|
|
|
|
EXPECT_FORMAT(1.43e-24, "1.43e-24");
|
|
|
|
EXPECT_FORMAT(0.0f, "0");
|
|
|
|
EXPECT_FORMAT(3.14159265358979, "3.14159265358979");
|
|
|
|
EXPECT_FORMAT(1.61803399, "1.61803399");
|
|
|
|
EXPECT_FORMAT(2.71827, "2.71827");
|
|
|
|
EXPECT_FORMAT(42.f, "42");
|
|
|
|
EXPECT_FORMAT(123456.78, "123456.78");
|
|
|
|
EXPECT_FORMAT(23456.78910, "23456.7891");
|
|
|
|
|
|
|
|
#undef EXPECT_FORMAT
|
|
|
|
}
|
|
|
|
|
2020-11-09 10:34:44 +00:00
|
|
|
TEST_CASE(no_precision_no_trailing_number)
|
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{:.0}", 0.1), "0");
|
2020-11-09 10:34:44 +00:00
|
|
|
}
|
|
|
|
|
2021-06-19 14:00:31 +00:00
|
|
|
TEST_CASE(precision_with_trailing_zeros)
|
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{:0.3}", 1.12), "1.120");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:0.1}", 1.12), "1.1");
|
2021-06-19 14:00:31 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 20:28:02 +00:00
|
|
|
TEST_CASE(magnitude_less_than_zero)
|
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{}", -0.654), "-0.654");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{}", 0.654), "0.654");
|
2021-04-15 20:28:02 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 22:34:00 +00:00
|
|
|
TEST_CASE(format_nullptr)
|
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{}", nullptr), ByteString::formatted("{:p}", static_cast<FlatPtr>(0)));
|
2021-01-01 22:34:00 +00:00
|
|
|
}
|
|
|
|
|
2021-01-09 00:00:22 +00:00
|
|
|
struct C {
|
|
|
|
int i;
|
|
|
|
};
|
|
|
|
template<>
|
|
|
|
struct AK::Formatter<C> : AK::Formatter<FormatString> {
|
2021-11-16 00:15:21 +00:00
|
|
|
ErrorOr<void> format(FormatBuilder& builder, C c)
|
2021-01-09 00:00:22 +00:00
|
|
|
{
|
2022-07-11 17:32:29 +00:00
|
|
|
return AK::Formatter<FormatString>::format(builder, "C(i={})"sv, c.i);
|
2021-01-09 00:00:22 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_CASE(use_format_string_formatter)
|
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{:*<10}", C { 42 }), "C(i=42)***");
|
2021-01-09 00:00:22 +00:00
|
|
|
}
|
|
|
|
|
2021-04-08 16:50:25 +00:00
|
|
|
TEST_CASE(long_long_regression)
|
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{}", 0x0123456789abcdefLL), "81985529216486895");
|
2021-04-08 16:50:25 +00:00
|
|
|
|
|
|
|
StringBuilder builder;
|
|
|
|
AK::FormatBuilder fmtbuilder { builder };
|
2021-11-16 00:15:21 +00:00
|
|
|
MUST(fmtbuilder.put_i64(0x0123456789abcdefLL));
|
2021-04-08 16:50:25 +00:00
|
|
|
|
|
|
|
EXPECT_EQ(builder.string_view(), "81985529216486895");
|
|
|
|
}
|
2021-06-17 08:48:36 +00:00
|
|
|
|
|
|
|
TEST_CASE(hex_dump)
|
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{:hex-dump}", "0000"), "30303030");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:>4hex-dump}", "0000"), "30303030 0000");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:>2hex-dump}", "0000"), "3030 00\n3030 00");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:*>4hex-dump}", "0000"), "30303030****0000");
|
2021-06-17 08:48:36 +00:00
|
|
|
}
|
2021-07-17 22:27:12 +00:00
|
|
|
|
2022-12-08 13:08:08 +00:00
|
|
|
TEST_CASE(span_format)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Vector<int> v { 1, 2, 3, 4 };
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{}", v.span()), "[ 1, 2, 3, 4 ]");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{}", const_cast<AddConst<decltype(v)>&>(v).span()), "[ 1, 2, 3, 4 ]");
|
2022-12-08 13:08:08 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
Vector<StringView> v { "1"sv, "2"sv, "3"sv, "4"sv };
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{}", v.span()), "[ 1, 2, 3, 4 ]");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{}", const_cast<AddConst<decltype(v)>&>(v).span()), "[ 1, 2, 3, 4 ]");
|
2022-12-08 13:08:08 +00:00
|
|
|
}
|
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
Vector<Vector<ByteString>> v { { "1"sv, "2"sv }, { "3"sv, "4"sv } };
|
|
|
|
EXPECT_EQ(ByteString::formatted("{}", v.span()), "[ [ 1, 2 ], [ 3, 4 ] ]");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{}", const_cast<AddConst<decltype(v)>&>(v).span()), "[ [ 1, 2 ], [ 3, 4 ] ]");
|
2022-12-08 13:08:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-17 22:27:12 +00:00
|
|
|
TEST_CASE(vector_format)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Vector<int> v { 1, 2, 3, 4 };
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{}", v), "[ 1, 2, 3, 4 ]");
|
2021-07-17 22:27:12 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
Vector<StringView> v { "1"sv, "2"sv, "3"sv, "4"sv };
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{}", v), "[ 1, 2, 3, 4 ]");
|
2021-07-17 22:27:12 +00:00
|
|
|
}
|
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
Vector<Vector<ByteString>> v { { "1"sv, "2"sv }, { "3"sv, "4"sv } };
|
|
|
|
EXPECT_EQ(ByteString::formatted("{}", v), "[ [ 1, 2 ], [ 3, 4 ] ]");
|
2021-07-17 22:27:12 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-21 21:07:03 +00:00
|
|
|
|
|
|
|
TEST_CASE(format_wchar)
|
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{}", L'a'), "a");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{}", L'\U0001F41E'), "\xF0\x9F\x90\x9E");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:x}", L'a'), "61");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:x}", L'\U0001F41E'), "1f41e");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:d}", L'a'), "97");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:d}", L'\U0001F41E'), "128030");
|
2021-09-21 21:07:03 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
EXPECT_EQ(ByteString::formatted("{:6}", L'a'), "a ");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:6d}", L'a'), " 97");
|
|
|
|
EXPECT_EQ(ByteString::formatted("{:#x}", L'\U0001F41E'), "0x1f41e");
|
2021-09-21 21:07:03 +00:00
|
|
|
}
|