Debian Codegraph @

libcxx/test/std/input.output/file.streams/fstreams/fstream.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_fstream
13
14 // void open(const char* s, ios_base::openmode mode = ios_base::in|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::fstream fs;
28 assert(!fs.is_open());
29 fs.open(temp.c_str(), std::ios_base::in | std::ios_base::out
30 | std::ios_base::trunc);
31 assert(fs.is_open());
32 double x = 0;
33 fs << 3.25;
34 fs.seekg(0);
35 fs >> x;
36 assert(x == 3.25);
37 }
38 std::remove(temp.c_str());
39
40 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
41 {
42 std::wfstream fs;
43 assert(!fs.is_open());
44 fs.open(temp.c_str(), std::ios_base::in | std::ios_base::out
45 | std::ios_base::trunc);
46 assert(fs.is_open());
47 double x = 0;
48 fs << 3.25;
49 fs.seekg(0);
50 fs >> x;
51 assert(x == 3.25);
52 }
53 std::remove(temp.c_str());
54 #endif
55
56 #if TEST_STD_VER >= 23
57 // Test all the noreplace flag combinations
58 {
59 std::ios_base::openmode modes[] = {
60 std::ios_base::out | std::ios_base::noreplace,
61 std::ios_base::out | std::ios_base::trunc | std::ios_base::noreplace,
62 std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::noreplace,
63 std::ios_base::out | std::ios_base::noreplace | std::ios_base::binary,
64 std::ios_base::out | std::ios_base::trunc | std::ios_base::noreplace | std::ios_base::binary,
65 std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::noreplace |
66 std::ios_base::binary,
67 };
68 for (auto mode : modes) {
69 std::string tmp = get_temp_file_name(); // also creates the file
70
71 {
72 std::fstream f;
73 f.open(tmp.c_str(), mode);
74 assert(!f.is_open()); // since it already exists
75 }
76
77 {
78 std::remove(tmp.c_str());
79
80 std::fstream f;
81 f.open(tmp.c_str(), mode);
82 assert(f.is_open()); // since it doesn't exist
83 }
84 }
85
86 # ifndef TEST_HAS_NO_WIDE_CHARACTERS
87 for (auto mode : modes) {
88 std::string tmp = get_temp_file_name(); // also creates the file
89
90 {
91 std::wfstream f;
92 f.open(tmp.c_str(), mode);
93 assert(!f.is_open()); // since it already exists
94 }
95
96 {
97 std::remove(tmp.c_str());
98
99 std::wfstream f;
100 f.open(tmp.c_str(), mode);
101 assert(f.is_open()); // since it doesn't exist
102 }
103 }
104 # endif
105 }
106 #endif // TEST_STD_VER >= 23
107
108 return 0;
109 }