brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.6 KiB · fbb03f1 Raw
181 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_ofstream13 14// explicit basic_ofstream(const char* s, ios_base::openmode mode = 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#include <ios>24 25#include "test_macros.h"26#include "operator_hijacker.h"27#include "platform_support.h"28 29int main(int, char**)30{31    std::string temp = get_temp_file_name();32 33    {34        std::ofstream fs(temp.c_str());35        fs << 3.25;36    }37    {38        std::ifstream fs(temp.c_str());39        double x = 0;40        fs >> x;41        assert(x == 3.25);42    }43    std::remove(temp.c_str());44 45    {46      std::basic_ofstream<char, operator_hijacker_char_traits<char> > fs(temp.c_str());47      fs << "3.25";48    }49    {50      std::ifstream fs(temp.c_str());51      double x = 0;52      fs >> x;53      assert(x == 3.25);54    }55    std::remove(temp.c_str());56 57    {58      std::ofstream fs(temp.c_str(), std::ios_base::out);59      fs << 3.25;60    }61    {62      std::ifstream fs(temp.c_str());63      double x = 0;64      fs >> x;65      assert(x == 3.25);66    }67    std::remove(temp.c_str());68 69    {70      std::basic_ofstream<char, operator_hijacker_char_traits<char> > fs(temp.c_str(), std::ios_base::out);71      fs << "3.25";72    }73    {74      std::ifstream fs(temp.c_str());75      double x = 0;76      fs >> x;77      assert(x == 3.25);78    }79    std::remove(temp.c_str());80#ifndef TEST_HAS_NO_WIDE_CHARACTERS81    {82        std::wofstream fs(temp.c_str());83        fs << 3.25;84    }85    {86        std::wifstream fs(temp.c_str());87        double x = 0;88        fs >> x;89        assert(x == 3.25);90    }91    std::remove(temp.c_str());92 93    {94      std::basic_ofstream<wchar_t, operator_hijacker_char_traits<wchar_t> > fs(temp.c_str());95      fs << L"3.25";96    }97    {98      std::ifstream fs(temp.c_str());99      double x = 0;100      fs >> x;101      assert(x == 3.25);102    }103    std::remove(temp.c_str());104 105    {106      std::wofstream fs(temp.c_str(), std::ios_base::out);107      fs << 3.25;108    }109    {110      std::wifstream fs(temp.c_str());111      double x = 0;112      fs >> x;113      assert(x == 3.25);114    }115    std::remove(temp.c_str());116 117    {118      std::basic_ofstream<wchar_t, operator_hijacker_char_traits<wchar_t> > fs(temp.c_str(), std::ios_base::out);119      fs << L"3.25";120    }121    {122      std::ifstream fs(temp.c_str());123      double x = 0;124      fs >> x;125      assert(x == 3.25);126    }127    std::remove(temp.c_str());128 129#endif130 131#if TEST_STD_VER >= 23132    // Test all the noreplace flag combinations133    {134        std::ios_base::openmode modes[] = {135            std::ios_base::out | std::ios_base::noreplace,136            std::ios_base::out | std::ios_base::trunc | std::ios_base::noreplace,137            std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::noreplace,138            std::ios_base::out | std::ios_base::noreplace | std::ios_base::binary,139            std::ios_base::out | std::ios_base::trunc | std::ios_base::noreplace | std::ios_base::binary,140            std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::noreplace |141                std::ios_base::binary,142        };143        for (auto mode : modes) {144          std::string tmp = get_temp_file_name(); // also creates the file145 146          {147            std::ofstream f(tmp.c_str(), mode);148            assert(!f.is_open()); // since it already exists149          }150 151          {152            std::remove(tmp.c_str());153 154            std::ofstream f(tmp.c_str(), mode);155            assert(f.is_open()); // since it doesn't exist156          }157        }158 159#  ifndef TEST_HAS_NO_WIDE_CHARACTERS160        for (auto mode : modes) {161          std::string tmp = get_temp_file_name(); // also creates the file162 163          {164            std::wofstream f(tmp.c_str(), mode);165            assert(!f.is_open()); // since it already exists166          }167 168          {169            std::remove(tmp.c_str());170 171            std::wofstream f(tmp.c_str(), mode);172            assert(f.is_open()); // since it doesn't exist173          }174        }175#  endif176    }177#endif // TEST_STD_VER >= 23178 179    return 0;180}181