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 at (size_type); // constexpr in C++17 12 13 #include <array> 14 #include <cassert> 15 16 #ifndef TEST_HAS_NO_EXCEPTIONS 17 #include <stdexcept> 18 #endif 19 20 #include "test_macros.h" 21 22 // std::array is explicitly allowed to be initialized with A a = { init-list };. 23 // Disable the missing braces warning for this reason. 24 #include "disable_missing_braces_warning.h" 25 26 27 TEST_CONSTEXPR_CXX17 bool tests() 28 { 29 { 30 typedef double T; 31 typedef std::array<T, 3> C; 32 C c = {1, 2, 3.5}; 33 typename C::reference r1 = c.at(0); 34 assert(r1 == 1); 35 r1 = 5.5; 36 assert(c[0] == 5.5); 37 38 typename C::reference r2 = c.at(2); 39 assert(r2 == 3.5); 40 r2 = 7.5; 41 assert(c[2] == 7.5); 42 } 43 return true; 44 } 45 46 void test_exceptions() 47 { 48 #ifndef TEST_HAS_NO_EXCEPTIONS 49 { 50 std::array<int, 4> array = {1, 2, 3, 4}; 51 52 try { 53 TEST_IGNORE_NODISCARD array.at(4); 54 assert(false); 55 } catch (std::out_of_range const&) { 56 // pass 57 } catch (...) { 58 assert(false); 59 } 60 61 try { 62 TEST_IGNORE_NODISCARD array.at(5); 63 assert(false); 64 } catch (std::out_of_range const&) { 65 // pass 66 } catch (...) { 67 assert(false); 68 } 69 70 try { 71 TEST_IGNORE_NODISCARD array.at(6); 72 assert(false); 73 } catch (std::out_of_range const&) { 74 // pass 75 } catch (...) { 76 assert(false); 77 } 78 79 try { 80 TEST_IGNORE_NODISCARD array.at(-1); 81 assert(false); 82 } catch (std::out_of_range const&) { 83 // pass 84 } catch (...) { 85 assert(false); 86 } 87 } 88 89 { 90 std::array<int, 0> array = {}; 91 92 try { 93 TEST_IGNORE_NODISCARD array.at(0); 94 assert(false); 95 } catch (std::out_of_range const&) { 96 // pass 97 } catch (...) { 98 assert(false); 99 } 100 } 101 #endif 102 } 103 104 int main(int, char**) 105 { 106 tests(); 107 test_exceptions(); 108 109 #if TEST_STD_VER >= 17 110 static_assert(tests(), ""); 111 #endif 112 return 0; 113 } 114