brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.1 KiB · 53c6bed Raw
170 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 11// <fstream>12 13// plate <class charT, class traits = char_traits<charT> >14// class basic_ofstream15 16// template<class T>17// explicit basic_ofstream(const T& s, ios_base::openmode mode = ios_base::out); // Since C++1718// Constraints: is_same_v<T, filesystem::path> is true19 20#include <cassert>21#include <filesystem>22#include <fstream>23#include <type_traits>24 25#include "platform_support.h"26#include "operator_hijacker.h"27#include "test_macros.h"28#include "test_iterators.h"29 30namespace fs = std::filesystem;31 32template <class CharT>33constexpr bool test_non_convert_to_path() {34  // String types35  static_assert(!std::is_constructible_v<std::ofstream, std::basic_string_view<CharT>>);36  static_assert(!std::is_constructible_v<std::ofstream, const std::basic_string_view<CharT>>);37 38  // Char* pointers39  if constexpr (!std::is_same_v<CharT, char> && !std::is_same_v<CharT, fs::path::value_type>)40    static_assert(!std::is_constructible_v<std::ofstream, const CharT*>);41 42  // Iterators43  static_assert(!std::is_convertible_v<std::ofstream, cpp17_input_iterator<const CharT*>>);44 45  return true;46}47 48static_assert(test_non_convert_to_path<char>());49 50#if !defined(TEST_HAS_NO_WIDE_CHARACTERS) && !defined(TEST_HAS_OPEN_WITH_WCHAR)51static_assert(test_non_convert_to_path<wchar_t>());52#endif // !TEST_HAS_NO_WIDE_CHARACTERS && !TEST_HAS_OPEN_WITH_WCHAR53 54#ifndef TEST_HAS_NO_CHAR8_T55static_assert(test_non_convert_to_path<char8_t>());56#endif // TEST_HAS_NO_CHAR8_T57 58static_assert(test_non_convert_to_path<char16_t>());59static_assert(test_non_convert_to_path<char32_t>());60 61int main(int, char**) {62  {63    static_assert(!std::is_convertible<fs::path, std::ofstream>::value,64                  "ctor should be explicit");65    static_assert(std::is_constructible<std::ofstream, fs::path const&,66                                        std::ios_base::openmode>::value,67                  "");68  }69  fs::path p = get_temp_file_name();70  {71    std::ofstream stream(p);72    stream << 3.25;73  }74  {75    std::ifstream stream(p);76    double x = 0;77    stream >> x;78    assert(x == 3.25);79  }80  std::remove(p.string().c_str());81 82  {83    std::basic_ofstream<char, operator_hijacker_char_traits<char> > stream(p);84    stream << "3.25";85  }86  {87    std::ifstream stream(p);88    double x = 0;89    stream >> x;90    assert(x == 3.25);91  }92  std::remove(p.string().c_str());93 94  {95    std::ofstream stream(p, std::ios_base::out);96    stream << 3.25;97  }98  {99    std::ifstream stream(p);100    double x = 0;101    stream >> x;102    assert(x == 3.25);103  }104  std::remove(p.string().c_str());105 106  {107    std::basic_ofstream<char, operator_hijacker_char_traits<char> > stream(p, std::ios_base::out);108    stream << "3.25";109  }110  {111    std::ifstream stream(p);112    double x = 0;113    stream >> x;114    assert(x == 3.25);115  }116  std::remove(p.string().c_str());117 118#ifndef TEST_HAS_NO_WIDE_CHARACTERS119  {120    std::wofstream stream(p);121    stream << 3.25;122  }123  {124    std::wifstream stream(p);125    double x = 0;126    stream >> x;127    assert(x == 3.25);128  }129  std::remove(p.string().c_str());130 131  {132    std::basic_ofstream<wchar_t, operator_hijacker_char_traits<wchar_t> > stream(p);133    stream << L"3.25";134  }135  {136    std::wifstream stream(p);137    double x = 0;138    stream >> x;139    assert(x == 3.25);140  }141  std::remove(p.string().c_str());142 143  {144    std::wofstream stream(p, std::ios_base::out);145    stream << 3.25;146  }147  {148    std::wifstream stream(p);149    double x = 0;150    stream >> x;151    assert(x == 3.25);152  }153  std::remove(p.string().c_str());154 155  {156    std::basic_ofstream<wchar_t, operator_hijacker_char_traits<wchar_t> > stream(p, std::ios_base::out);157    stream << L"3.25";158  }159  {160    std::wifstream stream(p);161    double x = 0;162    stream >> x;163    assert(x == 3.25);164  }165  std::remove(p.string().c_str());166#endif167 168  return 0;169}170