algorithm_tests.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 <array> // for array
  17. #include <cstddef> // for size_t
  18. #include <gsl/algorithm> // for copy
  19. #include <gsl/span> // for span
  20. #include <gtest/gtest.h>
  21. #include "deathTestCommon.h"
  22. namespace gsl
  23. {
  24. struct fail_fast;
  25. } // namespace gsl
  26. using namespace gsl;
  27. TEST(algorithm_tests, same_type)
  28. {
  29. // dynamic source and destination span
  30. {
  31. std::array<int, 5> src{1, 2, 3, 4, 5};
  32. std::array<int, 10> dst{};
  33. const span<int> src_span(src);
  34. const span<int> dst_span(dst);
  35. copy(src_span, dst_span);
  36. copy(src_span, dst_span.subspan(src_span.size()));
  37. for (std::size_t i = 0; i < src.size(); ++i)
  38. {
  39. EXPECT_TRUE(dst[i] == src[i]);
  40. EXPECT_TRUE(dst[i + src.size()] == src[i]);
  41. }
  42. }
  43. // static source and dynamic destination span
  44. {
  45. std::array<int, 5> src{1, 2, 3, 4, 5};
  46. std::array<int, 10> dst{};
  47. const span<int, 5> src_span(src);
  48. const span<int> dst_span(dst);
  49. copy(src_span, dst_span);
  50. copy(src_span, dst_span.subspan(src_span.size()));
  51. for (std::size_t i = 0; i < src.size(); ++i)
  52. {
  53. EXPECT_TRUE(dst[i] == src[i]);
  54. EXPECT_TRUE(dst[i + src.size()] == src[i]);
  55. }
  56. }
  57. // dynamic source and static destination span
  58. {
  59. std::array<int, 5> src{1, 2, 3, 4, 5};
  60. std::array<int, 10> dst{};
  61. const gsl::span<int> src_span(src);
  62. const gsl::span<int, 10> dst_span(dst);
  63. copy(src_span, dst_span);
  64. copy(src_span, dst_span.subspan(src_span.size()));
  65. for (std::size_t i = 0; i < src.size(); ++i)
  66. {
  67. EXPECT_TRUE(dst[i] == src[i]);
  68. EXPECT_TRUE(dst[i + src.size()] == src[i]);
  69. }
  70. }
  71. // static source and destination span
  72. {
  73. std::array<int, 5> src{1, 2, 3, 4, 5};
  74. std::array<int, 10> dst{};
  75. const span<int, 5> src_span(src);
  76. const span<int, 10> dst_span(dst);
  77. copy(src_span, dst_span);
  78. copy(src_span, dst_span.subspan(src_span.size()));
  79. for (std::size_t i = 0; i < src.size(); ++i)
  80. {
  81. EXPECT_TRUE(dst[i] == src[i]);
  82. EXPECT_TRUE(dst[i + src.size()] == src[i]);
  83. }
  84. }
  85. }
  86. TEST(algorithm_tests, compatible_type)
  87. {
  88. // dynamic source and destination span
  89. {
  90. std::array<short, 5> src{1, 2, 3, 4, 5};
  91. std::array<int, 10> dst{};
  92. const span<short> src_span(src);
  93. const span<int> dst_span(dst);
  94. copy(src_span, dst_span);
  95. copy(src_span, dst_span.subspan(src_span.size()));
  96. for (std::size_t i = 0; i < src.size(); ++i)
  97. {
  98. EXPECT_TRUE(dst[i] == src[i]);
  99. EXPECT_TRUE(dst[i + src.size()] == src[i]);
  100. }
  101. }
  102. // static source and dynamic destination span
  103. {
  104. std::array<short, 5> src{1, 2, 3, 4, 5};
  105. std::array<int, 10> dst{};
  106. const span<short, 5> src_span(src);
  107. const span<int> dst_span(dst);
  108. copy(src_span, dst_span);
  109. copy(src_span, dst_span.subspan(src_span.size()));
  110. for (std::size_t i = 0; i < src.size(); ++i)
  111. {
  112. EXPECT_TRUE(dst[i] == src[i]);
  113. EXPECT_TRUE(dst[i + src.size()] == src[i]);
  114. }
  115. }
  116. // dynamic source and static destination span
  117. {
  118. std::array<short, 5> src{1, 2, 3, 4, 5};
  119. std::array<int, 10> dst{};
  120. const span<short> src_span(src);
  121. const span<int, 10> dst_span(dst);
  122. copy(src_span, dst_span);
  123. copy(src_span, dst_span.subspan(src_span.size()));
  124. for (std::size_t i = 0; i < src.size(); ++i)
  125. {
  126. EXPECT_TRUE(dst[i] == src[i]);
  127. EXPECT_TRUE(dst[i + src.size()] == src[i]);
  128. }
  129. }
  130. // static source and destination span
  131. {
  132. std::array<short, 5> src{1, 2, 3, 4, 5};
  133. std::array<int, 10> dst{};
  134. const span<short, 5> src_span(src);
  135. const span<int, 10> dst_span(dst);
  136. copy(src_span, dst_span);
  137. copy(src_span, dst_span.subspan(src_span.size()));
  138. for (std::size_t i = 0; i < src.size(); ++i)
  139. {
  140. EXPECT_TRUE(dst[i] == src[i]);
  141. EXPECT_TRUE(dst[i + src.size()] == src[i]);
  142. }
  143. }
  144. }
  145. #ifdef CONFIRM_COMPILATION_ERRORS
  146. TEST(algorithm_tests, incompatible_type)
  147. {
  148. std::array<int, 4> src{1, 2, 3, 4};
  149. std::array<int*, 12> dst{};
  150. span<int> src_span_dyn(src);
  151. span<int, 4> src_span_static(src);
  152. span<int*> dst_span_dyn(dst);
  153. span<int*, 4> dst_span_static(dst);
  154. // every line should produce a compilation error
  155. copy(src_span_dyn, dst_span_dyn);
  156. copy(src_span_dyn, dst_span_static);
  157. copy(src_span_static, dst_span_dyn);
  158. copy(src_span_static, dst_span_static);
  159. }
  160. #endif
  161. TEST(algorithm_tests, small_destination_span)
  162. {
  163. const auto terminateHandler = std::set_terminate([] {
  164. std::cerr << "Expected Death. small_destination_span";
  165. std::abort();
  166. });
  167. const auto expected = GetExpectedDeathString(terminateHandler);
  168. std::array<int, 12> src{1, 2, 3, 4};
  169. std::array<int, 4> dst{};
  170. const span<int> src_span_dyn(src);
  171. const span<int, 12> src_span_static(src);
  172. const span<int> dst_span_dyn(dst);
  173. const span<int, 4> dst_span_static(dst);
  174. EXPECT_DEATH(copy(src_span_dyn, dst_span_dyn), expected);
  175. EXPECT_DEATH(copy(src_span_dyn, dst_span_static), expected);
  176. EXPECT_DEATH(copy(src_span_static, dst_span_dyn), expected);
  177. #ifdef CONFIRM_COMPILATION_ERRORS
  178. copy(src_span_static, dst_span_static);
  179. #endif
  180. }