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 // const_reference at (size_type) const; // constexpr in C++14 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_CXX14 bool tests() 28 { 29 { 30 typedef double T; 31 typedef std::array<T, 3> C; 32 C const c = {1, 2, 3.5}; 33 typename C::const_reference r1 = c.at(0); 34 assert(r1 == 1); 35 36 typename C::const_reference r2 = c.at(2); 37 assert(r2 == 3.5); 38 } 39 return true; 40 } 41 42 void test_exceptions() 43 { 44 #ifndef TEST_HAS_NO_EXCEPTIONS 45 { 46 std::array<int, 4> const array = {1, 2, 3, 4}; 47 48 try { 49 TEST_IGNORE_NODISCARD array.at(4); 50 assert(false); 51 } catch (std::out_of_range const&) { 52 // pass 53 } catch (...) { 54 assert(false); 55 } 56 57 try { 58 TEST_IGNORE_NODISCARD array.at(5); 59 assert(false); 60 } catch (std::out_of_range const&) { 61 // pass 62 } catch (...) { 63 assert(false); 64 } 65 66 try { 67 TEST_IGNORE_NODISCARD array.at(6); 68 assert(false); 69 } catch (std::out_of_range const&) { 70 // pass 71 } catch (...) { 72 assert(false); 73 } 74 75 try { 76 TEST_IGNORE_NODISCARD array.at(-1); 77 assert(false); 78 } catch (std::out_of_range const&) { 79 // pass 80 } catch (...) { 81 assert(false); 82 } 83 } 84 85 { 86 std::array<int, 0> array = {}; 87 88 try { 89 TEST_IGNORE_NODISCARD array.at(0); 90 assert(false); 91 } catch (std::out_of_range const&) { 92 // pass 93 } catch (...) { 94 assert(false); 95 } 96 } 97 #endif 98 } 99 100 int main(int, char**) 101 { 102 tests(); 103 test_exceptions(); 104 105 #if TEST_STD_VER >= 14 106 static_assert(tests(), ""); 107 #endif 108 return 0; 109 } 110