Debian Codegraph @

libcxx/test/std/input.output/file.streams/fstreams/native_handle_test_helpers.h

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 #ifndef TEST_STD_INPUT_OUTPUT_FILE_STREAMS_FSTREAMS_TEST_HELPERS_H
10 #define TEST_STD_INPUT_OUTPUT_FILE_STREAMS_FSTREAMS_TEST_HELPERS_H
11
12 #include <cassert>
13 #include <concepts>
14 #include <cstdio>
15 #include <fstream>
16 #include <filesystem>
17 #include <type_traits>
18 #include <utility>
19
20 #if defined(_WIN32)
21 # include <io.h>
22 # include <windows.h>
23 #else
24 # include <fcntl.h>
25 #endif
26
27 #include "platform_support.h"
28 #include "types.h"
29
30 #if TEST_STD_VER >= 26
31
32 inline bool is_handle_valid(NativeHandleT handle) {
33 # if defined(_WIN32)
34 BY_HANDLE_FILE_INFORMATION fileInformation;
35 return GetFileInformationByHandle(handle, &fileInformation);
36 # elif __has_include(<unistd.h>) // POSIX
37 return fcntl(handle, F_GETFL) != -1 || errno != EBADF;
38 # else
39 # error "Provide a native file handle!"
40 # endif
41 }
42
43 template <typename CharT, typename StreamT>
44 inline void test_native_handle() {
45 static_assert(
46 std::is_same_v<typename std::basic_filebuf<CharT>::native_handle_type, typename StreamT::native_handle_type>);
47
48 StreamT f;
49 std::filesystem::path p = get_temp_file_name();
50
51 // non-const
52 {
53 f.open(p);
54 std::same_as<NativeHandleT> decltype(auto) handle = f.native_handle();
55 assert(is_handle_valid(handle));
56 assert(f.rdbuf()->native_handle() == handle);
57 assert(std::as_const(f).rdbuf()->native_handle() == handle);
58 f.close();
59 assert(!is_handle_valid(handle));
60 static_assert(noexcept(f.native_handle()));
61 }
62 // const
63 {
64 f.open(p);
65 std::same_as<NativeHandleT> decltype(auto) const_handle = std::as_const(f).native_handle();
66 assert(is_handle_valid(const_handle));
67 assert(f.rdbuf()->native_handle() == const_handle);
68 assert(std::as_const(f).rdbuf()->native_handle() == const_handle);
69 f.close();
70 assert(!is_handle_valid(const_handle));
71 static_assert(noexcept(std::as_const(f).native_handle()));
72 }
73 }
74
75 template <typename StreamT>
76 inline void test_native_handle_type() {
77 static_assert(std::is_trivially_copyable_v<typename StreamT::native_handle_type>);
78 static_assert(std::semiregular<typename StreamT::native_handle_type>);
79 static_assert(std::is_same_v<typename StreamT::native_handle_type, NativeHandleT>);
80 }
81
82 #endif // #if TEST_STD_VER >= 26
83
84 #endif // TEST_STD_INPUT_OUTPUT_FILE_STREAMS_FSTREAMS_TEST_HELPERS_H