xref: /llvm-project/libcxx/test/std/containers/sequences/array/array.creation/to_array.pass.cpp (revision 9aea9ab83dc047b0361520df572062783b8497e3)
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 
9 // <array>
10 // UNSUPPORTED: c++03, c++11, c++14, c++17
11 
12 // template <typename T, size_t Size>
13 // constexpr auto to_array(T (&arr)[Size])
14 //    -> array<remove_cv_t<T>, Size>;
15 
16 // template <typename T, size_t Size>
17 // constexpr auto to_array(T (&&arr)[Size])
18 //    -> array<remove_cv_t<T>, Size>;
19 
20 #include <array>
21 #include <cassert>
22 
23 #include "test_macros.h"
24 #include "MoveOnly.h"
25 
tests()26 constexpr bool tests()
27 {
28   //  Test deduced type.
29   {
30     auto arr = std::to_array({1, 2, 3});
31     ASSERT_SAME_TYPE(decltype(arr), std::array<int, 3>);
32     assert(arr[0] == 1);
33     assert(arr[1] == 2);
34     assert(arr[2] == 3);
35   }
36 
37   {
38     const long l1 = 42;
39     auto arr = std::to_array({1L, 4L, 9L, l1});
40     ASSERT_SAME_TYPE(decltype(arr)::value_type, long);
41     static_assert(arr.size() == 4, "");
42     assert(arr[0] == 1);
43     assert(arr[1] == 4);
44     assert(arr[2] == 9);
45     assert(arr[3] == l1);
46   }
47 
48   {
49     auto arr = std::to_array("meow");
50     ASSERT_SAME_TYPE(decltype(arr), std::array<char, 5>);
51     assert(arr[0] == 'm');
52     assert(arr[1] == 'e');
53     assert(arr[2] == 'o');
54     assert(arr[3] == 'w');
55     assert(arr[4] == '\0');
56   }
57 
58   {
59     double source[3] = {4.0, 5.0, 6.0};
60     auto arr = std::to_array(source);
61     ASSERT_SAME_TYPE(decltype(arr), std::array<double, 3>);
62     assert(arr[0] == 4.0);
63     assert(arr[1] == 5.0);
64     assert(arr[2] == 6.0);
65   }
66 
67   {
68     double source[3] = {4.0, 5.0, 6.0};
69     auto arr = std::to_array(std::move(source));
70     ASSERT_SAME_TYPE(decltype(arr), std::array<double, 3>);
71     assert(arr[0] == 4.0);
72     assert(arr[1] == 5.0);
73     assert(arr[2] == 6.0);
74   }
75 
76   {
77     MoveOnly source[] = {MoveOnly{0}, MoveOnly{1}, MoveOnly{2}};
78 
79     auto arr = std::to_array(std::move(source));
80     ASSERT_SAME_TYPE(decltype(arr), std::array<MoveOnly, 3>);
81     for (int i = 0; i < 3; ++i)
82       assert(arr[i].get() == i && source[i].get() == 0);
83   }
84 
85 #ifndef _MSVC_STL_VERSION
86   // Test C99 compound literal.
87   {
88     auto arr = std::to_array((int[]){3, 4});
89     ASSERT_SAME_TYPE(decltype(arr), std::array<int, 2>);
90     assert(arr[0] == 3);
91     assert(arr[1] == 4);
92   }
93 #endif // ! _MSVC_STL_VERSION
94 
95   //  Test explicit type.
96   {
97     auto arr = std::to_array<long>({1, 2, 3});
98     ASSERT_SAME_TYPE(decltype(arr), std::array<long, 3>);
99     assert(arr[0] == 1);
100     assert(arr[1] == 2);
101     assert(arr[2] == 3);
102   }
103 
104   {
105     struct A {
106       int a;
107       double b;
108     };
109 
110     auto arr = std::to_array<A>({{3, .1}});
111     ASSERT_SAME_TYPE(decltype(arr), std::array<A, 1>);
112     assert(arr[0].a == 3);
113     assert(arr[0].b == .1);
114   }
115 
116   return true;
117 }
118 
main(int,char **)119 int main(int, char**)
120 {
121   tests();
122   static_assert(tests(), "");
123   return 0;
124 }
125