[libcxx-commits] [libcxx] a281bdd - [libc++] [test] Improve the tests for std::{begin, end}(valarray).
Arthur O'Dwyer via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Mar 1 11:25:46 PST 2022
Author: Arthur O'Dwyer
Date: 2022-03-01T14:24:46-05:00
New Revision: a281bdd51bdb486300b7ff7144d9a4ee56177a15
URL: https://github.com/llvm/llvm-project/commit/a281bdd51bdb486300b7ff7144d9a4ee56177a15
DIFF: https://github.com/llvm/llvm-project/commit/a281bdd51bdb486300b7ff7144d9a4ee56177a15.diff
LOG: [libc++] [test] Improve the tests for std::{begin,end}(valarray).
Incidentally, this removes some unqualified ADL calls to `begin` and `end`.
Differential Revision: https://reviews.llvm.org/D119687
Added:
libcxx/test/std/numerics/numarray/valarray.range/begin-end.pass.cpp
Modified:
Removed:
libcxx/test/std/numerics/numarray/valarray.range/begin_const.pass.cpp
libcxx/test/std/numerics/numarray/valarray.range/begin_non_const.pass.cpp
libcxx/test/std/numerics/numarray/valarray.range/end_const.pass.cpp
libcxx/test/std/numerics/numarray/valarray.range/end_non_const.pass.cpp
################################################################################
diff --git a/libcxx/test/std/numerics/numarray/valarray.range/begin-end.pass.cpp b/libcxx/test/std/numerics/numarray/valarray.range/begin-end.pass.cpp
new file mode 100644
index 0000000000000..d5ec017e04a86
--- /dev/null
+++ b/libcxx/test/std/numerics/numarray/valarray.range/begin-end.pass.cpp
@@ -0,0 +1,69 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 valarray;
+
+// template <class T> unspecified begin(valarray<T>& v);
+// template <class T> unspecified begin(const valarray<T>& v);
+// template <class T> unspecified end(valarray<T>& v);
+// template <class T> unspecified end(const valarray<T>& v);
+
+#include <valarray>
+#include <cassert>
+#include <iterator>
+#include <type_traits>
+
+#include "test_macros.h"
+
+int main(int, char**)
+{
+ {
+ int a[] = {1, 2, 3, 4, 5};
+ std::valarray<int> v(a, 5);
+ const std::valarray<int>& cv = v;
+ using It = decltype(std::begin(v));
+ using CIt = decltype(std::begin(cv));
+ static_assert(std::is_base_of<std::random_access_iterator_tag, std::iterator_traits<It>::iterator_category>::value, "");
+ static_assert(std::is_base_of<std::random_access_iterator_tag, std::iterator_traits<CIt>::iterator_category>::value, "");
+ ASSERT_SAME_TYPE(decltype(*std::begin(v)), int&);
+ ASSERT_SAME_TYPE(decltype(*std::begin(cv)), const int&);
+ assert(&*std::begin(v) == &v[0]);
+ assert(&*std::begin(cv) == &cv[0]);
+ *std::begin(v) = 10;
+ assert(v[0] == 10);
+
+ ASSERT_SAME_TYPE(decltype(std::end(v)), It);
+ ASSERT_SAME_TYPE(decltype(std::end(cv)), CIt);
+ assert(&*std::prev(std::end(v)) == &v[4]);
+ assert(&*std::prev(std::end(cv)) == &cv[4]);
+ }
+#if TEST_STD_VER >= 11
+ {
+ int a[] = {1, 2, 3, 4, 5};
+ std::valarray<int> v(a, 5);
+ int sum = 0;
+ for (int& i : v) {
+ sum += i;
+ }
+ assert(sum == 15);
+ }
+ {
+ int a[] = {1, 2, 3, 4, 5};
+ const std::valarray<int> cv(a, 5);
+ int sum = 0;
+ for (const int& i : cv) {
+ sum += i;
+ }
+ assert(sum == 15);
+ }
+#endif
+
+ return 0;
+}
diff --git a/libcxx/test/std/numerics/numarray/valarray.range/begin_const.pass.cpp b/libcxx/test/std/numerics/numarray/valarray.range/begin_const.pass.cpp
deleted file mode 100644
index fcf55ce74ff88..0000000000000
--- a/libcxx/test/std/numerics/numarray/valarray.range/begin_const.pass.cpp
+++ /dev/null
@@ -1,33 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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 valarray;
-
-// template <class T>
-// unspecified1
-// begin(const valarray<T>& v);
-
-#include <valarray>
-#include <cassert>
-
-#include "test_macros.h"
-
-int main(int, char**)
-{
- {
- typedef int T;
- T a[] = {1, 2, 3, 4, 5};
- const unsigned N = sizeof(a)/sizeof(a[0]);
- const std::valarray<T> v(a, N);
- assert(v[0] == 1);
- }
-
- return 0;
-}
diff --git a/libcxx/test/std/numerics/numarray/valarray.range/begin_non_const.pass.cpp b/libcxx/test/std/numerics/numarray/valarray.range/begin_non_const.pass.cpp
deleted file mode 100644
index dc9cb028bf42e..0000000000000
--- a/libcxx/test/std/numerics/numarray/valarray.range/begin_non_const.pass.cpp
+++ /dev/null
@@ -1,34 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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 valarray;
-
-// template <class T>
-// unspecified1
-// begin(valarray<T>& v);
-
-#include <valarray>
-#include <cassert>
-
-#include "test_macros.h"
-
-int main(int, char**)
-{
- {
- typedef int T;
- T a[] = {1, 2, 3, 4, 5};
- const unsigned N = sizeof(a)/sizeof(a[0]);
- std::valarray<T> v(a, N);
- *begin(v) = 10;
- assert(v[0] == 10);
- }
-
- return 0;
-}
diff --git a/libcxx/test/std/numerics/numarray/valarray.range/end_const.pass.cpp b/libcxx/test/std/numerics/numarray/valarray.range/end_const.pass.cpp
deleted file mode 100644
index a7422daf29ce9..0000000000000
--- a/libcxx/test/std/numerics/numarray/valarray.range/end_const.pass.cpp
+++ /dev/null
@@ -1,35 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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 valarray;
-
-// template <class T>
-// unspecified1
-// end(const valarray<T>& v);
-
-#include <valarray>
-#include <cassert>
-#include <cstddef>
-
-#include "test_macros.h"
-
-int main(int, char**)
-{
- {
- typedef int T;
- T a[] = {1, 2, 3, 4, 5};
- const unsigned N = sizeof(a)/sizeof(a[0]);
- const std::valarray<T> v(a, N);
- assert(v[v.size()-1] == 5);
- assert(static_cast<std::size_t>(end(v) - begin(v)) == v.size());
- }
-
- return 0;
-}
diff --git a/libcxx/test/std/numerics/numarray/valarray.range/end_non_const.pass.cpp b/libcxx/test/std/numerics/numarray/valarray.range/end_non_const.pass.cpp
deleted file mode 100644
index 73a066942ecb8..0000000000000
--- a/libcxx/test/std/numerics/numarray/valarray.range/end_non_const.pass.cpp
+++ /dev/null
@@ -1,36 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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 valarray;
-
-// template <class T>
-// unspecified1
-// end(valarray<T>& v);
-
-#include <valarray>
-#include <cassert>
-#include <cstddef>
-
-#include "test_macros.h"
-
-int main(int, char**)
-{
- {
- typedef int T;
- T a[] = {1, 2, 3, 4, 5};
- const unsigned N = sizeof(a)/sizeof(a[0]);
- std::valarray<T> v(a, N);
- *(end(v) - 1) = 10;
- assert(v[v.size()-1] == 10);
- assert(static_cast<std::size_t>(end(v) - begin(v)) == v.size());
- }
-
- return 0;
-}
More information about the libcxx-commits
mailing list