public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed] libstdc++: Add missing tests for std::basic_filebuf::native_handle()
Date: Sat, 16 Sep 2023 00:17:37 +0100	[thread overview]
Message-ID: <20230915231748.2348413-1-jwakely@redhat.com> (raw)

Tested aarch64-linux. Pushed to trunk.

-- >8 --

I forgot to 'git add' these files in the commit that added the new
member function to basic_filebuf.

libstdc++-v3/ChangeLog:

	* testsuite/27_io/basic_filebuf/native_handle/char/1.cc: New test.
	* testsuite/27_io/basic_filebuf/native_handle/wchar_t/1.cc: New test.
---
 .../basic_filebuf/native_handle/char/1.cc     | 61 +++++++++++++++++++
 .../basic_filebuf/native_handle/wchar_t/1.cc  | 55 +++++++++++++++++
 2 files changed, 116 insertions(+)
 create mode 100644 libstdc++-v3/testsuite/27_io/basic_filebuf/native_handle/char/1.cc
 create mode 100644 libstdc++-v3/testsuite/27_io/basic_filebuf/native_handle/wchar_t/1.cc

diff --git a/libstdc++-v3/testsuite/27_io/basic_filebuf/native_handle/char/1.cc b/libstdc++-v3/testsuite/27_io/basic_filebuf/native_handle/char/1.cc
new file mode 100644
index 00000000000..749451a4c81
--- /dev/null
+++ b/libstdc++-v3/testsuite/27_io/basic_filebuf/native_handle/char/1.cc
@@ -0,0 +1,61 @@
+// { dg-options "-fno-inline" }
+// { dg-do run { target c++26 } }
+// { dg-additional-files "filebuf_members-1.txt" }
+
+#include <fstream>
+
+#ifndef __cpp_lib_fstream_native_handle
+# error "Feature-test macro for fstream_native_handle missing in <fstream>"
+#elif __cpp_lib_fstream_native_handle != 202306L
+# error "Feature-test macro for fstream_native_handle has wrong value in <fstream>"
+#endif
+
+using type = std::basic_filebuf<char>::native_handle_type;
+
+#include <cstdio> // std::fclose(FILE*)
+#if __has_include(<unistd.h>)
+# include <unistd.h> // close(int)
+#endif
+#if __has_include(<handleapi.h>)
+# include <handleapi.h> // CloseHandle(HANDLE)
+#endif
+
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+  std::filebuf f;
+  f.open("filebuf_members-1.txt", std::ios::in);
+  type handle = f.native_handle();
+
+  auto native_close = []<typename HandleT>(HandleT handle) {
+    if constexpr (std::is_same_v<HandleT, std::FILE*>)
+      std::fclose(handle); // --enable-cstdio=stdio_pure
+#if __has_include(<unistd.h>)
+    else if constexpr (std::is_same_v<HandleT, int>)
+      ::close(handle); // POSIX
+#endif
+#if __has_include(<handleapi.h>)
+    else if constexpr (std::is_same_v<HandleT, void*>)
+      ::CloseHandle(handle); // Windows
+#endif
+    else
+      VERIFY( false );
+  };
+  native_close(handle);
+
+  try
+  {
+    f.sgetc();
+    VERIFY( false );
+  }
+  catch (const std::ios::failure&)
+  {
+  }
+}
+
+int main()
+{
+  test01();
+}
diff --git a/libstdc++-v3/testsuite/27_io/basic_filebuf/native_handle/wchar_t/1.cc b/libstdc++-v3/testsuite/27_io/basic_filebuf/native_handle/wchar_t/1.cc
new file mode 100644
index 00000000000..7ee5e4e8f53
--- /dev/null
+++ b/libstdc++-v3/testsuite/27_io/basic_filebuf/native_handle/wchar_t/1.cc
@@ -0,0 +1,55 @@
+// { dg-options "-fno-inline" }
+// { dg-do run { target c++26 } }
+// { dg-additional-files "filebuf_members-1.txt" }
+
+#include <fstream>
+
+using type = std::basic_filebuf<wchar_t>::native_handle_type;
+
+#include <cstdio> // std::fclose(FILE*)
+#if __has_include(<unistd.h>)
+# include <unistd.h> // close(int)
+#endif
+#if __has_include(<handleapi.h>)
+# include <handleapi.h> // CloseHandle(HANDLE)
+#endif
+
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+  std::wfilebuf f;
+  f.open("filebuf_members-1.txt", std::wios::in);
+  type handle = f.native_handle();
+
+  auto native_close = []<typename HandleT>(HandleT handle) {
+    if constexpr (std::is_same_v<HandleT, std::FILE*>)
+      std::fclose(handle); // --enable-cstdio=stdio_pure
+#if __has_include(<unistd.h>)
+    else if constexpr (std::is_same_v<HandleT, int>)
+      ::close(handle); // POSIX
+#endif
+#if __has_include(<handleapi.h>)
+    else if constexpr (std::is_same_v<HandleT, void*>)
+      ::CloseHandle(handle); // Windows
+#endif
+    else
+      VERIFY( false );
+  };
+  native_close(handle);
+
+  try
+  {
+    f.sgetc();
+    VERIFY( false );
+  }
+  catch (const std::ios::failure&)
+  {
+  }
+}
+
+int main()
+{
+  test01();
+}
-- 
2.41.0


                 reply	other threads:[~2023-09-15 23:17 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230915231748.2348413-1-jwakely@redhat.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).