106 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++1410// UNSUPPORTED: no-localization11 12// <filesystem>13 14// class path15 16// template <class charT, class traits>17// basic_ostream<charT, traits>&18// operator<<(basic_ostream<charT, traits>& os, const path& p);19//20// template <class charT, class traits>21// basic_istream<charT, traits>&22// operator>>(basic_istream<charT, traits>& is, path& p)23//24 25#include <filesystem>26#include <type_traits>27#include <sstream>28#include <cassert>29#include <iostream>30 31#include "count_new.h"32#include "make_string.h"33#include "test_iterators.h"34#include "test_macros.h"35namespace fs = std::filesystem;36 37MultiStringType InStr = MKSTR("abcdefg/\"hijklmnop\"/qrstuvwxyz/123456789");38MultiStringType OutStr = MKSTR("\"abcdefg/\\\"hijklmnop\\\"/qrstuvwxyz/123456789\"");39 40template <class CharT>41void doIOTest() {42 using namespace fs;43 using Ptr = const CharT*;44 using StrStream = std::basic_stringstream<CharT>;45 const Ptr E = OutStr;46 const path p((const char*)InStr);47 StrStream ss;48 { // test output49 auto& ret = (ss << p);50 assert(ss.str() == E);51 assert(&ret == &ss);52 }53 { // test input54 path p_in;55 auto& ret = ss >> p_in;56 assert(p_in.native() == (const path::value_type*)InStr);57 assert(&ret == &ss);58 }59}60 61namespace impl {62using namespace fs;63 64template <class Stream, class Tp, class = decltype(std::declval<Stream&>() << std::declval<Tp&>())>65std::true_type is_ostreamable_imp(int);66 67template <class Stream, class Tp>68std::false_type is_ostreamable_imp(long);69 70template <class Stream, class Tp, class = decltype(std::declval<Stream&>() >> std::declval<Tp&>())>71std::true_type is_istreamable_imp(int);72 73template <class Stream, class Tp>74std::false_type is_istreamable_imp(long);75 76 77} // namespace impl78 79template <class Stream, class Tp>80struct is_ostreamable : decltype(impl::is_ostreamable_imp<Stream, Tp>(0)) {};81template <class Stream, class Tp>82struct is_istreamable : decltype(impl::is_istreamable_imp<Stream, Tp>(0)) {};83 84void test_LWG2989() {85#ifndef TEST_HAS_NO_WIDE_CHARACTERS86 static_assert(!is_ostreamable<decltype(std::cout), std::wstring>::value, "");87 static_assert(!is_ostreamable<decltype(std::wcout), std::string>::value, "");88 static_assert(!is_istreamable<decltype(std::cin), std::wstring>::value, "");89 static_assert(!is_istreamable<decltype(std::wcin), std::string>::value, "");90#endif91}92 93int main(int, char**) {94 doIOTest<char>();95#ifndef TEST_HAS_NO_WIDE_CHARACTERS96 doIOTest<wchar_t>();97#endif98 // TODO(var-const): uncomment when it becomes possible to instantiate a `basic_ostream` object with a sized character99 // type (see https://llvm.org/PR53119).100 //doIOTest<char16_t>();101 //doIOTest<char32_t>();102 test_LWG2989();103 104 return 0;105}106