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