Debian Codegraph @

libcxx/test/std/input.output/file.streams/fstreams/ofstream.members/open_pointer.pass.cpp

raw
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-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 // <fstream>
10
11 // template <class charT, class traits = char_traits<charT> >
12 // class basic_ofstream
13
14 // void open(const char* s, ios_base::openmode mode = ios_base::out);
15
16 // XFAIL: LIBCXX-AIX-FIXME
17
18 #include <fstream>
19 #include <cassert>
20 #include "test_macros.h"
21 #include "platform_support.h"
22
23 int main(int, char**)
24 {
25 std::string temp = get_temp_file_name();
26 {
27 std::ofstream fs;
28 assert(!fs.is_open());
29 char c = 'a';
30 fs << c;
31 assert(fs.fail());
32 fs.open(temp.c_str());
33 assert(fs.is_open());
34 fs << c;
35 }
36 {
37 std::ifstream fs(temp.c_str());
38 char c = 0;
39 fs >> c;
40 assert(c == 'a');
41 }
42 std::remove(temp.c_str());
43
44 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
45 {
46 std::wofstream fs;
47 assert(!fs.is_open());
48 wchar_t c = L'a';
49 fs << c;
50 assert(fs.fail());
51 fs.open(temp.c_str());
52 assert(fs.is_open());
53 fs << c;
54 }
55 {
56 std::wifstream fs(temp.c_str());
57 wchar_t c = 0;
58 fs >> c;
59 assert(c == L'a');
60 }
61 std::remove(temp.c_str());
62 #endif
63
64 #if TEST_STD_VER >= 23
65 // Test all the noreplace flag combinations
66 {
67 std::ios_base::openmode modes[] = {
68 std::ios_base::out | std::ios_base::noreplace,
69 std::ios_base::out | std::ios_base::trunc | std::ios_base::noreplace,
70 std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::noreplace,
71 std::ios_base::out | std::ios_base::noreplace | std::ios_base::binary,
72 std::ios_base::out | std::ios_base::trunc | std::ios_base::noreplace | std::ios_base::binary,
73 std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::noreplace |
74 std::ios_base::binary,
75 };
76 for (auto mode : modes) {
77 std::string tmp = get_temp_file_name(); // also creates the file
78
79 {
80 std::ofstream f;
81 f.open(tmp.c_str(), mode);
82 assert(!f.is_open()); // since it already exists
83 }
84
85 {
86 std::remove(tmp.c_str());
87
88 std::ofstream f;
89 f.open(tmp.c_str(), mode);
90 assert(f.is_open()); // since it doesn't exist
91 }
92 }
93
94 # ifndef TEST_HAS_NO_WIDE_CHARACTERS
95 for (auto mode : modes) {
96 std::string tmp = get_temp_file_name(); // also creates the file
97
98 {
99 std::wofstream f;
100 f.open(tmp.c_str(), mode);
101 assert(!f.is_open()); // since it already exists
102 }
103 {
104 std::remove(tmp.c_str());
105
106 std::wofstream f;
107 f.open(tmp.c_str(), mode);
108 assert(f.is_open()); // since it doesn't exist
109 }
110 }
111 # endif
112 }
113 #endif // TEST_STD_VER >= 23
114
115 return 0;
116 }