From 2933676ea26d24ae0e2597b138335e3b50d6af57 Mon Sep 17 00:00:00 2001 From: Andreas Tsouchlos Date: Tue, 15 Feb 2022 11:34:49 +0100 Subject: [PATCH] Fixed bug in hex formatting, wrote tests for hex int formatting --- const_fmt/format_impl.h | 4 +-- examples/src/examples.cpp | 2 +- test/CMakeLists.txt | 1 + test/src/format_hex.cpp | 59 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 test/src/format_hex.cpp diff --git a/const_fmt/format_impl.h b/const_fmt/format_impl.h index 2bbc9be..8ca5143 100644 --- a/const_fmt/format_impl.h +++ b/const_fmt/format_impl.h @@ -101,7 +101,7 @@ constexpr inline const char* digits2_base(size_t value) { return &"000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F" "202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F" "404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F" - "606162636465666768696A6B6C6D6E6F707172737475767778796A6B6C6D6E6F" + "606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F" "808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9F" "A0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF" "C0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF" @@ -161,7 +161,7 @@ constexpr inline void format_base(char* out, uint_t value, int n_digits, } if (value < divisor) { - *--out = static_cast('0' + value); + *--out = digits2_base(value*divisor)[0]; return; } diff --git a/examples/src/examples.cpp b/examples/src/examples.cpp index 911f63c..e955122 100644 --- a/examples/src/examples.cpp +++ b/examples/src/examples.cpp @@ -5,7 +5,7 @@ using namespace const_fmt; int main() { - constexpr auto s = "This is an integer: {:08x}, and this is a float: {:09.4b}"_const_fmt(125u, -86.2); + constexpr auto s = "This is an integer: {:08x}, and this is a float: {:09.4b}"_const_fmt(122u, -86.2); // Convert s (with a type of 'std::array') into something // writable to std::cout diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index c2818fe..13950f3 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -28,4 +28,5 @@ package_add_test(parse_test src/parse.cpp) package_add_test(format_utility_test src/format_utility.cpp) package_add_test(format_decimal_test src/format_decimal.cpp) package_add_test(format_binary_test src/format_binary.cpp) +package_add_test(format_hex_test src/format_hex.cpp) diff --git a/test/src/format_hex.cpp b/test/src/format_hex.cpp new file mode 100644 index 0000000..6735fd5 --- /dev/null +++ b/test/src/format_hex.cpp @@ -0,0 +1,59 @@ +#include +#include + + +using namespace const_fmt; +using namespace const_fmt::const_fmt_detail; + + +TEST(FormatHex, positive_int) { + constexpr std::array control1 = {'0', '0', '0', '0', + '0', '0', '1', '0'}; + constexpr std::array formatted1 = const_format<"{:08x}">(0x10); + + constexpr std::array control2 = {' ', ' ', ' ', 'F', + 'F', 'A', '7', '6'}; + constexpr std::array formatted2 = const_format<"{:8x}">(0xffa76); + + constexpr std::array control3 = {'0', '0', '0', '0', + 'B', 'C', 'E', 'F'}; + constexpr std::array formatted3 = const_format<"{:08.4x}">(0xbcef); + + constexpr std::array control4 = {'A', 'D', '0', '1'}; + constexpr std::array formatted4 = const_format<"{:4x}">(0xad01); + + constexpr std::array control5 = {'f', 'f', 'f', 'f'}; + constexpr std::array formatted5 = const_format<"{:4x}">(0x12345); + + EXPECT_EQ(control1, formatted1); + EXPECT_EQ(control2, formatted2); + EXPECT_EQ(control3, formatted3); + EXPECT_EQ(control4, formatted4); + EXPECT_EQ(control5, formatted5); +} + +TEST(FormatHex, negative_int) { + constexpr std::array control1 = {'-', '0', '0', '0', + '0', '0', '1', '0'}; + constexpr std::array formatted1 = const_format<"{:08x}">(-0x10); + + constexpr std::array control2 = {' ', ' ', '-', 'F', + 'F', 'A', '7', '6'}; + constexpr std::array formatted2 = const_format<"{:8x}">(-0xffa76); + + constexpr std::array control3 = {'-', '0', '0', '0', + 'B', 'C', 'E', 'F'}; + constexpr std::array formatted3 = const_format<"{:08.4x}">(-0xbcef); + + constexpr std::array control4 = {'-', 'A', 'D', '1'}; + constexpr std::array formatted4 = const_format<"{:4x}">(-0xad1); + + constexpr std::array control5 = {'-', 'f', 'f', 'f'}; + constexpr std::array formatted5 = const_format<"{:04x}">(-0x1234); + + EXPECT_EQ(control1, formatted1); + EXPECT_EQ(control2, formatted2); + EXPECT_EQ(control3, formatted3); + EXPECT_EQ(control4, formatted4); + EXPECT_EQ(control5, formatted5); +}