Added test cases for floats
This commit is contained in:
parent
9bf3e63058
commit
9c22b2a084
@ -148,7 +148,7 @@ template <std::signed_integral int_t, fmt_data_t t_fmt_node>
|
||||
constexpr inline void format_int(char* out, int_t value) {
|
||||
const auto [abs_value, negative] = get_abs_value(value);
|
||||
|
||||
const int n_digits = count_digits(abs_value);
|
||||
const std::size_t n_digits = count_digits(abs_value);
|
||||
|
||||
format_decimal(out + 1 * (negative), abs_value, n_digits,
|
||||
t_fmt_node.length - 1 * (negative));
|
||||
@ -156,6 +156,7 @@ constexpr inline void format_int(char* out, int_t value) {
|
||||
if constexpr (t_fmt_node.has_zero_padding) {
|
||||
if (negative) *(out) = '-';
|
||||
} else {
|
||||
if (n_digits < t_fmt_node.length)
|
||||
if (negative) *(out + t_fmt_node.length - n_digits - 1) = '-';
|
||||
}
|
||||
}
|
||||
@ -194,10 +195,12 @@ constexpr inline void format_float(char* out, float_t value) {
|
||||
constexpr std::size_t factor = const_pow(10, t_fmt_node.precision);
|
||||
const int fractional = static_cast<int>((value - integral) * factor);
|
||||
|
||||
const auto [fractional_abs, fractional_negative] = get_abs_value(fractional);
|
||||
const auto [fractional_abs, fractional_negative] =
|
||||
get_abs_value(fractional);
|
||||
|
||||
format_int<int, fmt_node_integral>(out, integral);
|
||||
format_int<uint16_t, fmt_node_fractional>(out + t_fmt_node.length - t_fmt_node.precision, fractional_abs);
|
||||
format_int<uint16_t, fmt_node_fractional>(
|
||||
out + t_fmt_node.length - t_fmt_node.precision, fractional_abs);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
using namespace const_fmt;
|
||||
|
||||
int main() {
|
||||
constexpr auto s = "abcdef {:9.3}"_const_fmt(-876.3);
|
||||
constexpr auto s = "abcdef {:09.2}"_const_fmt(-876.3);
|
||||
|
||||
// Convert s (with a type of 'std::array<char, N>') into something
|
||||
// writable to std::cout
|
||||
|
||||
@ -22,10 +22,14 @@ TEST(Format, positive_int) {
|
||||
constexpr std::array<char, 4> control4 = {'6', '7', '8', '9'};
|
||||
constexpr std::array<char, 4> formatted4 = const_format<"{:4}">(6789);
|
||||
|
||||
constexpr std::array<char, 4> control5 = {'f', 'f', 'f', 'f'};
|
||||
constexpr std::array<char, 4> formatted5 = const_format<"{:4}">(67895);
|
||||
|
||||
EXPECT_EQ(control1, formatted1);
|
||||
EXPECT_EQ(control2, formatted2);
|
||||
EXPECT_EQ(control3, formatted3);
|
||||
EXPECT_EQ(control4, formatted4);
|
||||
EXPECT_EQ(control5, formatted5);
|
||||
}
|
||||
|
||||
TEST(Format, negative_int) {
|
||||
@ -44,21 +48,59 @@ TEST(Format, negative_int) {
|
||||
constexpr std::array<char, 5> control4 = {'-', '6', '7', '8', '9'};
|
||||
constexpr std::array<char, 5> formatted4 = const_format<"{:5}">(-6789);
|
||||
|
||||
constexpr std::array<char, 5> control5 = {'-', 'f', 'f', 'f', 'f'};
|
||||
constexpr std::array<char, 5> formatted5 = const_format<"{:05}">(-66789);
|
||||
|
||||
EXPECT_EQ(control1, formatted1);
|
||||
EXPECT_EQ(control2, formatted2);
|
||||
EXPECT_EQ(control3, formatted3);
|
||||
EXPECT_EQ(control4, formatted4);
|
||||
EXPECT_EQ(control5, formatted5);
|
||||
}
|
||||
|
||||
TEST(Format, positive_float) {
|
||||
constexpr std::array<char, 8> control1 = {'0', '0', '2', '.',
|
||||
'3', '4', '5', '6'};
|
||||
constexpr std::array<char, 8> formatted1 = const_format<"{:08.4}">(2.3456);
|
||||
|
||||
// Float error: 2323.2 -> 2323.1
|
||||
constexpr std::array<char, 8> control2 = {' ', ' ', '2', '3',
|
||||
'2', '3', '.', '1'};
|
||||
constexpr std::array<char, 8> formatted2 = const_format<"{:8.1}">(2323.2);
|
||||
|
||||
constexpr std::array<char, 8> control3 = {'1', '2', '3', '4',
|
||||
'.', '5', '0', '0'};
|
||||
constexpr std::array<char, 8> formatted3 = const_format<"{:08.3}">(1234.5);
|
||||
|
||||
// Float error: .9 -> .8
|
||||
constexpr std::array<char, 4> control4 = {'f', 'f', '.', '8'};
|
||||
constexpr std::array<char, 4> formatted4 = const_format<"{:4.1}">(678.9);
|
||||
|
||||
EXPECT_EQ(control1, formatted1);
|
||||
EXPECT_EQ(control2, formatted2);
|
||||
EXPECT_EQ(control3, formatted3);
|
||||
EXPECT_EQ(control4, formatted4);
|
||||
}
|
||||
//
|
||||
//TEST(Format, positive_float) {
|
||||
// // TODO
|
||||
// EXPECT_EQ(true, false);
|
||||
//}
|
||||
//
|
||||
//TEST(Format, negative_float) {
|
||||
// // TODO
|
||||
// EXPECT_EQ(true, false);
|
||||
//}
|
||||
|
||||
TEST(Format, negative_float) {
|
||||
constexpr std::array<char, 8> control1 = {'-', '0', '2', '.',
|
||||
'3', '4', '5', '6'};
|
||||
constexpr std::array<char, 8> formatted1 = const_format<"{:08.4}">(-2.3456);
|
||||
|
||||
// Float error: 2323.2 -> 2323.1
|
||||
constexpr std::array<char, 8> control2 = {' ', '-', '2', '3',
|
||||
'2', '3', '.', '1'};
|
||||
constexpr std::array<char, 8> formatted2 = const_format<"{:8.1}">(-2323.2);
|
||||
|
||||
constexpr std::array<char, 8> control3 = {'-', 'f', 'f', 'f',
|
||||
'.', '5', '0', '0'};
|
||||
constexpr std::array<char, 8> formatted3 = const_format<"{:08.3}">(-1234.5);
|
||||
|
||||
|
||||
EXPECT_EQ(control1, formatted1);
|
||||
EXPECT_EQ(control2, formatted2);
|
||||
EXPECT_EQ(control3, formatted3);
|
||||
}
|
||||
//
|
||||
// TEST(Format, string) {
|
||||
// // TODO
|
||||
|
||||
Loading…
Reference in New Issue
Block a user