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 using size_type = decltype(array)::size_type; 77 TEST_IGNORE_NODISCARD array.at(static_cast<size_type>(-1)); 78 assert(false); 79 } catch (std::out_of_range const&) { 80 // pass 81 } catch (...) { 82 assert(false); 83 } 84 } 85 86 { 87 std::array<int, 0> array = {}; 88 89 try { 90 TEST_IGNORE_NODISCARD array.at(0); 91 assert(false); 92 } catch (std::out_of_range const&) { 93 // pass 94 } catch (...) { 95 assert(false); 96 } 97 } 98 #endif 99 } 100 101 int main(int, char**) 102 { 103 tests(); 104 test_exceptions(); 105 106 #if TEST_STD_VER >= 14 107 static_assert(tests(), ""); 108 #endif 109 return 0; 110 } 111