Skip to content

Commit

Permalink
fix standard containers printing in tests
Browse files Browse the repository at this point in the history
Using overloaded set of `operator<<()` for different containers.

(cherry picked from commit c952a65)
  • Loading branch information
pmed committed Mar 14, 2021
1 parent 2b03741 commit 352c061
Showing 1 changed file with 116 additions and 49 deletions.
165 changes: 116 additions & 49 deletions test/test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,71 +7,123 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <iosfwd>
#include <array>

#include <string>
#include <sstream>
#include <stdexcept>
#include <tuple>

// containers
#include <array>
#include <vector>
#include <deque>
#include <list>
#include <forward_list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <utility>

#include "v8pp/context.hpp"
#include "v8pp/convert.hpp"
#include "v8pp/utility.hpp"

template<typename Char, typename Traits,
typename T, typename Alloc, typename ...Other,
template<typename, typename, typename ...> class Sequence>
std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os,
Sequence<T, Alloc, Other...> const& sequence)
template<typename Sequence>
std::ostream& print_sequence(std::ostream& os, Sequence const& sequence, char const brackets[2]);

template<typename T, std::size_t N>
std::ostream& operator<<(std::ostream& os, std::array<T, N> const& array)
{
os << '[';
bool first = true;
for (auto const& item : sequence)
{
if (!first) os << ", ";
os << item;
first = false;
}
os << ']';
return os;
return print_sequence(os, array, "[]");
}

template<typename Char, typename Traits, typename T, size_t N>
std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os,
std::array<T, N> const& array)
template<typename Char, typename Traits, typename Alloc, typename = typename std::enable_if<!std::is_same<Char, char>::value>::type>
std::ostream& operator<<(std::ostream& os, std::basic_string<Char, Traits, Alloc> const& string)
{
os << '[';
bool first = true;
for (auto const& item : array)
{
if (!first) os << ", ";
os << item;
first = false;
}
os << ']';
return os;
return print_sequence(os, string, "''");
}

template<typename Char, typename Traits,
typename Key, typename Value, typename Less, typename Alloc, typename ...Other,
template<typename, typename, typename, typename, typename ...> class Mapping>
std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os,
Mapping<Key, Value, Less, Alloc, Other...> const& mapping)
template<typename T, typename Alloc>
std::ostream& operator<<(std::ostream& os, std::vector<T, Alloc> const& vector)
{
os << '{';
bool first = true;
for (auto const& item : mapping)
{
if (!first) os << ", ";
os << item.first << ": " <<item.second;
first = false;
}
os << '}';
return os;
return print_sequence(os, vector, "[]");
}

template<typename T, typename Alloc>
std::ostream& operator<<(std::ostream& os, std::list<T, Alloc> const& list)
{
return print_sequence(os, list, "[]");
}

template<typename T, typename Alloc>
std::ostream& operator<<(std::ostream& os, std::forward_list<T, Alloc> const& fwd_list)
{
return print_sequence(os, fwd_list, "[]");
}

template<typename T, typename Alloc>
std::ostream& operator<<(std::ostream& os, std::deque<T, Alloc> const& deque)
{
return print_sequence(os, deque, "[]");
}

template<typename Char, typename Traits,
typename Enum, typename = typename std::enable_if<std::is_enum<Enum>::value>::type>
std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os, Enum value)
template<typename Key, typename Comp, typename Alloc>
std::ostream& operator<<(std::ostream& os, std::set<Key, Comp, Alloc> const& set)
{
return print_sequence(os, set, "[]");
}

template<typename Key, typename Comp, typename Alloc>
std::ostream& operator<<(std::ostream& os, std::multiset<Key, Comp, Alloc> const& multiset)
{
return print_sequence(os, multiset, "[]");
}

template<typename Key, typename Value, typename Comp, typename Alloc>
std::ostream& operator<<(std::ostream& os, std::map<Key, Value, Comp, Alloc> const& map)
{
return print_sequence(os, map, "{}");
}

template<typename Key, typename Value, typename Comp, typename Alloc>
std::ostream& operator<<(std::ostream& os, std::multimap<Key, Value, Comp, Alloc> const& multimap)
{
return print_sequence(os, multimap, "{}");
}

template<typename Key, typename Hash, typename Eq, typename Alloc>
std::ostream& operator<<(std::ostream& os, std::unordered_set<Key, Hash, Eq, Alloc> const& unordered_set)
{
return print_sequence(os, unordered_set, "[]");
}

template<typename Key, typename Hash, typename Eq, typename Alloc>
std::ostream& operator<<(std::ostream& os, std::unordered_multiset<Key, Hash, Eq, Alloc> const& unordered_multiset)
{
return print_sequence(os, unordered_multiset, "[]");
}

template<typename Key, typename T, typename Hash, typename Eq, typename Alloc>
std::ostream& operator<<(std::ostream& os, std::unordered_map<Key, T, Hash, Eq, Alloc> const& unordered_map)
{
return print_sequence(os, unordered_map, "{}");
}

template<typename Key, typename T, typename Hash, typename Eq, typename Alloc>
std::ostream& operator<<(std::ostream& os, std::unordered_multimap<Key, T, Hash, Eq, Alloc> const& unordered_multimap)
{
return print_sequence(os, unordered_multimap, "{}");
}

template<typename First, typename Second>
std::ostream& operator<<(std::ostream& os, std::pair<First, Second> const& pair)
{
return os << pair.first << ": " << pair.second;
}

template<typename Enum, typename = typename std::enable_if<std::is_enum<Enum>::value>::type>
std::ostream& operator<<(std::ostream& os, Enum value)
{
return os << static_cast<typename std::underlying_type<Enum>::type>(value);
}
Expand All @@ -87,9 +139,24 @@ template<typename Char, typename Traits, typename... Ts>
std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os,
std::tuple<Ts...> const& tuple)
{
os << '{';
os << '(';
print_tuple(os, tuple, std::index_sequence_for<Ts...>{});
os << '}';
os << ')';
return os;
}

template<typename Sequence>
std::ostream& print_sequence(std::ostream& os, Sequence const& sequence, char const brackets[2])
{
os << brackets[0];
bool first = true;
for (auto const& item : sequence)
{
if (!first) os << ", ";
os << item;
first = false;
}
os << brackets[1];
return os;
}

Expand Down

0 comments on commit 352c061

Please sign in to comment.