at_tests.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
  4. //
  5. // This code is licensed under the MIT License (MIT).
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  8. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  9. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  10. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  11. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  12. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  13. // THE SOFTWARE.
  14. //
  15. ///////////////////////////////////////////////////////////////////////////////
  16. #include <gtest/gtest.h>
  17. #include <gsl/util> // for at
  18. #include <array> // for array
  19. #include <cstddef> // for size_t
  20. #include <exception> // for terminate
  21. #include <initializer_list> // for initializer_list
  22. #include <vector> // for vector
  23. #if defined(__cplusplus) && __cplusplus >= 202002L
  24. #include <span>
  25. #endif // __cplusplus >= 202002L
  26. #include "deathTestCommon.h"
  27. TEST(at_tests, static_array)
  28. {
  29. int a[4] = {1, 2, 3, 4};
  30. const int(&c_a)[4] = a;
  31. for (int i = 0; i < 4; ++i)
  32. {
  33. EXPECT_TRUE(&gsl::at(a, i) == &a[i]);
  34. EXPECT_TRUE(&gsl::at(c_a, i) == &a[i]);
  35. }
  36. const auto terminateHandler = std::set_terminate([] {
  37. std::cerr << "Expected Death. static_array";
  38. std::abort();
  39. });
  40. const auto expected = GetExpectedDeathString(terminateHandler);
  41. EXPECT_DEATH(gsl::at(a, -1), expected);
  42. EXPECT_DEATH(gsl::at(a, 4), expected);
  43. EXPECT_DEATH(gsl::at(c_a, -1), expected);
  44. EXPECT_DEATH(gsl::at(c_a, 4), expected);
  45. }
  46. TEST(at_tests, std_array)
  47. {
  48. std::array<int, 4> a = {1, 2, 3, 4};
  49. const std::array<int, 4>& c_a = a;
  50. for (int i = 0; i < 4; ++i)
  51. {
  52. EXPECT_TRUE(&gsl::at(a, i) == &a[static_cast<std::size_t>(i)]);
  53. EXPECT_TRUE(&gsl::at(c_a, i) == &a[static_cast<std::size_t>(i)]);
  54. }
  55. const auto terminateHandler = std::set_terminate([] {
  56. std::cerr << "Expected Death. std_array";
  57. std::abort();
  58. });
  59. const auto expected = GetExpectedDeathString(terminateHandler);
  60. EXPECT_DEATH(gsl::at(a, -1), expected);
  61. EXPECT_DEATH(gsl::at(a, 4), expected);
  62. EXPECT_DEATH(gsl::at(c_a, -1), expected);
  63. EXPECT_DEATH(gsl::at(c_a, 4), expected);
  64. }
  65. TEST(at_tests, std_vector)
  66. {
  67. std::vector<int> a = {1, 2, 3, 4};
  68. const std::vector<int>& c_a = a;
  69. for (int i = 0; i < 4; ++i)
  70. {
  71. EXPECT_TRUE(&gsl::at(a, i) == &a[static_cast<std::size_t>(i)]);
  72. EXPECT_TRUE(&gsl::at(c_a, i) == &a[static_cast<std::size_t>(i)]);
  73. }
  74. const auto terminateHandler = std::set_terminate([] {
  75. std::cerr << "Expected Death. std_vector";
  76. std::abort();
  77. });
  78. const auto expected = GetExpectedDeathString(terminateHandler);
  79. EXPECT_DEATH(gsl::at(a, -1), expected);
  80. EXPECT_DEATH(gsl::at(a, 4), expected);
  81. EXPECT_DEATH(gsl::at(c_a, -1), expected);
  82. EXPECT_DEATH(gsl::at(c_a, 4), expected);
  83. }
  84. TEST(at_tests, InitializerList)
  85. {
  86. const std::initializer_list<int> a = {1, 2, 3, 4};
  87. for (int i = 0; i < 4; ++i)
  88. {
  89. EXPECT_TRUE(gsl::at(a, i) == i + 1);
  90. EXPECT_TRUE(gsl::at({1, 2, 3, 4}, i) == i + 1);
  91. }
  92. const auto terminateHandler = std::set_terminate([] {
  93. std::cerr << "Expected Death. InitializerList";
  94. std::abort();
  95. });
  96. const auto expected = GetExpectedDeathString(terminateHandler);
  97. EXPECT_DEATH(gsl::at(a, -1), expected);
  98. EXPECT_DEATH(gsl::at(a, 4), expected);
  99. EXPECT_DEATH(gsl::at({1, 2, 3, 4}, -1), expected);
  100. EXPECT_DEATH(gsl::at({1, 2, 3, 4}, 4), expected);
  101. }
  102. #if defined(FORCE_STD_SPAN_TESTS) || defined(__cpp_lib_span) && __cpp_lib_span >= 202002L
  103. TEST(at_tests, std_span)
  104. {
  105. std::vector<int> vec{1, 2, 3, 4, 5};
  106. std::span sp{vec};
  107. std::vector<int> cvec{1, 2, 3, 4, 5};
  108. std::span csp{cvec};
  109. for (gsl::index i = 0; i < gsl::narrow_cast<gsl::index>(vec.size()); ++i)
  110. {
  111. EXPECT_TRUE(&gsl::at(sp, i) == &vec[gsl::narrow_cast<size_t>(i)]);
  112. EXPECT_TRUE(&gsl::at(csp, i) == &cvec[gsl::narrow_cast<size_t>(i)]);
  113. }
  114. const auto terminateHandler = std::set_terminate([] {
  115. std::cerr << "Expected Death. std_span";
  116. std::abort();
  117. });
  118. const auto expected = GetExpectedDeathString(terminateHandler);
  119. EXPECT_DEATH(gsl::at(sp, -1), expected);
  120. EXPECT_DEATH(gsl::at(sp, gsl::narrow_cast<gsl::index>(sp.size())), expected);
  121. EXPECT_DEATH(gsl::at(csp, -1), expected);
  122. EXPECT_DEATH(gsl::at(csp, gsl::narrow_cast<gsl::index>(sp.size())), expected);
  123. }
  124. #endif // defined(FORCE_STD_SPAN_TESTS) || defined(__cpp_lib_span) && __cpp_lib_span >= 202002L
  125. #if !defined(_MSC_VER) || defined(__clang__) || _MSC_VER >= 1910
  126. static constexpr bool test_constexpr()
  127. {
  128. int a1[4] = {1, 2, 3, 4};
  129. const int(&c_a1)[4] = a1;
  130. std::array<int, 4> a2 = {1, 2, 3, 4};
  131. const std::array<int, 4>& c_a2 = a2;
  132. for (int i = 0; i < 4; ++i)
  133. {
  134. if (&gsl::at(a1, i) != &a1[i]) return false;
  135. if (&gsl::at(c_a1, i) != &a1[i]) return false;
  136. // requires C++17:
  137. // if (&gsl::at(a2, i) != &a2[static_cast<std::size_t>(i)]) return false;
  138. if (&gsl::at(c_a2, i) != &c_a2[static_cast<std::size_t>(i)]) return false;
  139. if (gsl::at({1, 2, 3, 4}, i) != i + 1) return false;
  140. }
  141. return true;
  142. }
  143. static_assert(test_constexpr(), "FAIL");
  144. #endif