xref: /llvm-project/libcxx/test/std/containers/views/mdspan/extents/ctor_from_integral.pass.cpp (revision cfa096d9c92e758c4916dfd06d107fcec2edeca4)
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 integral:
13 //
14 // template<class ... OtherIndexTypes>
15 //     constexpr explicit extents(OtherIndexTypes ... exts) noexcept;
16 //
17 // Let N be sizeof...(OtherIndexTypes), and let
18 // exts_arr be array<index_type, N>{static_cast<index_type>(std::move(exts))...}.
19 //
20 // Constraints:
21 //   * (is_convertible_v<OtherIndexTypes, index_type> && ...) is true,
22 //   * (is_nothrow_constructible_v<index_type, OtherIndexType> && ...) is true, and
23 //   * N == rank_dynamic() || N == rank() is true.
24 //
25 // Preconditions:
26 //   * If N != rank_dynamic() is true, exts_arr[r] equals Er for each r for which
27 //     Er is a static extent, and
28 //   * either
29 //     - sizeof...(exts) == 0 is true, or
30 //     - each element of exts is nonnegative and is representable as a value of type index_type.
31 //
32 
33 #include <mdspan>
34 #include <cassert>
35 #include <type_traits>
36 
37 #include "../ConvertibleToIntegral.h"
38 #include "CtorTestCombinations.h"
39 #include "test_macros.h"
40 
41 struct IntegralCtorTest {
42   template <class E, class AllExtents, class Extents, size_t... Indices>
test_constructionIntegralCtorTest43   static constexpr void test_construction(AllExtents all_ext, Extents ext, std::index_sequence<Indices...>) {
44     // construction from indices
45     ASSERT_NOEXCEPT(E(ext[Indices]...));
46     test_runtime_observers(E(ext[Indices]...), all_ext);
47   }
48 };
49 
main(int,char **)50 int main(int, char**) {
51   test_index_type_combo<IntegralCtorTest>();
52   static_assert(test_index_type_combo<IntegralCtorTest>());
53 
54   constexpr size_t D = std::dynamic_extent;
55   using E            = std::extents<int, 1, D, 3, D>;
56 
57   // check can't construct from too few arguments
58   static_assert(!std::is_constructible_v<E, int>, "extents constructible from illegal arguments");
59   // check can't construct from rank_dynamic < #args < rank
60   static_assert(!std::is_constructible_v<E, int, int, int>, "extents constructible from illegal arguments");
61   // check can't construct from too many arguments
62   static_assert(!std::is_constructible_v<E, int, int, int, int, int>, "extents constructible from illegal arguments");
63 
64   // test construction fails from types not convertible to index_type but convertible to other integer types
65   static_assert(std::is_convertible_v<IntType, int>, "Test helper IntType unexpectedly not convertible to int");
66   static_assert(!std::is_constructible_v< std::extents<unsigned long, D>, IntType>,
67                 "extents constructible from illegal arguments");
68   return 0;
69 }
70