[libcxx-commits] [libcxx] [libc++] add floating point type check for uniform real distrubtion (PR #70564)

Nhat Nguyen via libcxx-commits libcxx-commits at lists.llvm.org
Sun Oct 29 19:58:27 PDT 2023


https://github.com/NhatNguyen1810 updated https://github.com/llvm/llvm-project/pull/70564

>From a8b9c704511411d38bdcf82db66ca0e99d1a4246 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E2=80=9CNhat?= <“nhat7203 at gmail.com”>
Date: Sat, 28 Oct 2023 14:30:43 -0400
Subject: [PATCH 01/10] add floating point type check for uniform real
 distrubtion

---
 .../__random/uniform_real_distribution.h        |  3 +++
 .../random/uniform_real_distribution.pass.cpp   | 17 +++++++++++++++++
 .../test/libcxx/transitive_includes/cxx23.csv   |  1 +
 .../test/libcxx/transitive_includes/cxx26.csv   |  1 +
 4 files changed, 22 insertions(+)
 create mode 100644 libcxx/test/libcxx/random/uniform_real_distribution.pass.cpp

diff --git a/libcxx/include/__random/uniform_real_distribution.h b/libcxx/include/__random/uniform_real_distribution.h
index 1388cef95f39414..975c1dff3a32741 100644
--- a/libcxx/include/__random/uniform_real_distribution.h
+++ b/libcxx/include/__random/uniform_real_distribution.h
@@ -14,6 +14,7 @@
 #include <__random/is_valid.h>
 #include <iosfwd>
 #include <limits>
+#include <type_traits>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
@@ -27,6 +28,8 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 template<class _RealType = double>
 class _LIBCPP_TEMPLATE_VIS uniform_real_distribution
 {
+  static_assert(std::is_floating_point<_RealType>::value, "result_type must be floating type");
+
 public:
     // types
     typedef _RealType result_type;
diff --git a/libcxx/test/libcxx/random/uniform_real_distribution.pass.cpp b/libcxx/test/libcxx/random/uniform_real_distribution.pass.cpp
new file mode 100644
index 000000000000000..ad72552eed07553
--- /dev/null
+++ b/libcxx/test/libcxx/random/uniform_real_distribution.pass.cpp
@@ -0,0 +1,17 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// random
+
+// template<_RealType = double>
+// result_type must be floating type
+
+#include <random>
+#include <type_traits>
+
+struct test_uniform_real_distribution : public std::uniform_real_distribution<int> {};
diff --git a/libcxx/test/libcxx/transitive_includes/cxx23.csv b/libcxx/test/libcxx/transitive_includes/cxx23.csv
index 236dedd186c9268..1035cb418822de8 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx23.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx23.csv
@@ -495,6 +495,7 @@ random iosfwd
 random limits
 random numeric
 random string
+random type_traits
 random vector
 random version
 ranges compare
diff --git a/libcxx/test/libcxx/transitive_includes/cxx26.csv b/libcxx/test/libcxx/transitive_includes/cxx26.csv
index 236dedd186c9268..1035cb418822de8 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx26.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx26.csv
@@ -495,6 +495,7 @@ random iosfwd
 random limits
 random numeric
 random string
+random type_traits
 random vector
 random version
 ranges compare

>From 00ea9a64ae7d9cc2bfdf44da0b0715436759bd76 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E2=80=9CNhat?= <“nhat7203 at gmail.com”>
Date: Sun, 29 Oct 2023 19:30:59 -0400
Subject: [PATCH 02/10] add floating point type check for uniform real
 distrubtion

---
 .../traits_mismatch.verify.cpp}                                   | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename libcxx/test/libcxx/random/{uniform_real_distribution.pass.cpp => random.uniform.real/traits_mismatch.verify.cpp} (100%)

diff --git a/libcxx/test/libcxx/random/uniform_real_distribution.pass.cpp b/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp
similarity index 100%
rename from libcxx/test/libcxx/random/uniform_real_distribution.pass.cpp
rename to libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp

>From 2d8909feac615879a3f8131e860723633238ab4e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E2=80=9CNhat?= <“nhat7203 at gmail.com”>
Date: Sun, 29 Oct 2023 19:49:58 -0400
Subject: [PATCH 03/10] add floating point type check for uniform real
 distrubtion

---
 .../random/random.uniform.real/traits_mismatch.verify.cpp       | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp b/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp
index ad72552eed07553..1ba1a4d35e8863a 100644
--- a/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp
+++ b/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp
@@ -15,3 +15,5 @@
 #include <type_traits>
 
 struct test_uniform_real_distribution : public std::uniform_real_distribution<int> {};
+
+// expected-error at random:* {{static assertion failed{{.*}}traits_type::result_type must be floating type}}

>From 70ae882b66c583cb34bfc6e0e098c60246c56b89 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E2=80=9CNhat?= <“nhat7203 at gmail.com”>
Date: Sun, 29 Oct 2023 19:55:04 -0400
Subject: [PATCH 04/10] add floating point type check for uniform real
 distrubtion

---
 .../random/random.uniform.real/traits_mismatch.verify.cpp       | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp b/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp
index 1ba1a4d35e8863a..4f4229c738bbdd2 100644
--- a/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp
+++ b/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp
@@ -16,4 +16,4 @@
 
 struct test_uniform_real_distribution : public std::uniform_real_distribution<int> {};
 
-// expected-error at random:* {{static assertion failed{{.*}}traits_type::result_type must be floating type}}
+// expected-error at random:* {{static assertion failed due to requirement 'std::is_floating_point<int>::value': result_type must be floating type}}

>From faa16b8aeb535eb8d0278e8113b45399d1205d7d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E2=80=9CNhat?= <“nhat7203 at gmail.com”>
Date: Sun, 29 Oct 2023 20:03:39 -0400
Subject: [PATCH 05/10] add floating point type check for uniform real
 distrubtion

---
 .../random/random.uniform.real/traits_mismatch.verify.cpp       | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp b/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp
index 4f4229c738bbdd2..fd8e14d78312088 100644
--- a/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp
+++ b/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp
@@ -16,4 +16,4 @@
 
 struct test_uniform_real_distribution : public std::uniform_real_distribution<int> {};
 
-// expected-error at random:* {{static assertion failed due to requirement 'std::is_floating_point<int>::value': result_type must be floating type}}
+// expected-error-re at random:* {{static assertion failed due to requirement 'std::is_floating_point<int>::value': result_type must be floating type}}

>From dae2ea9c1e6b78fcc48ab514970e6868f044f837 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E2=80=9CNhat?= <“nhat7203 at gmail.com”>
Date: Sun, 29 Oct 2023 20:18:49 -0400
Subject: [PATCH 06/10] add floating point type check for uniform real
 distrubtion

---
 .../random/random.uniform.real/traits_mismatch.verify.cpp       | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp b/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp
index fd8e14d78312088..c7f0c9c01e9ded6 100644
--- a/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp
+++ b/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp
@@ -16,4 +16,4 @@
 
 struct test_uniform_real_distribution : public std::uniform_real_distribution<int> {};
 
-// expected-error-re at random:* {{static assertion failed due to requirement 'std::is_floating_point<int>::value': result_type must be floating type}}
+// expected-error-re at random:* {{static assertion failed{{.*}}traits_type::result_type must be floating type}}

>From c1b8695b52d1b510b9449862b9e05a5575b1257b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E2=80=9CNhat?= <“nhat7203 at gmail.com”>
Date: Sun, 29 Oct 2023 22:06:52 -0400
Subject: [PATCH 07/10] add floating point type check for uniform real
 distrubtion

---
 .../random.uniform.real/traits_mismatch.verify.cpp     | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp b/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp
index c7f0c9c01e9ded6..6908069c5129d7f 100644
--- a/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp
+++ b/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp
@@ -6,14 +6,18 @@
 //
 //===----------------------------------------------------------------------===//
 
-// random
+// __random
 
 // template<_RealType = double>
+// class uniform_real_distribution; 
+
 // result_type must be floating type
 
+// UNSUPPORTED: no-int-type
+
 #include <random>
 #include <type_traits>
 
-struct test_uniform_real_distribution : public std::uniform_real_distribution<int> {};
+struct test_random : public std::uniform_real_distribution<int> {};
 
-// expected-error-re at random:* {{static assertion failed{{.*}}traits_type::result_type must be floating type}}
+// expected-error@*:* {{static assertion failed due to requirement 'std::is_floating_point<int>::value'{{.*}}result_type must be floating type}}

>From 1c1e145f6e4a6752d3cb85fdc771c35bf400eb7b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E2=80=9CNhat?= <“nhat7203 at gmail.com”>
Date: Sun, 29 Oct 2023 22:25:27 -0400
Subject: [PATCH 08/10] add floating point type check for uniform real
 distrubtion

---
 .../random/random.uniform.real/traits_mismatch.verify.cpp       | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp b/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp
index 6908069c5129d7f..9e3bde26df54ea1 100644
--- a/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp
+++ b/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp
@@ -20,4 +20,4 @@
 
 struct test_random : public std::uniform_real_distribution<int> {};
 
-// expected-error@*:* {{static assertion failed due to requirement 'std::is_floating_point<int>::value'{{.*}}result_type must be floating type}}
+// expected-error at uniform_real_distribution:* {{static assertion failed due to requirement 'std::is_floating_point<int>::value'{{.*}}result_type must be floating type}}

>From 457b647623c835b9a9c2502847e8f25d9fa6ce71 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E2=80=9CNhat?= <“nhat7203 at gmail.com”>
Date: Sun, 29 Oct 2023 22:46:51 -0400
Subject: [PATCH 09/10] add floating point type check for uniform real
 distrubtion

---
 .../random/random.uniform.real/traits_mismatch.verify.cpp     | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp b/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp
index 9e3bde26df54ea1..d5b3ee229ff165d 100644
--- a/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp
+++ b/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp
@@ -6,7 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-// __random
+// <random>
 
 // template<_RealType = double>
 // class uniform_real_distribution; 
@@ -20,4 +20,4 @@
 
 struct test_random : public std::uniform_real_distribution<int> {};
 
-// expected-error at uniform_real_distribution:* {{static assertion failed due to requirement 'std::is_floating_point<int>::value'{{.*}}result_type must be floating type}}
+// expected-error-re at random:* {{static assertion failed due to requirement 'std::is_floating_point<int>::value'{{.*}}result_type must be floating type}}

>From 55b0887f643300ba6358d74a2d8eb4d7d4f074f6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E2=80=9CNhat?= <“nhat7203 at gmail.com”>
Date: Sun, 29 Oct 2023 22:58:10 -0400
Subject: [PATCH 10/10] add floating point type check for uniform real
 distrubtion

---
 .../random/random.uniform.real/traits_mismatch.verify.cpp      | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp b/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp
index d5b3ee229ff165d..5d636058c3c850b 100644
--- a/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp
+++ b/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp
@@ -16,8 +16,7 @@
 // UNSUPPORTED: no-int-type
 
 #include <random>
-#include <type_traits>
 
 struct test_random : public std::uniform_real_distribution<int> {};
 
-// expected-error-re at random:* {{static assertion failed due to requirement 'std::is_floating_point<int>::value'{{.*}}result_type must be floating type}}
+// expected-error-re@*:* {{static assertion failed due to requirement 'std::is_floating_point<int>::value'{{.*}}result_type must be floating type}}



More information about the libcxx-commits mailing list