127 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// <fstream>10 11// template <class charT, class traits = char_traits<charT> >12// class basic_fstream13 14// explicit basic_fstream(const char* s, ios_base::openmode mode = ios_base::in | ios_base::out);15 16// In C++23 and later, this test requires support for P2467R1 in the dylib (a3f17ba3febbd546f2342ffc780ac93b694fdc8d)17// XFAIL: (!c++03 && !c++11 && !c++14 && !c++17 && !c++20) && using-built-library-before-llvm-1818 19// XFAIL: LIBCXX-AIX-FIXME20 21#include <fstream>22#include <cassert>23 24#include "test_macros.h"25#include "platform_support.h"26#include "operator_hijacker.h"27 28int main(int, char**)29{30 std::string temp = get_temp_file_name();31 {32 std::fstream fs(temp.c_str(), std::ios_base::in | std::ios_base::out33 | std::ios_base::trunc);34 double x = 0;35 fs << 3.25;36 fs.seekg(0);37 fs >> x;38 assert(x == 3.25);39 }40 std::remove(temp.c_str());41 42 {43 std::basic_fstream<char, operator_hijacker_char_traits<char> > fs(44 temp.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::trunc);45 std::basic_string<char, operator_hijacker_char_traits<char> > x;46 fs << "3.25";47 fs.seekg(0);48 fs >> x;49 assert(x == "3.25");50 }51 std::remove(temp.c_str());52 53#ifndef TEST_HAS_NO_WIDE_CHARACTERS54 {55 std::wfstream fs(temp.c_str(), std::ios_base::in | std::ios_base::out56 | std::ios_base::trunc);57 double x = 0;58 fs << 3.25;59 fs.seekg(0);60 fs >> x;61 assert(x == 3.25);62 }63 std::remove(temp.c_str());64 65 {66 std::basic_fstream<wchar_t, operator_hijacker_char_traits<wchar_t> > fs(67 temp.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::trunc);68 std::basic_string<wchar_t, operator_hijacker_char_traits<wchar_t> > x;69 fs << L"3.25";70 fs.seekg(0);71 fs >> x;72 assert(x == L"3.25");73 }74 std::remove(temp.c_str());75#endif76 77#if TEST_STD_VER >= 2378 // Test all the noreplace flag combinations79 {80 std::ios_base::openmode modes[] = {81 std::ios_base::out | std::ios_base::noreplace,82 std::ios_base::out | std::ios_base::trunc | std::ios_base::noreplace,83 std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::noreplace,84 std::ios_base::out | std::ios_base::noreplace | std::ios_base::binary,85 std::ios_base::out | std::ios_base::trunc | std::ios_base::noreplace | std::ios_base::binary,86 std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::noreplace |87 std::ios_base::binary,88 };89 for (auto mode : modes) {90 std::string tmp = get_temp_file_name(); // also creates the file91 92 {93 std::fstream f(tmp.c_str(), mode);94 assert(!f.is_open()); // since it already exists95 }96 97 {98 std::remove(tmp.c_str());99 100 std::fstream f(tmp.c_str(), mode);101 assert(f.is_open()); // since it doesn't exist102 }103 }104 105# ifndef TEST_HAS_NO_WIDE_CHARACTERS106 for (auto mode : modes) {107 std::string tmp = get_temp_file_name(); // also creates the file108 109 {110 std::wfstream f(tmp.c_str(), mode);111 assert(!f.is_open()); // since it already exists112 }113 114 {115 std::remove(tmp.c_str());116 117 std::wfstream f(tmp.c_str(), mode);118 assert(f.is_open()); // since it doesn't exist119 }120 }121# endif122 }123#endif // TEST_STD_VER >= 23124 125 return 0;126}127