xref: /llvm-project/libcxx/test/std/containers/views/mdspan/extents/ctor_from_array.pass.cpp (revision f5832bab6f5024cabe32a9f668b7f44e6b7cfef5)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
9 
10 // <mdspan>
11 
12 // Test construction from array:
13 //
14 // template<class OtherIndexType, size_t N>
15 //     constexpr explicit(N != rank_dynamic()) extents(const array<OtherIndexType, N>& exts) noexcept;
16 //
17 // Constraints:
18 //   * is_convertible_v<const OtherIndexType&, index_type> is true,
19 //   * is_nothrow_constructible_v<index_type, const OtherIndexType&> is true, and
20 //   * N == rank_dynamic() || N == rank() is true.
21 //
22 // Preconditions:
23 //   * If N != rank_dynamic() is true, exts[r] equals Er for each r for which
24 //     Er is a static extent, and
25 //   * either
26 //     - N is zero, or
27 //     - exts[r] is nonnegative and is representable as a value of type index_type
28 //       for every rank index r.
29 //
30 
31 #include <mdspan>
32 #include <cassert>
33 #include <array>
34 #include <type_traits>
35 
36 #include "../ConvertibleToIntegral.h"
37 #include "CtorTestCombinations.h"
38 #include "test_macros.h"
39 
40 struct ArrayCtorTest {
41   template <class E, class T, size_t N, class Extents, size_t... Indices>
test_constructionArrayCtorTest42   static constexpr void test_construction(std::array<T, N> all_ext, Extents ext, std::index_sequence<Indices...>) {
43     ASSERT_NOEXCEPT(E(ext));
44     if constexpr (N == E::rank_dynamic()) {
45       test_implicit_construction_call<E>(ext, all_ext);
46     }
47     test_runtime_observers(E(ext), all_ext);
48   }
49 };
50 
51 template <class E>
52 struct implicit_construction {
53   bool value;
implicit_constructionimplicit_construction54   implicit_construction(E) : value(true) {}
55   template <class T>
implicit_constructionimplicit_construction56   implicit_construction(T) : value(false) {}
57 };
58 
main(int,char **)59 int main(int, char**) {
60   test_index_type_combo<ArrayCtorTest>();
61   static_assert(test_index_type_combo<ArrayCtorTest>());
62 
63   constexpr size_t D = std::dynamic_extent;
64   using E            = std::extents<int, 1, D, 3, D>;
65 
66   // check can't construct from too few arguments
67   static_assert(!std::is_constructible_v<E, std::array<int, 1>>, "extents constructible from illegal arguments");
68   // check can't construct from rank_dynamic < #args < rank
69   static_assert(!std::is_constructible_v<E, std::array<int, 3>>, "extents constructible from illegal arguments");
70   // check can't construct from too many arguments
71   static_assert(!std::is_constructible_v<E, std::array<int, 5>>, "extents constructible from illegal arguments");
72 
73   // test implicit construction fails from span and array if all extents are given
74   std::array a5{3, 4, 5, 6, 7};
75   // check that explicit construction works, i.e. no error
76   static_assert(std::is_constructible_v< std::extents<int, D, D, 5, D, D>, decltype(a5)>,
77                 "extents unexpectedly not constructible");
78   // check that implicit construction doesn't work
79   assert((implicit_construction<std::extents<int, D, D, 5, D, D>>(a5).value == false));
80 
81   // test construction fails from types not convertible to index_type but convertible to other integer types
82   static_assert(std::is_convertible_v<IntType, int>, "Test helper IntType unexpectedly not convertible to int");
83   static_assert(!std::is_constructible_v< std::extents<unsigned long, D>, std::array<IntType, 1>>,
84                 "extents constructible from illegal arguments");
85 
86   // index_type is not nothrow constructible
87   static_assert(std::is_convertible_v<IntType, unsigned char>);
88   static_assert(std::is_convertible_v<const IntType&, unsigned char>);
89   static_assert(!std::is_nothrow_constructible_v<unsigned char, const IntType&>);
90   static_assert(!std::is_constructible_v<std::dextents<unsigned char, 2>, std::array<IntType, 2>>);
91 
92   // convertible from non-const to index_type but not  from const
93   static_assert(std::is_convertible_v<IntTypeNC, int>);
94   static_assert(!std::is_convertible_v<const IntTypeNC&, int>);
95   static_assert(std::is_nothrow_constructible_v<int, IntTypeNC>);
96   static_assert(!std::is_constructible_v<std::dextents<int, 2>, std::array<IntTypeNC, 2>>);
97   return 0;
98 }
99