xref: /llvm-project/libcxx/test/std/containers/sequences/array/front_back.pass.cpp (revision 9fb3669429a8bc59622c26ab6f5cf6926ee97e7d)
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 
11 // reference front();  // constexpr in C++17
12 // reference back();   // constexpr in C++17
13 
14 #include <array>
15 #include <cassert>
16 
17 #include "test_macros.h"
18 
tests()19 TEST_CONSTEXPR_CXX17 bool tests()
20 {
21     {
22         typedef double T;
23         typedef std::array<T, 3> C;
24         C c = {1, 2, 3.5};
25 
26         C::reference r1 = c.front();
27         assert(r1 == 1);
28         r1 = 5.5;
29         assert(c[0] == 5.5);
30 
31         C::reference r2 = c.back();
32         assert(r2 == 3.5);
33         r2 = 7.5;
34         assert(c[2] == 7.5);
35     }
36     {
37         typedef double T;
38         typedef std::array<T, 0> C;
39         C c = {};
40         ASSERT_SAME_TYPE(decltype(c.back()), C::reference);
41         LIBCPP_ASSERT_NOEXCEPT(c.back());
42         ASSERT_SAME_TYPE(decltype(c.front()), C::reference);
43         LIBCPP_ASSERT_NOEXCEPT(c.front());
44         if (c.size() > (0)) { // always false
45             TEST_IGNORE_NODISCARD c.front();
46             TEST_IGNORE_NODISCARD c.back();
47         }
48     }
49     {
50         typedef double T;
51         typedef std::array<const T, 0> C;
52         C c = {};
53         ASSERT_SAME_TYPE(decltype( c.back()), C::reference);
54         LIBCPP_ASSERT_NOEXCEPT(    c.back());
55         ASSERT_SAME_TYPE(decltype( c.front()), C::reference);
56         LIBCPP_ASSERT_NOEXCEPT(    c.front());
57         if (c.size() > (0)) {
58             TEST_IGNORE_NODISCARD c.front();
59             TEST_IGNORE_NODISCARD c.back();
60         }
61     }
62 
63     return true;
64 }
65 
main(int,char **)66 int main(int, char**)
67 {
68     tests();
69 #if TEST_STD_VER >= 17
70     static_assert(tests(), "");
71 #endif
72     return 0;
73 }
74