Fixed bug in hex formatting, wrote tests for hex int formatting
This commit is contained in:
parent
7115e09aca
commit
2933676ea2
@ -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<char>('0' + value);
|
||||
*--out = digits2_base<t_format_type>(value*divisor)[0];
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@ -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<char, N>') into something
|
||||
// writable to std::cout
|
||||
|
||||
@ -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)
|
||||
|
||||
|
||||
59
test/src/format_hex.cpp
Normal file
59
test/src/format_hex.cpp
Normal file
@ -0,0 +1,59 @@
|
||||
#include <const_fmt/format.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
|
||||
using namespace const_fmt;
|
||||
using namespace const_fmt::const_fmt_detail;
|
||||
|
||||
|
||||
TEST(FormatHex, positive_int) {
|
||||
constexpr std::array<char, 8> control1 = {'0', '0', '0', '0',
|
||||
'0', '0', '1', '0'};
|
||||
constexpr std::array<char, 8> formatted1 = const_format<"{:08x}">(0x10);
|
||||
|
||||
constexpr std::array<char, 8> control2 = {' ', ' ', ' ', 'F',
|
||||
'F', 'A', '7', '6'};
|
||||
constexpr std::array<char, 8> formatted2 = const_format<"{:8x}">(0xffa76);
|
||||
|
||||
constexpr std::array<char, 8> control3 = {'0', '0', '0', '0',
|
||||
'B', 'C', 'E', 'F'};
|
||||
constexpr std::array<char, 8> formatted3 = const_format<"{:08.4x}">(0xbcef);
|
||||
|
||||
constexpr std::array<char, 4> control4 = {'A', 'D', '0', '1'};
|
||||
constexpr std::array<char, 4> formatted4 = const_format<"{:4x}">(0xad01);
|
||||
|
||||
constexpr std::array<char, 4> control5 = {'f', 'f', 'f', 'f'};
|
||||
constexpr std::array<char, 4> 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<char, 8> control1 = {'-', '0', '0', '0',
|
||||
'0', '0', '1', '0'};
|
||||
constexpr std::array<char, 8> formatted1 = const_format<"{:08x}">(-0x10);
|
||||
|
||||
constexpr std::array<char, 8> control2 = {' ', ' ', '-', 'F',
|
||||
'F', 'A', '7', '6'};
|
||||
constexpr std::array<char, 8> formatted2 = const_format<"{:8x}">(-0xffa76);
|
||||
|
||||
constexpr std::array<char, 8> control3 = {'-', '0', '0', '0',
|
||||
'B', 'C', 'E', 'F'};
|
||||
constexpr std::array<char, 8> formatted3 = const_format<"{:08.4x}">(-0xbcef);
|
||||
|
||||
constexpr std::array<char, 4> control4 = {'-', 'A', 'D', '1'};
|
||||
constexpr std::array<char, 4> formatted4 = const_format<"{:4x}">(-0xad1);
|
||||
|
||||
constexpr std::array<char, 4> control5 = {'-', 'f', 'f', 'f'};
|
||||
constexpr std::array<char, 4> 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);
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user