| 1 | // { dg-do run { target c++23 } } |
| 2 | |
| 3 | #include <deque> |
| 4 | #include <flat_map> |
| 5 | #include <flat_set> |
| 6 | #include <format> |
| 7 | #include <list> |
| 8 | #include <map> |
| 9 | #include <set> |
| 10 | #include <testsuite_hooks.h> |
| 11 | #include <unordered_map> |
| 12 | #include <unordered_set> |
| 13 | #include <vector> |
| 14 | |
| 15 | static_assert( std::format_kind<std::vector<int>> == std::range_format::sequence ); |
| 16 | static_assert( std::format_kind<std::deque<int>> == std::range_format::sequence ); |
| 17 | static_assert( std::format_kind<std::list<int>> == std::range_format::sequence ); |
| 18 | |
| 19 | static_assert( std::format_kind<std::set<int>> == std::range_format::set ); |
| 20 | static_assert( std::format_kind<std::multiset<int>> == std::range_format::set ); |
| 21 | static_assert( std::format_kind<std::unordered_set<int>> == std::range_format::set ); |
| 22 | static_assert( std::format_kind<std::unordered_multiset<int>> == std::range_format::set ); |
| 23 | static_assert( std::format_kind<std::flat_set<int>> == std::range_format::set ); |
| 24 | static_assert( std::format_kind<std::flat_multiset<int>> == std::range_format::set ); |
| 25 | |
| 26 | static_assert( std::format_kind<std::map<int, int>> == std::range_format::map ); |
| 27 | static_assert( std::format_kind<std::multimap<int, int>> == std::range_format::map ); |
| 28 | static_assert( std::format_kind<std::unordered_map<int, int>> == std::range_format::map ); |
| 29 | static_assert( std::format_kind<std::unordered_multimap<int, int>> == std::range_format::map ); |
| 30 | static_assert( std::format_kind<std::flat_map<int, int>> == std::range_format::map ); |
| 31 | static_assert( std::format_kind<std::flat_multimap<int, int>> == std::range_format::map ); |
| 32 | |
| 33 | template<typename T> |
| 34 | struct MyVec : std::vector<T> |
| 35 | {}; |
| 36 | |
| 37 | static_assert( std::format_kind<MyVec<int>> == std::range_format::sequence ); |
| 38 | |
| 39 | template<typename T> |
| 40 | struct MySet : std::vector<T> |
| 41 | { |
| 42 | using key_type = T; |
| 43 | }; |
| 44 | |
| 45 | static_assert( std::format_kind<MySet<int>> == std::range_format::set ); |
| 46 | |
| 47 | template<typename T> |
| 48 | struct MyMap : std::vector<T> |
| 49 | { |
| 50 | using key_type = T; |
| 51 | using mapped_type = int; |
| 52 | }; |
| 53 | |
| 54 | static_assert( std::format_kind<MyMap<std::pair<int, int>>> == std::range_format::map ); |
| 55 | static_assert( std::format_kind<MyMap<std::tuple<int, int>>> == std::range_format::map ); |
| 56 | static_assert( std::format_kind<MyMap<int>> == std::range_format::set ); |
| 57 | |
| 58 | template<typename T, std::range_format rf> |
| 59 | struct CustFormat : std::vector<T> |
| 60 | { |
| 61 | using std::vector<T>::vector; |
| 62 | }; |
| 63 | |
| 64 | template<typename T, std::range_format rf> |
| 65 | constexpr auto std::format_kind<CustFormat<T, rf>> = rf; |
| 66 | |
| 67 | void test_override() |
| 68 | { |
| 69 | CustFormat<int, std::range_format::disabled> disabledf; |
| 70 | static_assert( !std::formattable<decltype(disabledf), char> ); |
| 71 | |
| 72 | CustFormat<int, std::range_format::sequence> seqf{1, 2, 3}; |
| 73 | VERIFY( std::format("{}", seqf) == "[1, 2, 3]" ); |
| 74 | |
| 75 | CustFormat<int, std::range_format::set> setf{1, 2, 3}; |
| 76 | VERIFY( std::format("{}", setf) == "{1, 2, 3}" ); |
| 77 | |
| 78 | // TODO test map once formatter for pair is implenented |
| 79 | |
| 80 | CustFormat<char, std::range_format::string> stringf{'a', 'b', 'c', 'd'}; |
| 81 | VERIFY( std::format("{}", stringf) == "abcd" ); |
| 82 | // Support precision as string do |
| 83 | VERIFY( std::format("{:.2}", stringf) == "ab" ); |
| 84 | |
| 85 | CustFormat<char, std::range_format::debug_string> debugf{'a', 'b', 'c', 'd'}; |
| 86 | VERIFY( std::format("{}", debugf) == R"("abcd")" ); |
| 87 | // Support precision as string do |
| 88 | VERIFY( std::format("{:.3}", debugf) == R"("ab)" ); |
| 89 | } |
| 90 | |
| 91 | int main() |
| 92 | { |
| 93 | test_override(); |
| 94 | } |