diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index e17b605..e53938b 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -25,5 +25,6 @@ endmacro() package_add_test(utility_test src/utility.cpp) package_add_test(parse_test src/parse.cpp) -package_add_test(format_test src/format.cpp) +package_add_test(format_decimal_test src/format_decimal.cpp) +package_add_test(format_binary_test src/format_binary.cpp) diff --git a/test/src/format.cpp b/test/src/format.cpp index 33e1c42..d60712a 100644 --- a/test/src/format.cpp +++ b/test/src/format.cpp @@ -1,4 +1,4 @@ -#include +#include #include @@ -6,7 +6,7 @@ using namespace const_fmt; using namespace const_fmt::const_fmt_detail; -TEST(Format, positive_int) { +TEST(FormatDecimal, positive_int) { constexpr std::array control1 = {'0', '0', '0', '0', '0', '0', '0', '2'}; constexpr std::array formatted1 = const_format<"{:08}">(2); @@ -32,7 +32,7 @@ TEST(Format, positive_int) { EXPECT_EQ(control5, formatted5); } -TEST(Format, negative_int) { +TEST(FormatDecimal, negative_int) { constexpr std::array control1 = {'-', '0', '0', '0', '0', '0', '0', '2'}; constexpr std::array formatted1 = const_format<"{:08}">(-2); @@ -58,7 +58,7 @@ TEST(Format, negative_int) { EXPECT_EQ(control5, formatted5); } -TEST(Format, positive_float) { +TEST(FormatDecimal, positive_float) { constexpr std::array control1 = {'0', '0', '2', '.', '3', '4', '5', '6'}; constexpr std::array formatted1 = const_format<"{:08.4}">(2.3456); @@ -82,7 +82,7 @@ TEST(Format, positive_float) { EXPECT_EQ(control4, formatted4); } -TEST(Format, negative_float) { +TEST(FormatDecimal, negative_float) { constexpr std::array control1 = {'-', '0', '2', '.', '3', '4', '5', '6'}; constexpr std::array formatted1 = const_format<"{:08.4}">(-2.3456); @@ -100,9 +100,4 @@ TEST(Format, negative_float) { EXPECT_EQ(control1, formatted1); EXPECT_EQ(control2, formatted2); EXPECT_EQ(control3, formatted3); -} -// -// TEST(Format, string) { -// // TODO -// EXPECT_EQ(true, false); -//} +} \ No newline at end of file diff --git a/test/src/format_binary.cpp b/test/src/format_binary.cpp new file mode 100644 index 0000000..c5873b2 --- /dev/null +++ b/test/src/format_binary.cpp @@ -0,0 +1,59 @@ +#include +#include + + +using namespace const_fmt; +using namespace const_fmt::const_fmt_detail; + + +TEST(FormatBinary, positive_int) { + constexpr std::array control1 = {'0', '0', '0', '0', + '0', '0', '1', '0'}; + constexpr std::array formatted1 = const_format<"{:08b}">(0b10); + + constexpr std::array control2 = {' ', ' ', '1', '0', + '1', '0', '1', '0'}; + constexpr std::array formatted2 = const_format<"{:8b}">(0b101010); + + constexpr std::array control3 = {'0', '0', '0', '1', + '1', '0', '0', '1'}; + constexpr std::array formatted3 = const_format<"{:08.4b}">(0b11001); + + constexpr std::array control4 = {'1', '0', '1', '1'}; + constexpr std::array formatted4 = const_format<"{:4b}">(0b1011); + + constexpr std::array control5 = {'f', 'f', 'f', 'f'}; + constexpr std::array formatted5 = const_format<"{:4b}">(0b10011); + + EXPECT_EQ(control1, formatted1); + EXPECT_EQ(control2, formatted2); + EXPECT_EQ(control3, formatted3); + EXPECT_EQ(control4, formatted4); + EXPECT_EQ(control5, formatted5); +} + +TEST(FormatBinary, negative_int) { + constexpr std::array control1 = {'-', '0', '0', '0', + '0', '0', '1', '0'}; + constexpr std::array formatted1 = const_format<"{:08b}">(-0b10); + + constexpr std::array control2 = {' ', '-', '1', '0', + '1', '0', '1', '0'}; + constexpr std::array formatted2 = const_format<"{:8b}">(-0b101010); + + constexpr std::array control3 = {'-', '0', '0', '1', + '0', '0', '1', '1'}; + constexpr std::array formatted3 = const_format<"{:08.4b}">(-0b10011); + + constexpr std::array control4 = {'-', '1', '1', '0', '1'}; + constexpr std::array formatted4 = const_format<"{:5b}">(-0b1101); + + constexpr std::array control5 = {'-', 'f', 'f', 'f', 'f'}; + constexpr std::array formatted5 = const_format<"{:05b}">(-0b10101); + + EXPECT_EQ(control1, formatted1); + EXPECT_EQ(control2, formatted2); + EXPECT_EQ(control3, formatted3); + EXPECT_EQ(control4, formatted4); + EXPECT_EQ(control5, formatted5); +} diff --git a/test/src/format_decimal.cpp b/test/src/format_decimal.cpp new file mode 100644 index 0000000..16bc490 --- /dev/null +++ b/test/src/format_decimal.cpp @@ -0,0 +1,103 @@ +#include +#include + + +using namespace const_fmt; +using namespace const_fmt::const_fmt_detail; + + +TEST(FormatDecimal, positive_int) { + constexpr std::array control1 = {'0', '0', '0', '0', + '0', '0', '0', '2'}; + constexpr std::array formatted1 = const_format<"{:08}">(2); + + constexpr std::array control2 = {' ', ' ', ' ', '2', + '2', '2', '2', '2'}; + constexpr std::array formatted2 = const_format<"{:8}">(22222); + + constexpr std::array control3 = {'0', '0', '0', '1', + '2', '3', '4', '5'}; + constexpr std::array formatted3 = const_format<"{:08.4}">(12345); + + constexpr std::array control4 = {'6', '7', '8', '9'}; + constexpr std::array formatted4 = const_format<"{:4}">(6789); + + constexpr std::array control5 = {'f', 'f', 'f', 'f'}; + constexpr std::array 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(FormatDecimal, negative_int) { + constexpr std::array control1 = {'-', '0', '0', '0', + '0', '0', '0', '2'}; + constexpr std::array formatted1 = const_format<"{:08}">(-2); + + constexpr std::array control2 = {' ', ' ', '-', '2', + '2', '2', '2', '2'}; + constexpr std::array formatted2 = const_format<"{:8}">(-22222); + + constexpr std::array control3 = {'-', '0', '0', '1', + '2', '3', '4', '5'}; + constexpr std::array formatted3 = const_format<"{:08.4}">(-12345); + + constexpr std::array control4 = {'-', '6', '7', '8', '9'}; + constexpr std::array formatted4 = const_format<"{:5}">(-6789); + + constexpr std::array control5 = {'-', 'f', 'f', 'f', 'f'}; + constexpr std::array 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(FormatDecimal, positive_float) { + constexpr std::array control1 = {'0', '0', '2', '.', + '3', '4', '5', '6'}; + constexpr std::array formatted1 = const_format<"{:08.4}">(2.3456); + + // Float error: 2323.2 -> 2323.1 + constexpr std::array control2 = {' ', ' ', '2', '3', + '2', '3', '.', '1'}; + constexpr std::array formatted2 = const_format<"{:8.1}">(2323.2); + + constexpr std::array control3 = {'1', '2', '3', '4', + '.', '5', '0', '0'}; + constexpr std::array formatted3 = const_format<"{:08.3}">(1234.5); + + // Float error: .9 -> .8 + constexpr std::array control4 = {'f', 'f', '.', '8'}; + constexpr std::array formatted4 = const_format<"{:4.1}">(678.9); + + EXPECT_EQ(control1, formatted1); + EXPECT_EQ(control2, formatted2); + EXPECT_EQ(control3, formatted3); + EXPECT_EQ(control4, formatted4); +} + +TEST(FormatDecimal, negative_float) { + constexpr std::array control1 = {'-', '0', '2', '.', + '3', '4', '5', '6'}; + constexpr std::array formatted1 = const_format<"{:08.4}">(-2.3456); + + // Float error: 2323.2 -> 2323.1 + constexpr std::array control2 = {' ', '-', '2', '3', + '2', '3', '.', '1'}; + constexpr std::array formatted2 = const_format<"{:8.1}">(-2323.2); + + constexpr std::array control3 = {'-', 'f', 'f', 'f', + '.', '5', '0', '0'}; + constexpr std::array formatted3 = const_format<"{:08.3}">(-1234.5); + + + EXPECT_EQ(control1, formatted1); + EXPECT_EQ(control2, formatted2); + EXPECT_EQ(control3, formatted3); +} \ No newline at end of file