indexing.pass.cpp 3.4 KB

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