base: extends DUMP_VARS to support optional and vector

This commit is contained in:
Corentin Le Molgat
2024-02-15 11:56:57 +01:00
parent 17ca1184f6
commit 57248fe190
3 changed files with 45 additions and 4 deletions

View File

@@ -146,9 +146,9 @@ TYPED_TEST_P(RadixSortTest, RandomizedCorrectnessTestAgainstStdSortSmallSizes) {
std::vector<TypeParam> 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);
}
}

View File

@@ -39,6 +39,7 @@
#ifndef OR_TOOLS_BASE_DUMP_VARS_H_
#define OR_TOOLS_BASE_DUMP_VARS_H_
#include <optional>
#include <ostream>
#include <sstream>
#include <string>
@@ -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 <typename T>
std::ostream& operator<<(std::ostream& os,
const ::std::vector<T>& vec) {
for (T it : vec) {
os << ::std::to_string(it) << ',';
}
return os;
}
template <typename T>
std::ostream& operator<<(std::ostream& os,
const ::std::optional<T>& 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 {

View File

@@ -13,6 +13,7 @@
#include "ortools/base/dump_vars.h"
#include <optional>
#include <ostream>
#include <sstream>
#include <string>
@@ -117,6 +118,22 @@ TEST(DumpVars, ManyArgs) {
DUMP_VARS(a, b, c, d, e, f).str());
}
TEST(DumpVars, Vector) {
std::vector<float> 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<float> 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;