span_ext_tests.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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/span> // for span and span_ext
  18. #include <gsl/util> // for narrow_cast, at
  19. #include <array> // for array
  20. #include <exception> // for terminate
  21. #include <iostream> // for cerr
  22. #include <vector> // for vector
  23. using namespace std;
  24. using namespace gsl;
  25. #include "deathTestCommon.h"
  26. TEST(span_ext_test, make_span_from_pointer_length_constructor)
  27. {
  28. const auto terminateHandler = std::set_terminate([] {
  29. std::cerr << "Expected Death. from_pointer_length_constructor";
  30. std::abort();
  31. });
  32. const auto expected = GetExpectedDeathString(terminateHandler);
  33. int arr[4] = {1, 2, 3, 4};
  34. {
  35. auto s = make_span(&arr[0], 2);
  36. EXPECT_TRUE(s.size() == 2);
  37. EXPECT_TRUE(s.data() == &arr[0]);
  38. EXPECT_TRUE(s[0] == 1);
  39. EXPECT_TRUE(s[1] == 2);
  40. }
  41. {
  42. int* p = nullptr;
  43. auto s = make_span(p, narrow_cast<gsl::span<int>::size_type>(0));
  44. EXPECT_TRUE(s.size() == 0);
  45. EXPECT_TRUE(s.data() == nullptr);
  46. }
  47. {
  48. int* p = nullptr;
  49. auto workaround_macro = [=]() { make_span(p, 2); };
  50. EXPECT_DEATH(workaround_macro(), expected);
  51. }
  52. }
  53. TEST(span_ext_test, make_span_from_pointer_pointer_construction)
  54. {
  55. int arr[4] = {1, 2, 3, 4};
  56. {
  57. auto s = make_span(&arr[0], &arr[2]);
  58. EXPECT_TRUE(s.size() == 2);
  59. EXPECT_TRUE(s.data() == &arr[0]);
  60. EXPECT_TRUE(s[0] == 1);
  61. EXPECT_TRUE(s[1] == 2);
  62. }
  63. {
  64. auto s = make_span(&arr[0], &arr[0]);
  65. EXPECT_TRUE(s.size() == 0);
  66. EXPECT_TRUE(s.data() == &arr[0]);
  67. }
  68. {
  69. int* p = nullptr;
  70. auto s = make_span(p, p);
  71. EXPECT_TRUE(s.size() == 0);
  72. EXPECT_TRUE(s.data() == nullptr);
  73. }
  74. }
  75. TEST(span_ext_test, make_span_from_array_constructor)
  76. {
  77. int arr[5] = {1, 2, 3, 4, 5};
  78. int arr2d[2][3] = {1, 2, 3, 4, 5, 6};
  79. int arr3d[2][3][2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
  80. {
  81. const auto s = make_span(arr);
  82. EXPECT_TRUE(s.size() == 5);
  83. EXPECT_TRUE(s.data() == std::addressof(arr[0]));
  84. }
  85. {
  86. const auto s = make_span(std::addressof(arr2d[0]), 1);
  87. EXPECT_TRUE(s.size() == 1);
  88. EXPECT_TRUE(s.data() == std::addressof(arr2d[0]));
  89. }
  90. {
  91. const auto s = make_span(std::addressof(arr3d[0]), 1);
  92. EXPECT_TRUE(s.size() == 1);
  93. EXPECT_TRUE(s.data() == std::addressof(arr3d[0]));
  94. }
  95. }
  96. TEST(span_ext_test, make_span_from_dynamic_array_constructor)
  97. {
  98. double(*arr)[3][4] = new double[100][3][4];
  99. {
  100. auto s = make_span(&arr[0][0][0], 10);
  101. EXPECT_TRUE(s.size() == 10);
  102. EXPECT_TRUE(s.data() == &arr[0][0][0]);
  103. }
  104. delete[] arr;
  105. }
  106. TEST(span_ext_test, make_span_from_std_array_constructor)
  107. {
  108. std::array<int, 4> arr = {1, 2, 3, 4};
  109. {
  110. auto s = make_span(arr);
  111. EXPECT_TRUE(s.size() == arr.size());
  112. EXPECT_TRUE(s.data() == arr.data());
  113. }
  114. // This test checks for the bug found in gcc 6.1, 6.2, 6.3, 6.4, 6.5 7.1, 7.2, 7.3 - issue #590
  115. {
  116. gsl::span<int> s1 = make_span(arr);
  117. static gsl::span<int> s2;
  118. s2 = s1;
  119. #if defined(__GNUC__) && __GNUC__ == 6 && (__GNUC_MINOR__ == 4 || __GNUC_MINOR__ == 5) && \
  120. __GNUC_PATCHLEVEL__ == 0 && defined(__OPTIMIZE__)
  121. // Known to be broken in gcc 6.4 and 6.5 with optimizations
  122. // Issue in gcc: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83116
  123. EXPECT_TRUE(s1.size() == 4);
  124. EXPECT_TRUE(s2.size() == 0);
  125. #else
  126. EXPECT_TRUE(s1.size() == s2.size());
  127. #endif
  128. }
  129. }
  130. TEST(span_ext_test, make_span_from_const_std_array_constructor)
  131. {
  132. const std::array<int, 4> arr = {1, 2, 3, 4};
  133. {
  134. auto s = make_span(arr);
  135. EXPECT_TRUE(s.size() == arr.size());
  136. EXPECT_TRUE(s.data() == arr.data());
  137. }
  138. }
  139. TEST(span_ext_test, make_span_from_std_array_const_constructor)
  140. {
  141. std::array<const int, 4> arr = {1, 2, 3, 4};
  142. {
  143. auto s = make_span(arr);
  144. EXPECT_TRUE(s.size() == arr.size());
  145. EXPECT_TRUE(s.data() == arr.data());
  146. }
  147. }
  148. TEST(span_ext_test, make_span_from_container_constructor)
  149. {
  150. std::vector<int> v = {1, 2, 3};
  151. const std::vector<int> cv = v;
  152. {
  153. auto s = make_span(v);
  154. EXPECT_TRUE(s.size() == v.size());
  155. EXPECT_TRUE(s.data() == v.data());
  156. auto cs = make_span(cv);
  157. EXPECT_TRUE(cs.size() == cv.size());
  158. EXPECT_TRUE(cs.data() == cv.data());
  159. }
  160. }
  161. TEST(span_test, interop_with_gsl_at)
  162. {
  163. std::vector<int> vec{1, 2, 3, 4, 5};
  164. gsl::span<int> sp{vec};
  165. std::vector<int> cvec{1, 2, 3, 4, 5};
  166. gsl::span<int> csp{cvec};
  167. for (gsl::index i = 0; i < gsl::narrow_cast<gsl::index>(vec.size()); ++i)
  168. {
  169. EXPECT_TRUE(&gsl::at(sp, i) == &vec[gsl::narrow_cast<size_t>(i)]);
  170. EXPECT_TRUE(&gsl::at(csp, i) == &cvec[gsl::narrow_cast<size_t>(i)]);
  171. }
  172. const auto terminateHandler = std::set_terminate([] {
  173. std::cerr << "Expected Death. interop_with_gsl_at";
  174. std::abort();
  175. });
  176. const auto expected = GetExpectedDeathString(terminateHandler);
  177. EXPECT_DEATH(gsl::at(sp, -1), expected);
  178. EXPECT_DEATH(gsl::at(sp, gsl::narrow_cast<gsl::index>(sp.size())), expected);
  179. EXPECT_DEATH(gsl::at(csp, -1), expected);
  180. EXPECT_DEATH(gsl::at(csp, gsl::narrow_cast<gsl::index>(sp.size())), expected);
  181. }
  182. TEST(span_ext_test, iterator_free_functions)
  183. {
  184. int a[] = {1, 2, 3, 4};
  185. gsl::span<int> s{a};
  186. EXPECT_TRUE((std::is_same<decltype(s.begin()), decltype(begin(s))>::value));
  187. EXPECT_TRUE((std::is_same<decltype(s.end()), decltype(end(s))>::value));
  188. EXPECT_TRUE((std::is_same<decltype(std::cbegin(s)), decltype(cbegin(s))>::value));
  189. EXPECT_TRUE((std::is_same<decltype(std::cend(s)), decltype(cend(s))>::value));
  190. EXPECT_TRUE((std::is_same<decltype(s.rbegin()), decltype(rbegin(s))>::value));
  191. EXPECT_TRUE((std::is_same<decltype(s.rend()), decltype(rend(s))>::value));
  192. EXPECT_TRUE((std::is_same<decltype(std::crbegin(s)), decltype(crbegin(s))>::value));
  193. EXPECT_TRUE((std::is_same<decltype(std::crend(s)), decltype(crend(s))>::value));
  194. EXPECT_TRUE(s.begin() == begin(s));
  195. EXPECT_TRUE(s.end() == end(s));
  196. EXPECT_TRUE(s.rbegin() == rbegin(s));
  197. EXPECT_TRUE(s.rend() == rend(s));
  198. EXPECT_TRUE(s.begin() == cbegin(s));
  199. EXPECT_TRUE(s.end() == cend(s));
  200. EXPECT_TRUE(s.rbegin() == crbegin(s));
  201. EXPECT_TRUE(s.rend() == crend(s));
  202. }
  203. TEST(span_ext_test, ssize_free_function)
  204. {
  205. int a[] = {1, 2, 3, 4};
  206. gsl::span<int> s{a};
  207. EXPECT_FALSE((std::is_same<decltype(s.size()), decltype(ssize(s))>::value));
  208. EXPECT_TRUE(s.size() == static_cast<std::size_t>(ssize(s)));
  209. }
  210. #ifndef GSL_KERNEL_MODE
  211. TEST(span_ext_test, comparison_operators)
  212. {
  213. {
  214. gsl::span<int> s1;
  215. gsl::span<int> s2;
  216. EXPECT_TRUE(s1 == s2);
  217. EXPECT_FALSE(s1 != s2);
  218. EXPECT_FALSE(s1 < s2);
  219. EXPECT_TRUE(s1 <= s2);
  220. EXPECT_FALSE(s1 > s2);
  221. EXPECT_TRUE(s1 >= s2);
  222. EXPECT_TRUE(s2 == s1);
  223. EXPECT_FALSE(s2 != s1);
  224. EXPECT_FALSE(s2 != s1);
  225. EXPECT_TRUE(s2 <= s1);
  226. EXPECT_FALSE(s2 > s1);
  227. EXPECT_TRUE(s2 >= s1);
  228. }
  229. {
  230. int arr[] = {2, 1};
  231. gsl::span<int> s1 = arr;
  232. gsl::span<int> s2 = arr;
  233. EXPECT_TRUE(s1 == s2);
  234. EXPECT_FALSE(s1 != s2);
  235. EXPECT_FALSE(s1 < s2);
  236. EXPECT_TRUE(s1 <= s2);
  237. EXPECT_FALSE(s1 > s2);
  238. EXPECT_TRUE(s1 >= s2);
  239. EXPECT_TRUE(s2 == s1);
  240. EXPECT_FALSE(s2 != s1);
  241. EXPECT_FALSE(s2 < s1);
  242. EXPECT_TRUE(s2 <= s1);
  243. EXPECT_FALSE(s2 > s1);
  244. EXPECT_TRUE(s2 >= s1);
  245. }
  246. {
  247. int arr[] = {2, 1}; // bigger
  248. gsl::span<int> s1;
  249. gsl::span<int> s2 = arr;
  250. EXPECT_TRUE(s1 != s2);
  251. EXPECT_TRUE(s2 != s1);
  252. EXPECT_FALSE(s1 == s2);
  253. EXPECT_FALSE(s2 == s1);
  254. EXPECT_TRUE(s1 < s2);
  255. EXPECT_FALSE(s2 < s1);
  256. EXPECT_TRUE(s1 <= s2);
  257. EXPECT_FALSE(s2 <= s1);
  258. EXPECT_TRUE(s2 > s1);
  259. EXPECT_FALSE(s1 > s2);
  260. EXPECT_TRUE(s2 >= s1);
  261. EXPECT_FALSE(s1 >= s2);
  262. }
  263. {
  264. int arr1[] = {1, 2};
  265. int arr2[] = {1, 2};
  266. gsl::span<int> s1 = arr1;
  267. gsl::span<int> s2 = arr2;
  268. EXPECT_TRUE(s1 == s2);
  269. EXPECT_FALSE(s1 != s2);
  270. EXPECT_FALSE(s1 < s2);
  271. EXPECT_TRUE(s1 <= s2);
  272. EXPECT_FALSE(s1 > s2);
  273. EXPECT_TRUE(s1 >= s2);
  274. EXPECT_TRUE(s2 == s1);
  275. EXPECT_FALSE(s2 != s1);
  276. EXPECT_FALSE(s2 < s1);
  277. EXPECT_TRUE(s2 <= s1);
  278. EXPECT_FALSE(s2 > s1);
  279. EXPECT_TRUE(s2 >= s1);
  280. }
  281. {
  282. int arr[] = {1, 2, 3};
  283. gsl::span<int> s1 = {&arr[0], 2}; // shorter
  284. gsl::span<int> s2 = arr; // longer
  285. EXPECT_TRUE(s1 != s2);
  286. EXPECT_TRUE(s2 != s1);
  287. EXPECT_FALSE(s1 == s2);
  288. EXPECT_FALSE(s2 == s1);
  289. EXPECT_TRUE(s1 < s2);
  290. EXPECT_FALSE(s2 < s1);
  291. EXPECT_TRUE(s1 <= s2);
  292. EXPECT_FALSE(s2 <= s1);
  293. EXPECT_TRUE(s2 > s1);
  294. EXPECT_FALSE(s1 > s2);
  295. EXPECT_TRUE(s2 >= s1);
  296. EXPECT_FALSE(s1 >= s2);
  297. }
  298. {
  299. int arr1[] = {1, 2}; // smaller
  300. int arr2[] = {2, 1}; // bigger
  301. gsl::span<int> s1 = arr1;
  302. gsl::span<int> s2 = arr2;
  303. EXPECT_TRUE(s1 != s2);
  304. EXPECT_TRUE(s2 != s1);
  305. EXPECT_FALSE(s1 == s2);
  306. EXPECT_FALSE(s2 == s1);
  307. EXPECT_TRUE(s1 < s2);
  308. EXPECT_FALSE(s2 < s1);
  309. EXPECT_TRUE(s1 <= s2);
  310. EXPECT_FALSE(s2 <= s1);
  311. EXPECT_TRUE(s2 > s1);
  312. EXPECT_FALSE(s1 > s2);
  313. EXPECT_TRUE(s2 >= s1);
  314. EXPECT_FALSE(s1 >= s2);
  315. }
  316. }
  317. #endif // GSL_KERNEL_MODE