From f893ad1c589e68932637c2e16434e76de90106b4 Mon Sep 17 00:00:00 2001 From: yut23 Date: Sat, 4 Jan 2025 19:25:37 -0500 Subject: [PATCH] aoc_lib: rework pretty_print::repr Instead of passing an arbitrary bool for results formatting, use a struct with named flags. Currently supports `.hex_float` (what the previous `result` parameter controlled) and `.char_as_number` (like `aoc::as_number`, but also applies to nested values). --- aoc_lib/src/ds/grid.hpp | 4 +- aoc_lib/src/lib.hpp | 5 +- aoc_lib/src/test_pretty_print.cpp | 22 +++++--- aoc_lib/src/unit_test/pretty_print.hpp | 75 ++++++++++++++++---------- aoc_lib/src/unit_test/unit_test.hpp | 11 ++-- 5 files changed, 73 insertions(+), 44 deletions(-) diff --git a/aoc_lib/src/ds/grid.hpp b/aoc_lib/src/ds/grid.hpp index a417f91..a4a3e9d 100644 --- a/aoc_lib/src/ds/grid.hpp +++ b/aoc_lib/src/ds/grid.hpp @@ -438,7 +438,7 @@ Grid(std::vector> &&) -> Grid; template std::ostream &print_repr(std::ostream &os, const aoc::ds::Grid &grid, - const bool result) { + const pretty_print::repr_state state) { // Get the current field width (from std::setw()) which we will use for the // individual values. Also set it to 0, as it shouldn't apply to the // initial '['. @@ -449,7 +449,7 @@ std::ostream &print_repr(std::ostream &os, const aoc::ds::Grid &grid, if (j != 0) { os << " "; } - os << std::setw(field_width) << pretty_print::repr(value, result); + os << std::setw(field_width) << pretty_print::repr(value, state); ++j; } ++i; diff --git a/aoc_lib/src/lib.hpp b/aoc_lib/src/lib.hpp index 139ab06..2b8a076 100644 --- a/aoc_lib/src/lib.hpp +++ b/aoc_lib/src/lib.hpp @@ -427,9 +427,8 @@ class as_number { util::concepts::same_as_any, char, signed char, unsigned char>; if constexpr (is_char) { - using int_type = - std::conditional_t, short, unsigned short>; - os << static_cast(h.dest); + // all signed or unsigned 8-bit values will fit in a short + os << static_cast(h.dest); } else { os << h.dest; } diff --git a/aoc_lib/src/test_pretty_print.cpp b/aoc_lib/src/test_pretty_print.cpp index c4de24f..cb2980a 100644 --- a/aoc_lib/src/test_pretty_print.cpp +++ b/aoc_lib/src/test_pretty_print.cpp @@ -22,14 +22,14 @@ std::size_t test_repr() { unit_test::TestSuite suite("pretty_print::repr"); const auto test = [&suite](auto &&value, const std::string &expected, - bool result = false, + const pretty_print::repr_state state = {}, const std::source_location loc = std::source_location::current()) { const std::string type_name = util::demangle(typeid(std::remove_cvref_t).name()); - suite.test(type_name, [&value, &expected, &result, &loc]() { + suite.test(type_name, [&value, &expected, &state, &loc]() { std::ostringstream oss{}; - oss << pretty_print::repr(value, result); + oss << pretty_print::repr(value, state); unit_test::checks::check_equal(oss.str(), expected, "", loc); }); }; @@ -37,6 +37,7 @@ std::size_t test_repr() { using namespace std::string_literals; // templated containers test(std::vector{1, 2, 3}, "{1, 2, 3}"); + test(std::initializer_list{-4, 15, 6}, "{-4, 15, 6}"); test(std::list>{{1, 2, 3}, {2, 3, 4}, {3, 4, 5}}, "{{1, 2, 3}, {2, 3, 4}, {3, 4, 5}}"); test(std::array{1.5, 2.4, 3.2, -2.8}, "{1.5, 2.4, 3.2, -2.8}"); @@ -58,15 +59,24 @@ std::size_t test_repr() { test(std::strong_ordering::equal, "equal"); test(std::strong_ordering::equivalent, "equal"); + // bool test(true, "true"); test(false, "false"); + // floating point test(1.4f, "1.4"); test(3.14, "3.14"); - // includes hex format for results - test(1.4f, "1.4 (0x1.666666p+0)", true); - test(3.14, "3.14 (0x1.91eb851eb851fp+1)", true); + // include hex value as well + test(1.4f, "1.4 (0x1.666666p+0)", {.hex_float = true}); + test(3.14, "3.14 (0x1.91eb851eb851fp+1)", {.hex_float = true}); + // chars + test('a', R"('a')"); + test('\x10', R"('\x10')"); + test('\x10', R"(16)", {.char_as_number = true}); + test(std::initializer_list{-2, 0, 15, -42, 127, -128}, + "{-2, 0, 15, -42, 127, -128}", {.char_as_number = true}); + test(std::vector{254, 0, 15, 214, 127, 128}, "{254, 0, 15, 214, 127, 128}", {.char_as_number = true}); return suite.done(), suite.num_failed(); } diff --git a/aoc_lib/src/unit_test/pretty_print.hpp b/aoc_lib/src/unit_test/pretty_print.hpp index ff44a21..839812e 100644 --- a/aoc_lib/src/unit_test/pretty_print.hpp +++ b/aoc_lib/src/unit_test/pretty_print.hpp @@ -27,6 +27,11 @@ namespace pretty_print { template class repr; +struct repr_state { + bool hex_float : 1 = false; + bool char_as_number : 1 = false; +}; + template struct has_custom_print_repr : std::false_type {}; @@ -58,13 +63,13 @@ template