diff --git a/include/etl/format.h b/include/etl/format.h index 95dafc8d..3268f43b 100644 --- a/include/etl/format.h +++ b/include/etl/format.h @@ -123,7 +123,13 @@ namespace etl template using format_string = basic_format_string...>; - // supported types to format + // Supported types to format + // + // This is the limited number of types as defined in std::basic_format_arg + // https://en.cppreference.com/w/cpp/utility/format/basic_format_arg.html + // + // Further types to be supported are added via converting constructors in + // etl::basic_format_arg using supported_format_types = etl::variant< etl::monostate, bool, @@ -239,6 +245,21 @@ namespace etl { } + basic_format_arg(const short v) + : data(static_cast(v)) + { + } + + basic_format_arg(const unsigned short v) + : data(static_cast(v)) + { + } + + basic_format_arg(const long int v) + : data(static_cast(v)) + { + } + basic_format_arg(const unsigned int v) : data(v) { @@ -254,6 +275,13 @@ namespace etl { } + // Additional type to list of basic types as defined for std::basic_format_arg: + // Mapping unsigned long to unsigned long long int + basic_format_arg(const unsigned long v) + : data(static_cast(v)) + { + } + basic_format_arg(const char* v) : data(v) { @@ -264,6 +292,16 @@ namespace etl { } + basic_format_arg(const signed char v) + : data(static_cast(v)) + { + } + + basic_format_arg(const unsigned char v) + : data(static_cast(v)) + { + } + basic_format_arg(const float v) : data(v) { diff --git a/test/test_format.cpp b/test/test_format.cpp index 2e148bb4..a6c25eab 100644 --- a/test/test_format.cpp +++ b/test/test_format.cpp @@ -86,6 +86,62 @@ namespace CHECK_EQUAL("-1", test_format(s, "{}", -1)); } + //************************************************************************* + TEST(test_format_short) + { + etl::string<100> s; + + CHECK_EQUAL("1", test_format(s, "{}", static_cast(1))); + CHECK_EQUAL("123", test_format(s, "{}", static_cast(123))); + CHECK_EQUAL("4123", test_format(s, "{}", static_cast(4123))); + CHECK_EQUAL("1 2", test_format(s, "{} {}", static_cast(1), static_cast(2))); + CHECK_EQUAL("-123", test_format(s, "{}", static_cast(-123))); + CHECK_EQUAL("0", test_format(s, "{}", static_cast(0))); + CHECK_EQUAL("-1", test_format(s, "{}", static_cast(-1))); + } + + //************************************************************************* + TEST(test_format_unsigned_short) + { + etl::string<100> s; + + CHECK_EQUAL("1", test_format(s, "{}", static_cast(1))); + CHECK_EQUAL("123", test_format(s, "{}", static_cast(123))); + CHECK_EQUAL("4123", test_format(s, "{}", static_cast(4123))); + CHECK_EQUAL("1 2", test_format(s, "{} {}", static_cast(1), static_cast(2))); + CHECK_EQUAL("60123", test_format(s, "{}", static_cast(60123))); + CHECK_EQUAL("0", test_format(s, "{}", static_cast(0))); + CHECK_EQUAL("65500", test_format(s, "{}", static_cast(65500))); + } + + //************************************************************************* + TEST(test_format_long_int) + { + etl::string<100> s; + + CHECK_EQUAL("1", test_format(s, "{}", static_cast(1))); + CHECK_EQUAL("123", test_format(s, "{}", static_cast(123))); + CHECK_EQUAL("4123", test_format(s, "{}", static_cast(4123))); + CHECK_EQUAL("1 2", test_format(s, "{} {}", static_cast(1), static_cast(2))); + CHECK_EQUAL("-123", test_format(s, "{}", static_cast(-123))); + CHECK_EQUAL("0", test_format(s, "{}", static_cast(0))); + CHECK_EQUAL("-1", test_format(s, "{}", static_cast(-1))); + } + + //************************************************************************* + TEST(test_format_unsigned_long_int) + { + etl::string<100> s; + + CHECK_EQUAL("1", test_format(s, "{}", static_cast(1))); + CHECK_EQUAL("123", test_format(s, "{}", static_cast(123))); + CHECK_EQUAL("4123", test_format(s, "{}", static_cast(4123))); + CHECK_EQUAL("1 2", test_format(s, "{} {}", static_cast(1), static_cast(2))); + CHECK_EQUAL("60123", test_format(s, "{}", static_cast(60123))); + CHECK_EQUAL("0", test_format(s, "{}", static_cast(0))); + CHECK_EQUAL("65500", test_format(s, "{}", static_cast(65500))); + } + //************************************************************************* TEST(test_format_unsigned_int) { @@ -131,8 +187,10 @@ namespace { etl::string<100> s; - CHECK_EQUAL("34", test_format(s, "{}", static_cast(34))); - CHECK_EQUAL("-14", test_format(s, "{}", static_cast(-14))); + // mapped to unsigned char + //CHECK_EQUAL("34", test_format(s, "{}", static_cast(34))); + // mapped to signed char + //CHECK_EQUAL("-14", test_format(s, "{}", static_cast(-14))); CHECK_EQUAL("6534", test_format(s, "{}", static_cast(6534))); CHECK_EQUAL("-9414", test_format(s, "{}", static_cast(-9414))); CHECK_EQUAL("236534", test_format(s, "{}", static_cast(236534))); @@ -398,6 +456,82 @@ namespace } } + //************************************************************************* + TEST(test_format_size_t) + { + etl::string<100> s; + + CHECK_EQUAL("0", test_format(s, "{}", static_cast(0LL))); + CHECK_EQUAL("1", test_format(s, "{}", static_cast(1LL))); + CHECK_EQUAL("12345678", test_format(s, "{}", static_cast(12345678LL))); + CHECK_EQUAL("4123456780", test_format(s, "{}", static_cast(4123456780LL))); +#if ETL_PLATFORM_64BIT + static_assert(sizeof(size_t) == 8, "size_t is expected to be 64 bit on 64 bit platforms"); + CHECK_EQUAL("18446744073709551615", test_format(s, "{}", static_cast(18446744073709551615ULL))); + CHECK_EQUAL("1311768467463790320", test_format(s, "{}", static_cast(0x123456789ABCDEF0ULL))); +#endif + } + + //************************************************************************* + TEST(test_format_unsigned_long) + { + etl::string<100> s; + + CHECK_EQUAL("0", test_format(s, "{}", static_cast(0LL))); + CHECK_EQUAL("1", test_format(s, "{}", static_cast(1LL))); + CHECK_EQUAL("12345678", test_format(s, "{}", static_cast(12345678LL))); + CHECK_EQUAL("4123456780", test_format(s, "{}", static_cast(4123456780LL))); +#if ETL_PLATFORM_64BIT + static_assert(sizeof(unsigned long) == 8, "size_t is expected to be 64 bit on 64 bit platforms"); + CHECK_EQUAL("18446744073709551615", test_format(s, "{}", static_cast(18446744073709551615ULL))); + CHECK_EQUAL("1311768467463790320", test_format(s, "{}", static_cast(0x123456789ABCDEF0ULL))); +#endif + } + + //************************************************************************* + TEST(test_format_signed_char) + { + etl::string<100> s; + + CHECK_EQUAL("a s b", test_format(s, "a {} b", static_cast('s'))); + CHECK_EQUAL("a s b", test_format(s, "a {:c} b", static_cast('s'))); + CHECK_EQUAL("a 's' b", test_format(s, "a {:?} b", static_cast('s'))); + CHECK_EQUAL("a \t b", test_format(s, "a {} b", static_cast('\t'))); + CHECK_EQUAL("a '\\t' b", test_format(s, "a {:?} b", static_cast('\t'))); + CHECK_EQUAL("a '\\n' b", test_format(s, "a {:?} b", static_cast('\n'))); + CHECK_EQUAL("a '\\r' b", test_format(s, "a {:?} b", static_cast('\r'))); + CHECK_EQUAL("a '\\\"' b", test_format(s, "a {:?} b", static_cast('"'))); + CHECK_EQUAL("a '\\'' b", test_format(s, "a {:?} b", static_cast('\''))); + CHECK_EQUAL("a '\\\\' b", test_format(s, "a {:?} b", static_cast('\\'))); + CHECK_EQUAL("a '\\\\' b", test_format(s, "a {:?} b", static_cast('\\'))); + CHECK_EQUAL("a 97 b", test_format(s, "a {:d} b", static_cast('a'))); + CHECK_EQUAL("a 61 b", test_format(s, "a {:X} b", static_cast('a'))); + CHECK_EQUAL("a 61 b", test_format(s, "a {:x} b", static_cast('a'))); + CHECK_EQUAL("a 0x61 b", test_format(s, "a {:#x} b", static_cast('a'))); + } + + //************************************************************************* + TEST(test_format_unsigned_char) + { + etl::string<100> s; + + CHECK_EQUAL("a s b", test_format(s, "a {} b", static_cast('s'))); + CHECK_EQUAL("a s b", test_format(s, "a {:c} b", static_cast('s'))); + CHECK_EQUAL("a 's' b", test_format(s, "a {:?} b", static_cast('s'))); + CHECK_EQUAL("a \t b", test_format(s, "a {} b", static_cast('\t'))); + CHECK_EQUAL("a '\\t' b", test_format(s, "a {:?} b", static_cast('\t'))); + CHECK_EQUAL("a '\\n' b", test_format(s, "a {:?} b", static_cast('\n'))); + CHECK_EQUAL("a '\\r' b", test_format(s, "a {:?} b", static_cast('\r'))); + CHECK_EQUAL("a '\\\"' b", test_format(s, "a {:?} b", static_cast('"'))); + CHECK_EQUAL("a '\\'' b", test_format(s, "a {:?} b", static_cast('\''))); + CHECK_EQUAL("a '\\\\' b", test_format(s, "a {:?} b", static_cast('\\'))); + CHECK_EQUAL("a '\\\\' b", test_format(s, "a {:?} b", static_cast('\\'))); + CHECK_EQUAL("a 97 b", test_format(s, "a {:d} b", static_cast('a'))); + CHECK_EQUAL("a 61 b", test_format(s, "a {:X} b", static_cast('a'))); + CHECK_EQUAL("a 61 b", test_format(s, "a {:x} b", static_cast('a'))); + CHECK_EQUAL("a 0x61 b", test_format(s, "a {:#x} b", static_cast('a'))); + } + //************************************************************************* TEST(test_format_limit) {