Debian Codegraph @

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