Skip to content

Commit

Permalink
[core] Add more debug string types (ray-project#47928)
Browse files Browse the repository at this point in the history
Followup on ray-project#47893, add more
"blessed container types" to debug string function.

Signed-off-by: dentiny <[email protected]>
Signed-off-by: ujjawal-khare <[email protected]>
  • Loading branch information
dentiny authored and ujjawal-khare committed Oct 15, 2024
1 parent 269b9ad commit 7260fdf
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
25 changes: 25 additions & 0 deletions src/ray/util/container_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <set>
#include <sstream>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <vector>

Expand Down Expand Up @@ -69,6 +70,13 @@ std::ostream &operator<<(std::ostream &os, DebugStringWrapper<T> wrapper) {
return os << wrapper.obj_;
}

// TODO(hjiang): Implement debug string for `std::variant`.
template <>
inline std::ostream &operator<<(std::ostream &os,
DebugStringWrapper<std::nullopt_t> wrapper) {
return os << "(nullopt)";
}

template <typename... Ts>
std::ostream &operator<<(std::ostream &os, DebugStringWrapper<std::pair<Ts...>> pair) {
return os << "(" << debug_string(pair.obj_.first) << ", "
Expand All @@ -95,6 +103,10 @@ std::ostream &operator<<(std::ostream &os, DebugStringWrapper<std::tuple<Ts...>>
return os;
}

template <typename T, std::size_t N>
std::ostream &operator<<(std::ostream &os, DebugStringWrapper<std::array<T, N>> c) {
return c.StringifyContainer(os);
}
template <typename... Ts>
std::ostream &operator<<(std::ostream &os, DebugStringWrapper<std::vector<Ts...>> c) {
return c.StringifyContainer(os);
Expand Down Expand Up @@ -122,6 +134,19 @@ std::ostream &operator<<(std::ostream &os,
DebugStringWrapper<absl::flat_hash_map<Ts...>> c) {
return c.StringifyContainer(os);
}
template <typename... Ts>
std::ostream &operator<<(std::ostream &os,
DebugStringWrapper<std::unordered_map<Ts...>> c) {
return c.StringifyContainer(os);
}

template <typename T>
std::ostream &operator<<(std::ostream &os, DebugStringWrapper<std::optional<T>> c) {
if (!c.obj_.has_value()) {
return os << debug_string(std::nullopt);
}
return os << debug_string(c.obj_.value());
}

template <typename C>
const typename C::mapped_type &map_find_or_die(const C &c,
Expand Down
21 changes: 19 additions & 2 deletions src/ray/util/tests/container_util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@

#include "ray/util/container_util.h"

#include <gtest/gtest.h>

#include <optional>
#include <sstream>
#include <string>
#include <tuple>

#include "gtest/gtest.h"

namespace ray {

template <typename T>
Expand All @@ -29,8 +31,18 @@ std::string debug_string_to_string(const T &t) {
}

TEST(ContainerUtilTest, TestDebugString) {
// Numerical values.
ASSERT_EQ(debug_string_to_string(static_cast<int>(2)), "2");

// String values.
ASSERT_EQ(debug_string_to_string(std::string_view{"hello"}), "hello");
ASSERT_EQ(debug_string_to_string(std::string{"hello"}), "hello");

// Non-associative containers.
ASSERT_EQ(debug_string_to_string(std::vector<int>{1, 2}), "[1, 2]");
ASSERT_EQ(debug_string_to_string(std::array<int, 3>{1, 2, 3}), "[1, 2, 3]");

// Associative containers.
ASSERT_EQ(debug_string_to_string(std::set<int>{1, 2}), "[1, 2]");
ASSERT_EQ(debug_string_to_string(std::unordered_set<int>{2}), "[2]");
ASSERT_EQ(debug_string_to_string(absl::flat_hash_set<int>{1}), "[1]");
Expand All @@ -54,6 +66,11 @@ TEST(ContainerUtilTest, TestDebugString) {
ASSERT_EQ(debug_string_to_string(std::pair<int, std::string>{3, "value"}),
"(3, value)");

// Optional.
ASSERT_EQ(debug_string_to_string(std::nullopt), "(nullopt)");
ASSERT_EQ(debug_string_to_string(std::optional<std::string>{}), "(nullopt)");
ASSERT_EQ(debug_string_to_string(std::optional<std::string>{"hello"}), "hello");

// Composable: tuples of pairs of maps and vectors.
ASSERT_EQ(debug_string_to_string(
std::tuple<std::pair<int, std::vector<int>>, std::map<int, int>>{
Expand Down

0 comments on commit 7260fdf

Please sign in to comment.