[libcxx-commits] [libcxx] 6a6a80e - [libc++] [test] Add tests for std::span construction from initializer lists.

Arthur O'Dwyer via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jan 4 11:25:44 PST 2022


Author: Arthur O'Dwyer
Date: 2022-01-04T14:25:32-05:00
New Revision: 6a6a80e88eff1fde4e9c6c38c1efd45eff5c9f9f

URL: https://github.com/llvm/llvm-project/commit/6a6a80e88eff1fde4e9c6c38c1efd45eff5c9f9f
DIFF: https://github.com/llvm/llvm-project/commit/6a6a80e88eff1fde4e9c6c38c1efd45eff5c9f9f.diff

LOG: [libc++] [test] Add tests for std::span construction from initializer lists.

Differential Revision: https://reviews.llvm.org/D116481

Added: 
    libcxx/test/std/containers/views/span.cons/initializer_list.pass.cpp

Modified: 
    libcxx/test/std/containers/views/span.cons/array.pass.cpp
    libcxx/test/std/containers/views/span.cons/iterator_len.verify.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/test/std/containers/views/span.cons/array.pass.cpp b/libcxx/test/std/containers/views/span.cons/array.pass.cpp
index cbb60853385d8..fdee1f4ec8a76 100644
--- a/libcxx/test/std/containers/views/span.cons/array.pass.cpp
+++ b/libcxx/test/std/containers/views/span.cons/array.pass.cpp
@@ -74,50 +74,48 @@ void checkCV()
     }
 }
 
-
-template <typename T>
-constexpr bool testConstexprSpan()
+template<class T>
+constexpr bool testSpan()
 {
-    constexpr T val[2] = {};
+    T val[2] = {};
 
-    ASSERT_NOEXCEPT(std::span<const T>   {val});
+    ASSERT_NOEXCEPT(std::span<T>{val});
+    ASSERT_NOEXCEPT(std::span<T, 2>{val});
+    ASSERT_NOEXCEPT(std::span<const T>{val});
     ASSERT_NOEXCEPT(std::span<const T, 2>{val});
-    std::span<const T>    s1{val};
-    std::span<const T, 2> s2{val};
-    return
-        s1.data() == &val[0] && s1.size() == 2
-    &&  s2.data() == &val[0] && s2.size() == 2;
-}
-
 
-template <typename T>
-void testRuntimeSpan()
-{
-    T val[2] = {};
-    ASSERT_NOEXCEPT(std::span<T>   {val});
-    ASSERT_NOEXCEPT(std::span<T, 2>{val});
-    std::span<T>    s1{val};
-    std::span<T, 2> s2{val};
-    assert(s1.data() == &val[0] && s1.size() == 2);
-    assert(s2.data() == &val[0] && s2.size() == 2);
+    std::span<T> s1 = val;
+    std::span<T, 2> s2 = val;
+    std::span<const T> s3 = val;
+    std::span<const T, 2> s4 = val;
+    assert(s1.data() == val && s1.size() == 2);
+    assert(s2.data() == val && s2.size() == 2);
+    assert(s3.data() == val && s3.size() == 2);
+    assert(s4.data() == val && s4.size() == 2);
+
+    std::span<const int> s5 = {{1,2}};
+    std::span<const int, 2> s6 = {{1,2}};
+    assert(s5.size() == 2);  // and it dangles
+    assert(s6.size() == 2);  // and it dangles
+
+    return true;
 }
 
-struct A{};
+
+struct A {};
 
 int main(int, char**)
 {
-    static_assert(testConstexprSpan<int>(),    "");
-    static_assert(testConstexprSpan<long>(),   "");
-    static_assert(testConstexprSpan<double>(), "");
-    static_assert(testConstexprSpan<A>(),      "");
+    testSpan<int>();
+    testSpan<double>();
+    testSpan<A>();
+    testSpan<std::string>();
 
-    testRuntimeSpan<int>();
-    testRuntimeSpan<long>();
-    testRuntimeSpan<double>();
-    testRuntimeSpan<std::string>();
-    testRuntimeSpan<A>();
+    static_assert(testSpan<int>());
+    static_assert(testSpan<double>());
+    static_assert(testSpan<A>());
 
     checkCV();
 
-  return 0;
+    return 0;
 }

diff  --git a/libcxx/test/std/containers/views/span.cons/initializer_list.pass.cpp b/libcxx/test/std/containers/views/span.cons/initializer_list.pass.cpp
new file mode 100644
index 0000000000000..5b5c226e329a2
--- /dev/null
+++ b/libcxx/test/std/containers/views/span.cons/initializer_list.pass.cpp
@@ -0,0 +1,43 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+// <span>
+
+#include <span>
+#include <cassert>
+
+struct Sink {
+    constexpr Sink() = default;
+    constexpr Sink(Sink*) {}
+};
+
+constexpr int count(std::span<const Sink> sp) {
+    return sp.size();
+}
+
+template<int N>
+constexpr int countn(std::span<const Sink, N> sp) {
+    return sp.size();
+}
+
+constexpr bool test() {
+    Sink a[10];
+    assert(count({a}) == 10);
+    assert(count({a, a+10}) == 10);
+    assert(countn<10>({a}) == 10);
+    return true;
+}
+
+int main(int, char**)
+{
+    test();
+    static_assert(test());
+
+    return 0;
+}

diff  --git a/libcxx/test/std/containers/views/span.cons/iterator_len.verify.cpp b/libcxx/test/std/containers/views/span.cons/iterator_len.verify.cpp
index b2472a96ac252..1ee4cc3d494d3 100644
--- a/libcxx/test/std/containers/views/span.cons/iterator_len.verify.cpp
+++ b/libcxx/test/std/containers/views/span.cons/iterator_len.verify.cpp
@@ -31,7 +31,10 @@ int main(int, char**) {
   int arr[] = {1, 2, 3};
   createImplicitSpan<int, 1>(arr, 3);
 
-  std::span<const int> sp = {0, 0}; // expected-error {{no matching constructor for initialization of 'std::span<const int>'}}
+  std::span<int> sp = {0, 0}; // expected-error {{no matching constructor for initialization of 'std::span<int>'}}
+  std::span<int, 2> sp2 = {0, 0}; // expected-error {{no matching constructor for initialization of 'std::span<int, 2>'}}
+  std::span<const int> csp = {0, 0}; // expected-error {{no matching constructor for initialization of 'std::span<const int>'}}
+  std::span<const int, 2> csp2 = {0, 0}; // expected-error {{no matching constructor for initialization of 'std::span<const int, 2>'}}
 
   return 0;
 }


        


More information about the libcxx-commits mailing list