[libcxx-commits] [libcxx] [libc++] add floating point type check for uniform real distribution (PR #70564)
Nhat Nguyen via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Nov 27 15:46:05 PST 2023
https://github.com/changkhothuychung updated https://github.com/llvm/llvm-project/pull/70564
>From a30ac88730c0bd6d9dd1e0a9dc6ef9c9862db722 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] add floating point type check for uniform real distrubtion
Update libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp
Co-authored-by: Mark de Wever <zar-rpg at xs4all.nl>
---
libcxx/include/__random/cauchy_distribution.h | 3 +
.../__random/chi_squared_distribution.h | 4 +
.../__random/exponential_distribution.h | 3 +
.../__random/extreme_value_distribution.h | 3 +
.../include/__random/fisher_f_distribution.h | 3 +
libcxx/include/__random/gamma_distribution.h | 3 +
libcxx/include/__random/is_valid.h | 14 +++
.../include/__random/lognormal_distribution.h | 4 +
libcxx/include/__random/normal_distribution.h | 3 +
.../piecewise_constant_distribution.h | 3 +
.../__random/piecewise_linear_distribution.h | 3 +
.../include/__random/student_t_distribution.h | 3 +
.../__random/uniform_real_distribution.h | 3 +
.../include/__random/weibull_distribution.h | 4 +
.../rand.req.urng/valid_real_type.verify.cpp | 109 ++++++++++++++++++
15 files changed, 165 insertions(+)
create mode 100644 libcxx/test/libcxx/numerics/rand/rand.req.urng/valid_real_type.verify.cpp
diff --git a/libcxx/include/__random/cauchy_distribution.h b/libcxx/include/__random/cauchy_distribution.h
index 2fda6b53837673d..b93e48296cb17dc 100644
--- a/libcxx/include/__random/cauchy_distribution.h
+++ b/libcxx/include/__random/cauchy_distribution.h
@@ -28,6 +28,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS cauchy_distribution
{
+ static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
+ "RealType must be a supported floating-point type");
+
public:
// types
typedef _RealType result_type;
diff --git a/libcxx/include/__random/chi_squared_distribution.h b/libcxx/include/__random/chi_squared_distribution.h
index f2922b64dd6cfce..07c179c808538e2 100644
--- a/libcxx/include/__random/chi_squared_distribution.h
+++ b/libcxx/include/__random/chi_squared_distribution.h
@@ -11,6 +11,7 @@
#include <__config>
#include <__random/gamma_distribution.h>
+#include <__random/is_valid.h>
#include <iosfwd>
#include <limits>
@@ -26,6 +27,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS chi_squared_distribution
{
+ static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
+ "RealType must be a supported floating-point type");
+
public:
// types
typedef _RealType result_type;
diff --git a/libcxx/include/__random/exponential_distribution.h b/libcxx/include/__random/exponential_distribution.h
index b33b072eca08516..e2bdd6fe8827c8d 100644
--- a/libcxx/include/__random/exponential_distribution.h
+++ b/libcxx/include/__random/exponential_distribution.h
@@ -29,6 +29,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS exponential_distribution
{
+ static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
+ "RealType must be a supported floating-point type");
+
public:
// types
typedef _RealType result_type;
diff --git a/libcxx/include/__random/extreme_value_distribution.h b/libcxx/include/__random/extreme_value_distribution.h
index c583ec06a314a03..f02443ac1939e17 100644
--- a/libcxx/include/__random/extreme_value_distribution.h
+++ b/libcxx/include/__random/extreme_value_distribution.h
@@ -28,6 +28,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS extreme_value_distribution
{
+ static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
+ "RealType must be a supported floating-point type");
+
public:
// types
typedef _RealType result_type;
diff --git a/libcxx/include/__random/fisher_f_distribution.h b/libcxx/include/__random/fisher_f_distribution.h
index b757607bc461f4a..dc1d176b5b9b5aa 100644
--- a/libcxx/include/__random/fisher_f_distribution.h
+++ b/libcxx/include/__random/fisher_f_distribution.h
@@ -27,6 +27,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS fisher_f_distribution
{
+ static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
+ "RealType must be a supported floating-point type");
+
public:
// types
typedef _RealType result_type;
diff --git a/libcxx/include/__random/gamma_distribution.h b/libcxx/include/__random/gamma_distribution.h
index 777f6b5c76d5680..b5170a3fcc28b63 100644
--- a/libcxx/include/__random/gamma_distribution.h
+++ b/libcxx/include/__random/gamma_distribution.h
@@ -29,6 +29,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS gamma_distribution
{
+ static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
+ "RealType must be a supported floating-point type");
+
public:
// types
typedef _RealType result_type;
diff --git a/libcxx/include/__random/is_valid.h b/libcxx/include/__random/is_valid.h
index 113452bc5d35aa2..bde4733099d6884 100644
--- a/libcxx/include/__random/is_valid.h
+++ b/libcxx/include/__random/is_valid.h
@@ -23,6 +23,20 @@
_LIBCPP_BEGIN_NAMESPACE_STD
+// [rand.req.genl]/1.4:
+// The effect of instantiating a template that has a template type parameter
+// named RealType is undefined unless the corresponding template argument is
+// cv-unqualified and is one of float, double, or long double.
+
+template <class>
+struct __libcpp_random_is_valid_realtype : false_type {};
+template <>
+struct __libcpp_random_is_valid_realtype<float> : true_type {};
+template <>
+struct __libcpp_random_is_valid_realtype<double> : true_type {};
+template <>
+struct __libcpp_random_is_valid_realtype<long double> : true_type {};
+
// [rand.req.genl]/1.5:
// The effect of instantiating a template that has a template type parameter
// named IntType is undefined unless the corresponding template argument is
diff --git a/libcxx/include/__random/lognormal_distribution.h b/libcxx/include/__random/lognormal_distribution.h
index 1e8ac39dbac0319..140dae018ca6221 100644
--- a/libcxx/include/__random/lognormal_distribution.h
+++ b/libcxx/include/__random/lognormal_distribution.h
@@ -10,6 +10,7 @@
#define _LIBCPP___RANDOM_LOGNORMAL_DISTRIBUTION_H
#include <__config>
+#include <__random/is_valid.h>
#include <__random/normal_distribution.h>
#include <cmath>
#include <iosfwd>
@@ -27,6 +28,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS lognormal_distribution
{
+ static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
+ "RealType must be a supported floating-point type");
+
public:
// types
typedef _RealType result_type;
diff --git a/libcxx/include/__random/normal_distribution.h b/libcxx/include/__random/normal_distribution.h
index e2bf041b71fe2cd..5962528604488c0 100644
--- a/libcxx/include/__random/normal_distribution.h
+++ b/libcxx/include/__random/normal_distribution.h
@@ -28,6 +28,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS normal_distribution
{
+ static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
+ "RealType must be a supported floating-point type");
+
public:
// types
typedef _RealType result_type;
diff --git a/libcxx/include/__random/piecewise_constant_distribution.h b/libcxx/include/__random/piecewise_constant_distribution.h
index 5b26ab65e63f729..9c9899978098416 100644
--- a/libcxx/include/__random/piecewise_constant_distribution.h
+++ b/libcxx/include/__random/piecewise_constant_distribution.h
@@ -29,6 +29,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS piecewise_constant_distribution
{
+ static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
+ "RealType must be a supported floating-point type");
+
public:
// types
typedef _RealType result_type;
diff --git a/libcxx/include/__random/piecewise_linear_distribution.h b/libcxx/include/__random/piecewise_linear_distribution.h
index 6be44b29fcc8400..f4fe3c910ac7c2d 100644
--- a/libcxx/include/__random/piecewise_linear_distribution.h
+++ b/libcxx/include/__random/piecewise_linear_distribution.h
@@ -29,6 +29,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS piecewise_linear_distribution
{
+ static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
+ "RealType must be a supported floating-point type");
+
public:
// types
typedef _RealType result_type;
diff --git a/libcxx/include/__random/student_t_distribution.h b/libcxx/include/__random/student_t_distribution.h
index 2d2be23657ae0b2..09cef2ce5294756 100644
--- a/libcxx/include/__random/student_t_distribution.h
+++ b/libcxx/include/__random/student_t_distribution.h
@@ -29,6 +29,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS student_t_distribution
{
+ static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
+ "RealType must be a supported floating-point type");
+
public:
// types
typedef _RealType result_type;
diff --git a/libcxx/include/__random/uniform_real_distribution.h b/libcxx/include/__random/uniform_real_distribution.h
index 1388cef95f39414..48baa951388be76 100644
--- a/libcxx/include/__random/uniform_real_distribution.h
+++ b/libcxx/include/__random/uniform_real_distribution.h
@@ -27,6 +27,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS uniform_real_distribution
{
+ static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
+ "RealType must be a supported floating-point type");
+
public:
// types
typedef _RealType result_type;
diff --git a/libcxx/include/__random/weibull_distribution.h b/libcxx/include/__random/weibull_distribution.h
index df834e6557c835f..b67cf3675bc46a9 100644
--- a/libcxx/include/__random/weibull_distribution.h
+++ b/libcxx/include/__random/weibull_distribution.h
@@ -11,6 +11,7 @@
#include <__config>
#include <__random/exponential_distribution.h>
+#include <__random/is_valid.h>
#include <cmath>
#include <iosfwd>
#include <limits>
@@ -27,6 +28,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS weibull_distribution
{
+ static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
+ "RealType must be a supported floating-point type");
+
public:
// types
typedef _RealType result_type;
diff --git a/libcxx/test/libcxx/numerics/rand/rand.req.urng/valid_real_type.verify.cpp b/libcxx/test/libcxx/numerics/rand/rand.req.urng/valid_real_type.verify.cpp
new file mode 100644
index 000000000000000..6c6a109227df9f6
--- /dev/null
+++ b/libcxx/test/libcxx/numerics/rand/rand.req.urng/valid_real_type.verify.cpp
@@ -0,0 +1,109 @@
+//===----------------------------------------------------------------------===//
+//
+// 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>
+
+#include <random>
+
+void test() {
+ {
+ std::uniform_real_distribution<int>
+ baddist; //expected-error@*:* {{RealType must be a supported floating-point type}}
+ std::uniform_real_distribution<double> okdist;
+ (void)baddist;
+ (void)okdist;
+ }
+ {
+ std::exponential_distribution<int>
+ baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
+ std::exponential_distribution<double> okdist;
+ (void)baddist;
+ (void)okdist;
+ }
+
+ {
+ std::gamma_distribution<int> baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
+ std::gamma_distribution<double> okdist;
+ (void)baddist;
+ (void)okdist;
+ }
+
+ {
+ std::weibull_distribution<int> baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
+ std::weibull_distribution<double> okdist;
+ (void)baddist;
+ (void)okdist;
+ }
+
+ {
+ std::extreme_value_distribution<int>
+ baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
+ std::extreme_value_distribution<double> okdist;
+ (void)baddist;
+ (void)okdist;
+ }
+
+ {
+ std::normal_distribution<int> baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
+ std::normal_distribution<double> okdist;
+ (void)baddist;
+ (void)okdist;
+ }
+
+ {
+ std::lognormal_distribution<int> baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
+ std::lognormal_distribution<double> okdist;
+ (void)baddist;
+ (void)okdist;
+ }
+
+ {
+ std::chi_squared_distribution<int>
+ baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
+ std::chi_squared_distribution<double> okdist;
+ (void)baddist;
+ (void)okdist;
+ }
+
+ {
+ std::cauchy_distribution<int> baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
+ std::cauchy_distribution<double> okdist;
+ (void)baddist;
+ (void)okdist;
+ }
+
+ {
+ std::fisher_f_distribution<int> baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
+ std::fisher_f_distribution<double> okdist;
+ (void)baddist;
+ (void)okdist;
+ }
+
+ {
+ std::student_t_distribution<int> baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
+ std::student_t_distribution<double> okdist;
+ (void)baddist;
+ (void)okdist;
+ }
+
+ {
+ std::piecewise_constant_distribution<int>
+ baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
+ std::piecewise_constant_distribution<double> okdist;
+ (void)baddist;
+ (void)okdist;
+ }
+
+ {
+ std::piecewise_linear_distribution<int>
+ baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
+ std::piecewise_linear_distribution<double> okdist;
+ (void)baddist;
+ (void)okdist;
+ }
+}
More information about the libcxx-commits
mailing list