[libcxx-commits] [libcxx] [libc++] Fixes valarray proxy type compound assignment operations. (PR #76528)

via libcxx-commits libcxx-commits at lists.llvm.org
Mon Jan 22 10:29:36 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Mark de Wever (mordante)

<details>
<summary>Changes</summary>

The valarray<>::operator[](...) const functions return proxy objects. The valarray<>::operator[](...) functions return valarray objects.

However the standard allows functions returning valarray objects to return custom proxy objects instead. Libc++ returns __val_expr proxies. Functions taking a valarray object must work with the custom proxies too. 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 is to allow the standard proxies to behave as-if they are custom proxies.

This patch fixes the valarray compound assignments. Other operations, like the binary non-member functions are not fixed. These will be done in a followup patch.

Fixes: https://github.com/llvm/llvm-project/issues/21320

---

Patch is 50.66 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/76528.diff


19 Files Affected:

- (modified) libcxx/include/valarray (+90-10) 
- (added) libcxx/test/libcxx/numerics/numarray/class.gslice.array/assert.get.pass.cpp (+49) 
- (added) libcxx/test/libcxx/numerics/numarray/class.gslice.array/get.pass.cpp (+52) 
- (added) libcxx/test/libcxx/numerics/numarray/class.indirect.array/assert.get.pass.cpp (+49) 
- (added) libcxx/test/libcxx/numerics/numarray/class.indirect.array/get.pass.cpp (+44) 
- (added) libcxx/test/libcxx/numerics/numarray/class.mask.array/assert.get.pass.cpp (+48) 
- (added) libcxx/test/libcxx/numerics/numarray/class.mask.array/get.pass.cpp (+51) 
- (added) libcxx/test/libcxx/numerics/numarray/class.slice.array/assert.get.pass.cpp (+49) 
- (added) libcxx/test/libcxx/numerics/numarray/class.slice.array/get.pass.cpp (+46) 
- (modified) libcxx/test/std/numerics/numarray/template.valarray/valarray.cassign/and_valarray.pass.cpp (+59-17) 
- (modified) libcxx/test/std/numerics/numarray/template.valarray/valarray.cassign/divide_valarray.pass.cpp (+59-17) 
- (modified) libcxx/test/std/numerics/numarray/template.valarray/valarray.cassign/minus_valarray.pass.cpp (+59-17) 
- (modified) libcxx/test/std/numerics/numarray/template.valarray/valarray.cassign/modulo_valarray.pass.cpp (+59-17) 
- (modified) libcxx/test/std/numerics/numarray/template.valarray/valarray.cassign/or_valarray.pass.cpp (+59-17) 
- (modified) libcxx/test/std/numerics/numarray/template.valarray/valarray.cassign/plus_valarray.pass.cpp (+59-17) 
- (modified) libcxx/test/std/numerics/numarray/template.valarray/valarray.cassign/shift_left_valarray.pass.cpp (+59-17) 
- (modified) libcxx/test/std/numerics/numarray/template.valarray/valarray.cassign/shift_right_valarray.pass.cpp (+57-17) 
- (modified) libcxx/test/std/numerics/numarray/template.valarray/valarray.cassign/times_valarray.pass.cpp (+57-17) 
- (modified) libcxx/test/std/numerics/numarray/template.valarray/valarray.cassign/xor_valarray.pass.cpp (+57-17) 


``````````diff
diff --git a/libcxx/include/valarray b/libcxx/include/valarray
index fb6186849521288..44adcd71ec6167f 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 __val_expr_use_member_functions 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 __val_expr_use_member_functions;
+
+template <class>
+struct __val_expr_use_member_functions : false_type {};
+
+template <class _Tp>
+struct __val_expr_use_member_functions<slice_array<_Tp> > : true_type {};
+
+template <class _Tp>
+struct __val_expr_use_member_functions<gslice_array<_Tp> > : true_type {};
+
+template <class _Tp>
+struct __val_expr_use_member_functions<mask_array<_Tp> > : true_type {};
+
+template <class _Tp>
+struct __val_expr_use_member_functions<indirect_array<_Tp> > : true_type {};
+
 template <class _Tp>
 class _LIBCPP_TEMPLATE_VIS valarray {
 public:
@@ -903,6 +947,18 @@ 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 && __val_expr_use_member_functions<_Expr>::value, int> = 0>
+_LIBCPP_HIDE_FROM_ABI typename _Expr::value_type __get(const _Expr& __v, size_t __i) {
+  return __v.__get(__i);
+}
+
+template <class _Expr,
+          __enable_if_t<__is_val_expr<_Expr>::value && !__val_expr_use_member_functions<_Expr>::value, int> = 0>
+_LIBCPP_HIDE_FROM_ABI typename _Expr::value_type __get(const _Expr& __v, size_t __i) {
+  return __v[__i];
+}
+
 extern template _LIBCPP_EXPORTED_FROM_ABI void valarray<size_t>::resize(size_t, size_t);
 
 template <class _Op, class _Tp>
@@ -1025,6 +1081,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()) {}
@@ -1246,6 +1308,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_) {}
@@ -1425,6 +1493,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_)),
@@ -1624,6 +1698,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) {}
@@ -2355,7 +2435,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;
 }
 
@@ -2364,7 +2444,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;
 }
 
@@ -2373,7 +2453,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;
 }
 
@@ -2382,7 +2462,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;
 }
 
@@ -2391,7 +2471,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;
 }
 
@@ -2400,7 +2480,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;
 }
 
@@ -2409,7 +2489,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;
 }
 
@@ -2418,7 +2498,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;
 }
 
@@ -2427,7 +2507,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;
 }
 
@@ -2436,7 +2516,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 000000000000000..5cd21ddc18a6c52
--- /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 000000000000000..2a1842f69fd2d9f
--- /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 000000000000000..8a53ede1a130c01
--- /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 000000000000000..3c50c740bcaba3d
--- /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 000000000000000..12740f3a3e3ad6a
--- /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 000000000000000..e34c38289222a2d
--- /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 000000000000000..dab6b2b5174c83e
--- /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_arra...
[truncated]

``````````

</details>


https://github.com/llvm/llvm-project/pull/76528


More information about the libcxx-commits mailing list