xref: /llvm-project/libcxx/test/std/input.output/file.streams/fstreams/filebuf.members/open_path.pass.cpp (revision c352fa7407122ee62d990d6b82551650149f98d4)
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 // UNSUPPORTED: c++03, c++11, c++14
10 
11 // UNSUPPORTED: availability-filesystem-missing
12 
13 // <fstream>
14 
15 // basic_filebuf<charT,traits>* open(const filesystem::path& p, ios_base::openmode mode);
16 
17 #include <fstream>
18 #include <filesystem>
19 #include <cassert>
20 #include "test_macros.h"
21 #include "platform_support.h"
22 
23 namespace fs = std::filesystem;
24 
main(int,char **)25 int main(int, char**) {
26 
27   fs::path p = get_temp_file_name();
28   {
29     std::filebuf f;
30     assert(f.open(p, std::ios_base::out) != 0);
31     assert(f.is_open());
32     assert(f.sputn("123", 3) == 3);
33   }
34   {
35     std::filebuf f;
36     assert(f.open(p, std::ios_base::in) != 0);
37     assert(f.is_open());
38     assert(f.sbumpc() == '1');
39     assert(f.sbumpc() == '2');
40     assert(f.sbumpc() == '3');
41   }
42   std::remove(p.string().c_str());
43 
44 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
45   {
46     std::wfilebuf f;
47     assert(f.open(p, std::ios_base::out) != 0);
48     assert(f.is_open());
49     assert(f.sputn(L"123", 3) == 3);
50   }
51   {
52     std::wfilebuf f;
53     assert(f.open(p, std::ios_base::in) != 0);
54     assert(f.is_open());
55     assert(f.sbumpc() == L'1');
56     assert(f.sbumpc() == L'2');
57     assert(f.sbumpc() == L'3');
58   }
59   std::remove(p.string().c_str());
60 #endif
61 
62   return 0;
63 }
64