[libcxx-commits] [libcxx] [RFC][libc++] Fixes valarray proxy type operations. (PR #76528)
Mark de Wever via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Jan 10 10:00:04 PST 2024
https://github.com/mordante updated https://github.com/llvm/llvm-project/pull/76528
>From 26676bc2144ebcdbe53e4bf5a2f714e1f22e1aa3 Mon Sep 17 00:00:00 2001
From: Mark de Wever <koraq at xs4all.nl>
Date: Thu, 28 Dec 2023 20:23:00 +0100
Subject: [PATCH 1/2] [RFC][libc++] Fixes valarray proxy type operations.
The valarray<>::operator[](...) const functions return proxy objects.
The valarray<>::operator[](...) functions return valarray objects.
However the standard allows functions return valarray object to return
custom proxy objects. Libc++ returns __val_expr proxies. Functions taking
a valarray object should work with the custom proxies. Therefore several
operations have a custom proxy overload instead of valarray overloads.
Libc++ doesn't specify a valarray overload. This is an issue with the
standard proxy types; these can implicitly be converted to a valarray.
The solution seems to be adding valarray overloads for all functions that
take a custom proxy.
Fixes: https://github.com/llvm/llvm-project/issues/21320
---
libcxx/include/valarray | 13 ++++
.../valarray.cassign/and_valarray.pass.cpp | 76 ++++++++++++++-----
2 files changed, 72 insertions(+), 17 deletions(-)
diff --git a/libcxx/include/valarray b/libcxx/include/valarray
index fb618684952128..221b7cc2eff691 100644
--- a/libcxx/include/valarray
+++ b/libcxx/include/valarray
@@ -819,6 +819,19 @@ public:
_LIBCPP_HIDE_FROM_ABI valarray& operator<<=(const value_type& __x);
_LIBCPP_HIDE_FROM_ABI valarray& operator>>=(const value_type& __x);
+ // The valarray overloads are needed for the types with an implicit
+ // conversion to valarray. For example slice_array.
+ _LIBCPP_HIDE_FROM_ABI valarray& operator*=(const valarray& __v) { return operator*= <valarray>(__v); }
+ _LIBCPP_HIDE_FROM_ABI valarray& operator/=(const valarray& __v) { return operator/= <valarray>(__v); }
+ _LIBCPP_HIDE_FROM_ABI valarray& operator%=(const valarray& __v) { return operator%= <valarray>(__v); }
+ _LIBCPP_HIDE_FROM_ABI valarray& operator+=(const valarray& __v) { return operator+= <valarray>(__v); }
+ _LIBCPP_HIDE_FROM_ABI valarray& operator-=(const valarray& __v) { return operator-= <valarray>(__v); }
+ _LIBCPP_HIDE_FROM_ABI valarray& operator^=(const valarray& __v) { return operator^= <valarray>(__v); }
+ _LIBCPP_HIDE_FROM_ABI valarray& operator&=(const valarray& __v) { return operator&= <valarray>(__v); }
+ _LIBCPP_HIDE_FROM_ABI valarray& operator|=(const valarray& __v) { return operator|= <valarray>(__v); }
+ _LIBCPP_HIDE_FROM_ABI valarray& operator<<=(const valarray& __v) { return operator<<= <valarray>(__v); }
+ _LIBCPP_HIDE_FROM_ABI valarray& operator>>=(const valarray& __v) { return operator>>= <valarray>(__v); }
+
template <class _Expr, __enable_if_t<__is_val_expr<_Expr>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI valarray& operator*=(const _Expr& __v);
diff --git a/libcxx/test/std/numerics/numarray/template.valarray/valarray.cassign/and_valarray.pass.cpp b/libcxx/test/std/numerics/numarray/template.valarray/valarray.cassign/and_valarray.pass.cpp
index 5824d5344bfd8d..e5514f361c41a3 100644
--- a/libcxx/test/std/numerics/numarray/template.valarray/valarray.cassign/and_valarray.pass.cpp
+++ b/libcxx/test/std/numerics/numarray/template.valarray/valarray.cassign/and_valarray.pass.cpp
@@ -12,29 +12,71 @@
// valarray& operator&=(const valarray& v);
+// [valarray.syn]/3
+// Any function returning a valarray<T> is permitted to return an object of
+// another type, provided all the const member functions of valarray<T> are
+// also applicable to this type.
+//
+// Libc++ uses this and returns __val_expr<_Expr> for several operations.
+//
+// The const overloads of
+// valarray::operator[](...) const
+// return propxy objects. These proxies are implicitly convertable to
+// std::valarray.
+//
+// Validate the function works for valarray, the proxies, and __val_expr.
+
#include <valarray>
#include <cassert>
#include <cstddef>
#include "test_macros.h"
-int main(int, char**)
-{
- {
- typedef int T;
- T a1[] = {1, 2, 3, 4, 5};
- T a2[] = {6, 7, 8, 9, 10};
- T a3[] = {0, 2, 0, 0, 0};
- const unsigned N = sizeof(a1)/sizeof(a1[0]);
- std::valarray<T> v1(a1, N);
- std::valarray<T> v2(a2, N);
- std::valarray<T> v3(a3, N);
- v1 &= v2;
- assert(v1.size() == v2.size());
- assert(v1.size() == v3.size());
- for (std::size_t i = 0; i < v1.size(); ++i)
- assert(v1[i] == v3[i]);
- }
+template <class A>
+void test(const A& rhs) {
+ int input[] = {1, 2, 3, 4, 5};
+ int expected[] = {0, 2, 0, 0, 0};
+ const unsigned N = sizeof(input) / sizeof(input[0]);
+ std::valarray<int> value(input, N);
+
+ value &= rhs;
+
+ assert(value.size() == N);
+ for (std::size_t i = 0; i < value.size(); ++i)
+ assert(value[i] == expected[i]);
+}
+
+int main(int, char**) {
+ int input[] = {6, 7, 8, 9, 10};
+ const unsigned N = sizeof(input) / sizeof(input[0]);
+
+ std::valarray<bool> mask(true, N);
+ std::size_t indices[] = {0, 1, 2, 3, 4};
+ std::valarray<std::size_t> indirect(indices, N);
+
+ std::valarray<int> zero(0, N);
+
+ {
+ std::valarray<int> value(input, N);
+
+ test(value);
+ test(value[std::slice(0, N, 1)]);
+ test(value[std::gslice(0, std::valarray<std::size_t>(N, 1), std::valarray<std::size_t>(1, 1))]);
+ test(value[mask]);
+ test(value[indirect]);
+ test(value + zero);
+ }
+
+ {
+ const std::valarray<int> value(input, N);
+
+ test(value);
+ test(value[std::slice(0, N, 1)]);
+ test(value[std::gslice(0, std::valarray<std::size_t>(N, 1), std::valarray<std::size_t>(1, 1))]);
+ test(value[mask]);
+ test(value[indirect]);
+ test(value + zero);
+ }
return 0;
}
>From 28f9aa7b371cecd64615118c06730d93e561e80d Mon Sep 17 00:00:00 2001
From: Mark de Wever <koraq at xs4all.nl>
Date: Wed, 10 Jan 2024 17:17:47 +0100
Subject: [PATCH 2/2] Use a different approach.
This was discussed during the review with @ldionne.
Note still one function adapted for to evaluate this design.
---
libcxx/include/valarray | 98 ++++++++++++++++---
.../class.gslice.array/assert.get.pass.cpp | 49 ++++++++++
.../numarray/class.gslice.array/get.pass.cpp | 52 ++++++++++
.../class.indirect.array/assert.get.pass.cpp | 49 ++++++++++
.../class.indirect.array/get.pass.cpp | 44 +++++++++
.../class.mask.array/assert.get.pass.cpp | 48 +++++++++
.../numarray/class.mask.array/get.pass.cpp | 51 ++++++++++
.../class.slice.array/assert.get.pass.cpp | 49 ++++++++++
.../numarray/class.slice.array/get.pass.cpp | 46 +++++++++
9 files changed, 472 insertions(+), 14 deletions(-)
create mode 100644 libcxx/test/libcxx/numerics/numarray/class.gslice.array/assert.get.pass.cpp
create mode 100644 libcxx/test/libcxx/numerics/numarray/class.gslice.array/get.pass.cpp
create mode 100644 libcxx/test/libcxx/numerics/numarray/class.indirect.array/assert.get.pass.cpp
create mode 100644 libcxx/test/libcxx/numerics/numarray/class.indirect.array/get.pass.cpp
create mode 100644 libcxx/test/libcxx/numerics/numarray/class.mask.array/assert.get.pass.cpp
create mode 100644 libcxx/test/libcxx/numerics/numarray/class.mask.array/get.pass.cpp
create mode 100644 libcxx/test/libcxx/numerics/numarray/class.slice.array/assert.get.pass.cpp
create mode 100644 libcxx/test/libcxx/numerics/numarray/class.slice.array/get.pass.cpp
diff --git a/libcxx/include/valarray b/libcxx/include/valarray
index 221b7cc2eff691..692f06bae86cdc 100644
--- a/libcxx/include/valarray
+++ b/libcxx/include/valarray
@@ -733,6 +733,50 @@ struct __is_val_expr<__val_expr<_ValExpr> > : true_type {};
template <class _Tp>
struct __is_val_expr<valarray<_Tp> > : true_type {};
+template <class _Tp>
+struct __is_val_expr<slice_array<_Tp> > : true_type {};
+
+template <class _Tp>
+struct __is_val_expr<gslice_array<_Tp> > : true_type {};
+
+template <class _Tp>
+struct __is_val_expr<mask_array<_Tp> > : true_type {};
+
+template <class _Tp>
+struct __is_val_expr<indirect_array<_Tp> > : true_type {};
+
+// The functions using a __val_expr access the elements by their index.
+// valarray and the libc++ lazy proxies have an operator[]. The
+// Standard proxy array's don't have this operator, instead they have a
+// implementation specific accessor
+// __get(size_t)
+//
+// The functions use the non-member function
+// __get(__val_expr, size_t)
+//
+// If the __val_expr is a specialization of __index_use_member_get it
+// uses the __val_expr's member function
+// __get(size_t)
+// else it uses the __val_expr's member function
+// operator[](size_t)
+template <class _ValExpr>
+struct __index_use_member_get;
+
+template <class>
+struct __index_use_member_get : false_type {};
+
+template <class _Tp>
+struct __index_use_member_get<slice_array<_Tp> > : true_type {};
+
+template <class _Tp>
+struct __index_use_member_get<gslice_array<_Tp> > : true_type {};
+
+template <class _Tp>
+struct __index_use_member_get<mask_array<_Tp> > : true_type {};
+
+template <class _Tp>
+struct __index_use_member_get<indirect_array<_Tp> > : true_type {};
+
template <class _Tp>
class _LIBCPP_TEMPLATE_VIS valarray {
public:
@@ -819,19 +863,6 @@ public:
_LIBCPP_HIDE_FROM_ABI valarray& operator<<=(const value_type& __x);
_LIBCPP_HIDE_FROM_ABI valarray& operator>>=(const value_type& __x);
- // The valarray overloads are needed for the types with an implicit
- // conversion to valarray. For example slice_array.
- _LIBCPP_HIDE_FROM_ABI valarray& operator*=(const valarray& __v) { return operator*= <valarray>(__v); }
- _LIBCPP_HIDE_FROM_ABI valarray& operator/=(const valarray& __v) { return operator/= <valarray>(__v); }
- _LIBCPP_HIDE_FROM_ABI valarray& operator%=(const valarray& __v) { return operator%= <valarray>(__v); }
- _LIBCPP_HIDE_FROM_ABI valarray& operator+=(const valarray& __v) { return operator+= <valarray>(__v); }
- _LIBCPP_HIDE_FROM_ABI valarray& operator-=(const valarray& __v) { return operator-= <valarray>(__v); }
- _LIBCPP_HIDE_FROM_ABI valarray& operator^=(const valarray& __v) { return operator^= <valarray>(__v); }
- _LIBCPP_HIDE_FROM_ABI valarray& operator&=(const valarray& __v) { return operator&= <valarray>(__v); }
- _LIBCPP_HIDE_FROM_ABI valarray& operator|=(const valarray& __v) { return operator|= <valarray>(__v); }
- _LIBCPP_HIDE_FROM_ABI valarray& operator<<=(const valarray& __v) { return operator<<= <valarray>(__v); }
- _LIBCPP_HIDE_FROM_ABI valarray& operator>>=(const valarray& __v) { return operator>>= <valarray>(__v); }
-
template <class _Expr, __enable_if_t<__is_val_expr<_Expr>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI valarray& operator*=(const _Expr& __v);
@@ -916,6 +947,21 @@ template <class _Tp, size_t _Size>
valarray(const _Tp (&)[_Size], size_t) -> valarray<_Tp>;
#endif
+template <class _Expr, __enable_if_t<__is_val_expr<_Expr>::value, int> = 0>
+_LIBCPP_HIDE_FROM_ABI typename _Expr::value_type __get(const _Expr& __v, size_t __i, true_type) {
+ return __v.__get(__i);
+}
+
+template <class _Expr, __enable_if_t<__is_val_expr<_Expr>::value, int> = 0>
+_LIBCPP_HIDE_FROM_ABI typename _Expr::value_type __get(const _Expr& __v, size_t __i, false_type) {
+ return __v[__i];
+}
+
+template <class _Expr, __enable_if_t<__is_val_expr<_Expr>::value, int> = 0>
+_LIBCPP_HIDE_FROM_ABI typename _Expr::value_type __get(const _Expr& __v, size_t __i) {
+ return std::__get(__v, __i, __index_use_member_get<_Expr>()) ;
+}
+
extern template _LIBCPP_EXPORTED_FROM_ABI void valarray<size_t>::resize(size_t, size_t);
template <class _Op, class _Tp>
@@ -1038,6 +1084,12 @@ public:
_LIBCPP_HIDE_FROM_ABI void operator=(const valarray<value_type>& __va) const;
+ // Behaves like __val_expr::operator[], which returns by value.
+ _LIBCPP_HIDE_FROM_ABI value_type __get(size_t __i) const {
+ _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__i < __size_, "slice_array.__get() index out of bounds");
+ return __vp_[__i * __stride_];
+ }
+
private:
_LIBCPP_HIDE_FROM_ABI slice_array(const slice& __sl, const valarray<value_type>& __v)
: __vp_(const_cast<value_type*>(__v.__begin_ + __sl.start())), __size_(__sl.size()), __stride_(__sl.stride()) {}
@@ -1259,6 +1311,12 @@ public:
gslice_array(const gslice_array&) = default;
+ // Behaves like __val_expr::operator[], which returns by value.
+ _LIBCPP_HIDE_FROM_ABI value_type __get(size_t __i) const {
+ _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__i < __1d_.size(), "gslice_array.__get() index out of bounds");
+ return __vp_[__1d_[__i]];
+ }
+
private:
gslice_array(const gslice& __gs, const valarray<value_type>& __v)
: __vp_(const_cast<value_type*>(__v.__begin_)), __1d_(__gs.__1d_) {}
@@ -1438,6 +1496,12 @@ public:
_LIBCPP_HIDE_FROM_ABI void operator=(const value_type& __x) const;
+ // Behaves like __val_expr::operator[], which returns by value.
+ _LIBCPP_HIDE_FROM_ABI value_type __get(size_t __i) const {
+ _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__i < __1d_.size(), "mask_array.__get() index out of bounds");
+ return __vp_[__1d_[__i]];
+ }
+
private:
_LIBCPP_HIDE_FROM_ABI mask_array(const valarray<bool>& __vb, const valarray<value_type>& __v)
: __vp_(const_cast<value_type*>(__v.__begin_)),
@@ -1637,6 +1701,12 @@ public:
_LIBCPP_HIDE_FROM_ABI void operator=(const value_type& __x) const;
+ // Behaves like __val_expr::operator[], which returns by value.
+ _LIBCPP_HIDE_FROM_ABI value_type __get(size_t __i) const {
+ _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__i < __1d_.size(), "indirect_array.__get() index out of bounds");
+ return __vp_[__1d_[__i]];
+ }
+
private:
_LIBCPP_HIDE_FROM_ABI indirect_array(const valarray<size_t>& __ia, const valarray<value_type>& __v)
: __vp_(const_cast<value_type*>(__v.__begin_)), __1d_(__ia) {}
@@ -2431,7 +2501,7 @@ template <class _Expr, __enable_if_t<__is_val_expr<_Expr>::value, int> >
inline valarray<_Tp>& valarray<_Tp>::operator&=(const _Expr& __v) {
size_t __i = 0;
for (value_type* __t = __begin_; __t != __end_; ++__t, ++__i)
- *__t &= __v[__i];
+ *__t &= std::__get(__v, __i);
return *this;
}
diff --git a/libcxx/test/libcxx/numerics/numarray/class.gslice.array/assert.get.pass.cpp b/libcxx/test/libcxx/numerics/numarray/class.gslice.array/assert.get.pass.cpp
new file mode 100644
index 00000000000000..5cd21ddc18a6c5
--- /dev/null
+++ b/libcxx/test/libcxx/numerics/numarray/class.gslice.array/assert.get.pass.cpp
@@ -0,0 +1,49 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: has-unix-headers
+// UNSUPPORTED: c++03
+// UNSUPPORTED: libcpp-hardening-mode=none
+// XFAIL: availability-verbose_abort-missing
+
+// <valarray>
+
+// template<class T> class gslice_array;
+
+// T __get(size_t i); // where i is out of bounds
+
+#include <valarray>
+
+#include "check_assertion.h"
+
+int main(int, char**) {
+ unsigned input[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
+ const unsigned N = sizeof(input) / sizeof(input[0]);
+
+ std::valarray<unsigned> array(input, N);
+
+ {
+ std::gslice_array<unsigned> result =
+ array[std::gslice(0, std::valarray<std::size_t>(N, 1), std::valarray<std::size_t>(1, 1))];
+ TEST_LIBCPP_ASSERT_FAILURE(result.__get(N), "gslice_array.__get() index out of bounds");
+ }
+ {
+ std::valarray<std::size_t> sizes(2);
+ sizes[0] = 2;
+ sizes[1] = 3;
+
+ std::valarray<std::size_t> strides(2);
+ strides[0] = 6;
+ strides[1] = 1;
+
+ std::gslice_array<unsigned> result = array[std::gslice(1, sizes, strides)];
+ TEST_LIBCPP_ASSERT_FAILURE(result.__get(6), "gslice_array.__get() index out of bounds");
+ }
+
+ return 0;
+}
diff --git a/libcxx/test/libcxx/numerics/numarray/class.gslice.array/get.pass.cpp b/libcxx/test/libcxx/numerics/numarray/class.gslice.array/get.pass.cpp
new file mode 100644
index 00000000000000..2a1842f69fd2d9
--- /dev/null
+++ b/libcxx/test/libcxx/numerics/numarray/class.gslice.array/get.pass.cpp
@@ -0,0 +1,52 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// <valarray>
+
+// template<class T> class gslice_array;
+
+// T __get(size_t i);
+
+#include <valarray>
+#include <cassert>
+
+#include "test_macros.h"
+
+int main(int, char**) {
+ unsigned input[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
+ const unsigned N = sizeof(input) / sizeof(input[0]);
+
+ std::valarray<unsigned> array(input, N);
+
+ {
+ std::gslice_array<unsigned> result =
+ array[std::gslice(0, std::valarray<std::size_t>(N, 1), std::valarray<std::size_t>(1, 1))];
+ for (unsigned i = 0; i < N; ++i)
+ assert(result.__get(i) == i);
+ }
+
+ {
+ std::valarray<std::size_t> sizes(2);
+ sizes[0] = 2;
+ sizes[1] = 3;
+
+ std::valarray<std::size_t> strides(2);
+ strides[0] = 6;
+ strides[1] = 1;
+
+ std::gslice_array<unsigned> result = array[std::gslice(1, sizes, strides)];
+ assert(result.__get(0) == input[1 + 0 * 6 + 0 * 1]);
+ assert(result.__get(1) == input[1 + 0 * 6 + 1 * 1]);
+ assert(result.__get(2) == input[1 + 0 * 6 + 2 * 1]);
+
+ assert(result.__get(3) == input[1 + 1 * 6 + 0 * 1]);
+ assert(result.__get(4) == input[1 + 1 * 6 + 1 * 1]);
+ assert(result.__get(5) == input[1 + 1 * 6 + 2 * 1]);
+ }
+ return 0;
+}
diff --git a/libcxx/test/libcxx/numerics/numarray/class.indirect.array/assert.get.pass.cpp b/libcxx/test/libcxx/numerics/numarray/class.indirect.array/assert.get.pass.cpp
new file mode 100644
index 00000000000000..8a53ede1a130c0
--- /dev/null
+++ b/libcxx/test/libcxx/numerics/numarray/class.indirect.array/assert.get.pass.cpp
@@ -0,0 +1,49 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: has-unix-headers
+// UNSUPPORTED: c++03
+// UNSUPPORTED: libcpp-hardening-mode=none
+// XFAIL: availability-verbose_abort-missing
+
+// <valarray>
+
+// template<class T> class indirect_array;
+
+// T __get(size_t i); // where i is out of bounds
+
+#include <valarray>
+
+#include "check_assertion.h"
+
+int main(int, char**) {
+ unsigned input[] = {0, 1, 2, 3, 4};
+ const unsigned N = sizeof(input) / sizeof(input[0]);
+
+ std::valarray<unsigned> array(input, N);
+
+ {
+ std::indirect_array<unsigned> result = array[std::valarray<std::size_t>()];
+ TEST_LIBCPP_ASSERT_FAILURE(result.__get(0), "indirect_array.__get() index out of bounds");
+ }
+ {
+ std::indirect_array<unsigned> result = array[std::valarray<std::size_t>(std::size_t(0), std::size_t(N))];
+ TEST_LIBCPP_ASSERT_FAILURE(result.__get(N), "indirect_array.__get() index out of bounds");
+ }
+
+ {
+ std::valarray<std::size_t> indirect(std::size_t(0), std::size_t(3));
+ std::indirect_array<unsigned> result = array[indirect];
+ indirect[0] = 4;
+ indirect[1] = 1;
+ indirect[2] = 3;
+ TEST_LIBCPP_ASSERT_FAILURE(result.__get(3), "indirect_array.__get() index out of bounds");
+ }
+
+ return 0;
+}
diff --git a/libcxx/test/libcxx/numerics/numarray/class.indirect.array/get.pass.cpp b/libcxx/test/libcxx/numerics/numarray/class.indirect.array/get.pass.cpp
new file mode 100644
index 00000000000000..3c50c740bcaba3
--- /dev/null
+++ b/libcxx/test/libcxx/numerics/numarray/class.indirect.array/get.pass.cpp
@@ -0,0 +1,44 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// <valarray>
+
+// template<class T> class indirect_array;
+
+// T __get(size_t i);
+
+#include <valarray>
+#include <cassert>
+
+#include "test_macros.h"
+
+int main(int, char**) {
+ unsigned input[] = {0, 1, 2, 3, 4};
+ const unsigned N = sizeof(input) / sizeof(input[0]);
+
+ std::valarray<unsigned> array(input, N);
+
+ {
+ std::indirect_array<unsigned> result = array[std::valarray<std::size_t>(std::size_t(0), std::size_t(N))];
+ for (unsigned i = 0; i < N; ++i)
+ assert(result.__get(i) == 0);
+ }
+
+ {
+ std::valarray<std::size_t> indirect(std::size_t(0), std::size_t(3));
+ indirect[0] = 4;
+ indirect[1] = 1;
+ indirect[2] = 3;
+ std::indirect_array<unsigned> result = array[indirect];
+ assert(result.__get(0) == 4);
+ assert(result.__get(1) == 1);
+ assert(result.__get(2) == 3);
+ }
+
+ return 0;
+}
diff --git a/libcxx/test/libcxx/numerics/numarray/class.mask.array/assert.get.pass.cpp b/libcxx/test/libcxx/numerics/numarray/class.mask.array/assert.get.pass.cpp
new file mode 100644
index 00000000000000..12740f3a3e3ad6
--- /dev/null
+++ b/libcxx/test/libcxx/numerics/numarray/class.mask.array/assert.get.pass.cpp
@@ -0,0 +1,48 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: has-unix-headers
+// UNSUPPORTED: c++03
+// UNSUPPORTED: libcpp-hardening-mode=none
+// XFAIL: availability-verbose_abort-missing
+
+// <valarray>
+
+// template<class T> class mask_array;
+
+// T __get(size_t i); // where i is out of bounds
+
+#include <valarray>
+
+#include "check_assertion.h"
+
+int main(int, char**) {
+ unsigned input[] = {0, 1, 2, 3, 4};
+ const unsigned N = sizeof(input) / sizeof(input[0]);
+
+ std::valarray<unsigned> array(input, N);
+
+ {
+ std::mask_array<unsigned> result = array[std::valarray<bool>(false, N)];
+ TEST_LIBCPP_ASSERT_FAILURE(result.__get(0), "mask_array.__get() index out of bounds");
+ }
+ {
+ std::mask_array<unsigned> result = array[std::valarray<bool>(true, N)];
+ TEST_LIBCPP_ASSERT_FAILURE(result.__get(N), "mask_array.__get() index out of bounds");
+ }
+
+ {
+ std::valarray<bool> mask(false, N);
+ mask[1] = true;
+ mask[3] = true;
+ std::mask_array<unsigned> result = array[mask];
+ TEST_LIBCPP_ASSERT_FAILURE(result.__get(2), "mask_array.__get() index out of bounds");
+ }
+
+ return 0;
+}
diff --git a/libcxx/test/libcxx/numerics/numarray/class.mask.array/get.pass.cpp b/libcxx/test/libcxx/numerics/numarray/class.mask.array/get.pass.cpp
new file mode 100644
index 00000000000000..e34c38289222a2
--- /dev/null
+++ b/libcxx/test/libcxx/numerics/numarray/class.mask.array/get.pass.cpp
@@ -0,0 +1,51 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// <valarray>
+
+// template<class T> class mask_array;
+
+// T __get(size_t i);
+
+#include <valarray>
+#include <cassert>
+
+#include "test_macros.h"
+
+int main(int, char**) {
+ unsigned input[] = {0, 1, 2, 3, 4};
+ const unsigned N = sizeof(input) / sizeof(input[0]);
+
+ std::valarray<unsigned> array(input, N);
+
+ {
+ std::mask_array<unsigned> result = array[std::valarray<bool>(true, N)];
+ for (unsigned i = 0; i < N; ++i)
+ assert(result.__get(i) == i);
+ }
+
+ {
+ std::valarray<bool> mask(false, N);
+ mask[1] = true;
+ mask[3] = true;
+ std::mask_array<unsigned> result = array[mask];
+ assert(result.__get(0) == 1);
+ assert(result.__get(1) == 3);
+ }
+
+ {
+ std::valarray<bool> mask(false, N);
+ mask[0] = true;
+ mask[4] = true;
+ std::mask_array<unsigned> result = array[mask];
+ assert(result.__get(0) == 0);
+ assert(result.__get(1) == 4);
+ }
+
+ return 0;
+}
diff --git a/libcxx/test/libcxx/numerics/numarray/class.slice.array/assert.get.pass.cpp b/libcxx/test/libcxx/numerics/numarray/class.slice.array/assert.get.pass.cpp
new file mode 100644
index 00000000000000..dab6b2b5174c83
--- /dev/null
+++ b/libcxx/test/libcxx/numerics/numarray/class.slice.array/assert.get.pass.cpp
@@ -0,0 +1,49 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: has-unix-headers
+// UNSUPPORTED: c++03
+// UNSUPPORTED: libcpp-hardening-mode=none
+// XFAIL: availability-verbose_abort-missing
+
+// <valarray>
+
+// template<class T> class slice_array;
+
+// T __get(size_t i); // where i is out of bounds
+
+#include <valarray>
+
+#include "check_assertion.h"
+
+int main(int, char**) {
+ unsigned input[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
+ const unsigned N = sizeof(input) / sizeof(input[0]);
+
+ std::valarray<unsigned> array(input, N);
+
+ {
+ std::slice_array<unsigned> result = array[std::slice(0, 0, 0)];
+ TEST_LIBCPP_ASSERT_FAILURE(result.__get(0), "slice_array.__get() index out of bounds");
+ }
+ {
+ std::slice_array<unsigned> result = array[std::slice(0, N, 1)];
+ TEST_LIBCPP_ASSERT_FAILURE(result.__get(N), "slice_array.__get() index out of bounds");
+ }
+ {
+ std::slice_array<unsigned> result = array[std::slice(3, 2, 2)];
+ TEST_LIBCPP_ASSERT_FAILURE(result.__get(2), "slice_array.__get() index out of bounds");
+ }
+
+ {
+ std::slice_array<unsigned> result = array[std::slice(1, 3, 4)];
+ TEST_LIBCPP_ASSERT_FAILURE(result.__get(3), "slice_array.__get() index out of bounds");
+ }
+
+ return 0;
+}
diff --git a/libcxx/test/libcxx/numerics/numarray/class.slice.array/get.pass.cpp b/libcxx/test/libcxx/numerics/numarray/class.slice.array/get.pass.cpp
new file mode 100644
index 00000000000000..26871f310bae24
--- /dev/null
+++ b/libcxx/test/libcxx/numerics/numarray/class.slice.array/get.pass.cpp
@@ -0,0 +1,46 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// <valarray>
+
+// template<class T> class slice_array;
+
+// T __get(size_t i);
+
+#include <valarray>
+#include <cassert>
+
+#include "test_macros.h"
+
+int main(int, char**) {
+ unsigned input[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
+ const unsigned N = sizeof(input) / sizeof(input[0]);
+
+ std::valarray<unsigned> array(input, N);
+
+ {
+ std::slice_array<unsigned> result = array[std::slice(0, N, 1)];
+ for (unsigned i = 0; i < N; ++i)
+ assert(result.__get(i) == i);
+ }
+
+ {
+ std::slice_array<unsigned> result = array[std::slice(3, 2, 2)];
+ assert(result.__get(0) == 3);
+ assert(result.__get(1) == 5);
+ }
+
+ {
+ std::slice_array<unsigned> result = array[std::slice(1, 3, 4)];
+ assert(result.__get(0) == 1);
+ assert(result.__get(1) == 5);
+ assert(result.__get(2) == 9);
+ }
+
+ return 0;
+}
More information about the libcxx-commits
mailing list