107 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// <array>10 11// reference at (size_type); // constexpr in C++1712 13#include <array>14#include <cassert>15 16#ifndef TEST_HAS_NO_EXCEPTIONS17# include <stdexcept>18#endif19 20#include "test_macros.h"21 22TEST_CONSTEXPR_CXX17 bool tests() {23 {24 typedef double T;25 typedef std::array<T, 3> C;26 C c = {1, 2, 3.5};27 typename C::reference r1 = c.at(0);28 assert(r1 == 1);29 r1 = 5.5;30 assert(c[0] == 5.5);31 32 typename C::reference r2 = c.at(2);33 assert(r2 == 3.5);34 r2 = 7.5;35 assert(c[2] == 7.5);36 }37 return true;38}39 40void test_exceptions() {41#ifndef TEST_HAS_NO_EXCEPTIONS42 {43 std::array<int, 4> array = {1, 2, 3, 4};44 45 try {46 TEST_IGNORE_NODISCARD array.at(4);47 assert(false);48 } catch (std::out_of_range const&) {49 // pass50 } catch (...) {51 assert(false);52 }53 54 try {55 TEST_IGNORE_NODISCARD array.at(5);56 assert(false);57 } catch (std::out_of_range const&) {58 // pass59 } catch (...) {60 assert(false);61 }62 63 try {64 TEST_IGNORE_NODISCARD array.at(6);65 assert(false);66 } catch (std::out_of_range const&) {67 // pass68 } catch (...) {69 assert(false);70 }71 72 try {73 using size_type = decltype(array)::size_type;74 TEST_IGNORE_NODISCARD array.at(static_cast<size_type>(-1));75 assert(false);76 } catch (std::out_of_range const&) {77 // pass78 } catch (...) {79 assert(false);80 }81 }82 83 {84 std::array<int, 0> array = {};85 86 try {87 TEST_IGNORE_NODISCARD array.at(0);88 assert(false);89 } catch (std::out_of_range const&) {90 // pass91 } catch (...) {92 assert(false);93 }94 }95#endif96}97 98int main(int, char**) {99 tests();100 test_exceptions();101 102#if TEST_STD_VER >= 17103 static_assert(tests(), "");104#endif105 return 0;106}107