Skip to content

Commit 14bc459

Browse files
committed
fix standard containers printing in tests
Using overloaded set of `operator<<()` for different containers. (cherry picked from commit c952a65)
1 parent 9079335 commit 14bc459

File tree

1 file changed

+116
-49
lines changed

1 file changed

+116
-49
lines changed

test/test.hpp

Lines changed: 116 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,71 +7,123 @@
77
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
88
//
99
#include <iosfwd>
10-
#include <array>
10+
1111
#include <string>
1212
#include <sstream>
1313
#include <stdexcept>
1414
#include <tuple>
1515

16+
// containers
17+
#include <array>
18+
#include <vector>
19+
#include <deque>
20+
#include <list>
21+
#include <forward_list>
22+
#include <set>
23+
#include <map>
24+
#include <unordered_set>
25+
#include <unordered_map>
26+
#include <utility>
27+
1628
#include "v8pp/context.hpp"
1729
#include "v8pp/convert.hpp"
1830
#include "v8pp/utility.hpp"
1931

20-
template<typename Char, typename Traits,
21-
typename T, typename Alloc, typename ...Other,
22-
template<typename, typename, typename ...> class Sequence>
23-
std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os,
24-
Sequence<T, Alloc, Other...> const& sequence)
32+
template<typename Sequence>
33+
std::ostream& print_sequence(std::ostream& os, Sequence const& sequence, char const brackets[2]);
34+
35+
template<typename T, std::size_t N>
36+
std::ostream& operator<<(std::ostream& os, std::array<T, N> const& array)
2537
{
26-
os << '[';
27-
bool first = true;
28-
for (auto const& item : sequence)
29-
{
30-
if (!first) os << ", ";
31-
os << item;
32-
first = false;
33-
}
34-
os << ']';
35-
return os;
38+
return print_sequence(os, array, "[]");
3639
}
3740

38-
template<typename Char, typename Traits, typename T, size_t N>
39-
std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os,
40-
std::array<T, N> const& array)
41+
template<typename Char, typename Traits, typename Alloc, typename = typename std::enable_if<!std::is_same<Char, char>::value>::type>
42+
std::ostream& operator<<(std::ostream& os, std::basic_string<Char, Traits, Alloc> const& string)
4143
{
42-
os << '[';
43-
bool first = true;
44-
for (auto const& item : array)
45-
{
46-
if (!first) os << ", ";
47-
os << item;
48-
first = false;
49-
}
50-
os << ']';
51-
return os;
44+
return print_sequence(os, string, "''");
5245
}
5346

54-
template<typename Char, typename Traits,
55-
typename Key, typename Value, typename Less, typename Alloc, typename ...Other,
56-
template<typename, typename, typename, typename, typename ...> class Mapping>
57-
std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os,
58-
Mapping<Key, Value, Less, Alloc, Other...> const& mapping)
47+
template<typename T, typename Alloc>
48+
std::ostream& operator<<(std::ostream& os, std::vector<T, Alloc> const& vector)
5949
{
60-
os << '{';
61-
bool first = true;
62-
for (auto const& item : mapping)
63-
{
64-
if (!first) os << ", ";
65-
os << item.first << ": " <<item.second;
66-
first = false;
67-
}
68-
os << '}';
69-
return os;
50+
return print_sequence(os, vector, "[]");
51+
}
52+
53+
template<typename T, typename Alloc>
54+
std::ostream& operator<<(std::ostream& os, std::list<T, Alloc> const& list)
55+
{
56+
return print_sequence(os, list, "[]");
57+
}
58+
59+
template<typename T, typename Alloc>
60+
std::ostream& operator<<(std::ostream& os, std::forward_list<T, Alloc> const& fwd_list)
61+
{
62+
return print_sequence(os, fwd_list, "[]");
63+
}
64+
65+
template<typename T, typename Alloc>
66+
std::ostream& operator<<(std::ostream& os, std::deque<T, Alloc> const& deque)
67+
{
68+
return print_sequence(os, deque, "[]");
7069
}
7170

