From 57248fe1905b85916a73143ddee186eb86c3c2af Mon Sep 17 00:00:00 2001 From: Corentin Le Molgat Date: Thu, 15 Feb 2024 11:56:57 +0100 Subject: [PATCH] base: extends DUMP_VARS to support optional and vector --- ortools/algorithms/radix_sort_test.cc | 4 ++-- ortools/base/dump_vars.h | 28 +++++++++++++++++++++++++-- ortools/base/dump_vars_test.cc | 17 ++++++++++++++++ 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/ortools/algorithms/radix_sort_test.cc b/ortools/algorithms/radix_sort_test.cc index b4765548cf..6df67d07f3 100644 --- a/ortools/algorithms/radix_sort_test.cc +++ b/ortools/algorithms/radix_sort_test.cc @@ -146,9 +146,9 @@ TYPED_TEST_P(RadixSortTest, RandomizedCorrectnessTestAgainstStdSortSmallSizes) { std::vector expected_values = unsorted_values; absl::c_sort(expected_values); ASSERT_TRUE(sorted_values == expected_values) - /* << DUMP_VARS(test, use_main_radix_sort, radix_width, num_passes, size, + << DUMP_VARS(test, use_main_radix_sort, radix_width, num_passes, size, allow_negative, val_bits, max_abs_val, unsorted_values, - sorted_values, expected_values)*/; + sorted_values, expected_values); } } diff --git a/ortools/base/dump_vars.h b/ortools/base/dump_vars.h index e3e9bc16f1..277675b185 100644 --- a/ortools/base/dump_vars.h +++ b/ortools/base/dump_vars.h @@ -39,6 +39,7 @@ #ifndef OR_TOOLS_BASE_DUMP_VARS_H_ #define OR_TOOLS_BASE_DUMP_VARS_H_ +#include #include #include #include @@ -58,6 +59,9 @@ #define DUMP_FOR_EACH_N6(F, a, ...) F(a) DUMP_FOR_EACH_N5(F, __VA_ARGS__) #define DUMP_FOR_EACH_N7(F, a, ...) F(a) DUMP_FOR_EACH_N6(F, __VA_ARGS__) #define DUMP_FOR_EACH_N8(F, a, ...) F(a) DUMP_FOR_EACH_N7(F, __VA_ARGS__) +#define DUMP_FOR_EACH_N9(F, a, ...) F(a) DUMP_FOR_EACH_N8(F, __VA_ARGS__) +#define DUMP_FOR_EACH_N10(F, a, ...) F(a) DUMP_FOR_EACH_N9(F, __VA_ARGS__) +#define DUMP_FOR_EACH_N11(F, a, ...) F(a) DUMP_FOR_EACH_N10(F, __VA_ARGS__) #define DUMP_CONCATENATE(x, y) x##y #define DUMP_FOR_EACH_(N, F, ...) \ @@ -65,8 +69,8 @@ #define DUMP_NARG(...) DUMP_NARG_(__VA_OPT__(__VA_ARGS__, ) DUMP_RSEQ_N()) #define DUMP_NARG_(...) DUMP_ARG_N(__VA_ARGS__) -#define DUMP_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, N, ...) N -#define DUMP_RSEQ_N() 8, 7, 6, 5, 4, 3, 2, 1, 0 +#define DUMP_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, N, ...) N +#define DUMP_RSEQ_N() 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 #define DUMP_FOR_EACH(F, ...) \ DUMP_FOR_EACH_(DUMP_NARG(__VA_ARGS__), F __VA_OPT__(, __VA_ARGS__)) @@ -115,6 +119,26 @@ std::ostream& operator<<(std::ostream& os, return os; } +// needed by algorithms tests +template +std::ostream& operator<<(std::ostream& os, + const ::std::vector& vec) { + for (T it : vec) { + os << ::std::to_string(it) << ','; + } + return os; +} + +template +std::ostream& operator<<(std::ostream& os, + const ::std::optional& opt) { + if (opt.has_value()) + os << ::std::to_string(opt.value()); + else + os << "(none)"; + return os; +} + using DumpNames = ::std::vector<::std::string>; struct print_fields { diff --git a/ortools/base/dump_vars_test.cc b/ortools/base/dump_vars_test.cc index 2d4300a8bb..f9a9b42dcd 100644 --- a/ortools/base/dump_vars_test.cc +++ b/ortools/base/dump_vars_test.cc @@ -13,6 +13,7 @@ #include "ortools/base/dump_vars.h" +#include #include #include #include @@ -117,6 +118,22 @@ TEST(DumpVars, ManyArgs) { DUMP_VARS(a, b, c, d, e, f).str()); } +TEST(DumpVars, Vector) { + std::vector vec = {49.3, 3.14}; + EXPECT_EQ("vec = 49.299999,3.140000,", ToString(DUMP_VARS(vec))); + EXPECT_EQ("vec = 49.299999,3.140000,", DUMP_VARS(vec).str()); +} + +TEST(DumpVars, Optional) { + std::optional of = {}; + EXPECT_EQ("of = (none)", ToString(DUMP_VARS(of))); + EXPECT_EQ("of = (none)", DUMP_VARS(of).str()); + + of = 49.3f; + EXPECT_EQ("of = 49.299999", ToString(DUMP_VARS(of))); + EXPECT_EQ("of = 49.299999", DUMP_VARS(of).str()); +} + TEST(DumpVars, LazyEvaluation) { { int n = 0;