Debian Codegraph @

libstdc++-v3/testsuite/27_io/basic_filebuf/native_handle/char/1.cc

raw
1 // { dg-options "-fno-inline" }
2 // { dg-do run { target c++26 } }
3 // { dg-add-options no_pch }
4 // { dg-additional-files "filebuf_members-1.txt" }
5
6 #include <fstream>
7
8 #ifndef __cpp_lib_fstream_native_handle
9 # error "Feature-test macro for fstream_native_handle missing in <fstream>"
10 #elif __cpp_lib_fstream_native_handle != 202306L
11 # error "Feature-test macro for fstream_native_handle has wrong value in <fstream>"
12 #endif
13
14 using type = std::basic_filebuf<char>::native_handle_type;
15
16 #include <cstdio> // std::fclose(FILE*)
17 #if __has_include(<unistd.h>)
18 # include <unistd.h> // close(int)
19 #endif
20 #if __has_include(<io.h>)
21 # include <io.h> // _open_osfhandle
22 # include <fcntl.h> // _O_RDONLY, _O_TEXT
23 #endif
24
25 #include <testsuite_hooks.h>
26
27 void
28 test01()
29 {
30 std::filebuf f;
31 f.open("filebuf_members-1.txt", std::ios::in);
32 type handle = f.native_handle();
33
34 auto native_close = []<typename HandleT>(HandleT handle) {
35 if constexpr (std::is_same_v<HandleT, std::FILE*>)
36 std::fclose(handle); // --enable-cstdio=stdio_pure
37 #if __has_include(<unistd.h>)
38 else if constexpr (std::is_same_v<HandleT, int>)
39 ::close(handle); // POSIX
40 #endif
41 #if __has_include(<handleapi.h>)
42 else if constexpr (std::is_same_v<HandleT, void*>) // Windows
43 ::_close(::_open_osfhandle((intptr_t)handle, _O_RDONLY|_O_TEXT));
44 #endif
45 else
46 VERIFY( false );
47 };
48 native_close(handle);
49
50 try
51 {
52 f.sgetc();
53 VERIFY( false );
54 }
55 catch (const std::ios::failure&)
56 {
57 }
58 }
59
60 int main()
61 {
62 test01();
63 }