72-
template<typename Char, typename Traits,
73-
typename Enum, typename = typename std::enable_if<std::is_enum<Enum>::value>::type>
74-
std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os, Enum value)
71+
template<typename Key, typename Comp, typename Alloc>
72+
std::ostream& operator<<(std::ostream& os, std::set<Key, Comp, Alloc> const& set)
73+
{
74+
return print_sequence(os, set, "[]");
75+
}
76+
77+
template<typename Key, typename Comp, typename Alloc>
78+
std::ostream& operator<<(std::ostream& os, std::multiset<Key, Comp, Alloc> const& multiset)
79+
{
80+
return print_sequence(os, multiset, "[]");
81+
}
82+
83+
template<typename Key, typename Value, typename Comp, typename Alloc>
84+
std::ostream& operator<<(std::ostream& os, std::map<Key, Value, Comp, Alloc> const& map)
85+
{
86+
return print_sequence(os, map, "{}");
87+
}
88+
89+
template<typename Key, typename Value, typename Comp, typename Alloc>
90+
std::ostream& operator<<(std::ostream& os, std::multimap<Key, Value, Comp, Alloc> const& multimap)
91+
{
92+
return print_sequence(os, multimap, "{}");
93+
}
94+
95+
template<typename Key, typename Hash, typename Eq, typename Alloc>
96+
std::ostream& operator<<(std::ostream& os, std::unordered_set<Key, Hash, Eq, Alloc> const& unordered_set)
97+
{
98+
return print_sequence(os, unordered_set, "[]");
99+
}
100+
101+
template<typename Key, typename Hash, typename Eq, typename Alloc>
102+
std::ostream& operator<<(std::ostream& os, std::unordered_multiset<Key, Hash, Eq, Alloc> const& unordered_multiset)
103+
{
104+
return print_sequence(os, unordered_multiset, "[]");
105+
}
106+
107+
template<typename Key, typename T, typename Hash, typename Eq, typename Alloc>
108+
std::ostream& operator<<(std::ostream& os, std::unordered_map<Key, T, Hash, Eq, Alloc> const& unordered_map)
109+
{
110+
return print_sequence(os, unordered_map, "{}");
111+
}
112+
113+
template<typename Key, typename T, typename Hash, typename Eq, typename Alloc>
114+
std::ostream& operator<<(std::ostream& os, std::unordered_multimap<Key, T, Hash, Eq, Alloc> const& unordered_multimap)
115+
{
116+
return print_sequence(os, unordered_multimap, "{}");
117+
}
118+
119+
template<typename First, typename Second>
120+
std::ostream& operator<<(std::ostream& os, std::pair<First, Second> const& pair)
121+
{
122+
return os << pair.first << ": " << pair.second;
123+
}
124+
125+
template<typename Enum, typename = typename std::enable_if<std::is_enum<Enum>::value>::type>
126+
std::ostream& operator<<(std::ostream& os, Enum value)
75127
{
76128
return os << static_cast<typename std::underlying_type<Enum>::type>(value);
77129
}
@@ -87,9 +139,24 @@ template<typename Char, typename Traits, typename... Ts>
87139
std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os,
88140
std::tuple<Ts...> const& tuple)
89141
{
90-
os << '{';
142+
os << '(';
91143
print_tuple(os, tuple, std::index_sequence_for<Ts...>{});
92-
os << '}';
144+
os << ')';
145+
return os;
146+
}
147+
148+
template<typename Sequence>
149+
std::ostream& print_sequence(std::ostream& os, Sequence const& sequence, char const brackets[2])
150+
{
151+
os << brackets[0];
152+
bool first = true;
153+
for (auto const& item : sequence)
154+
{
155+
if (!first) os << ", ";
156+
os << item;
157+
first = false;
158+
}
159+
os << brackets[1];
93160
return os;
94161
}
95162

0 commit comments

Comments
 (0)