at.pass.cpp 2.8 KB

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