span_tests.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  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/byte> // for byte
  18. #include <gsl/span> // for span, span_iterator, operator==, operator!=
  19. #include <gsl/util> // for narrow_cast, at
  20. #include <array> // for array
  21. #include <cstddef> // for ptrdiff_t
  22. #include <iostream> // for ptrdiff_t
  23. #include <iterator> // for reverse_iterator, operator-, operator==
  24. #include <memory> // for unique_ptr, shared_ptr, make_unique, allo...
  25. #include <regex> // for match_results, sub_match, match_results<>...
  26. #include <string> // for string
  27. #include <type_traits> // for integral_constant<>::value, is_default_co...
  28. #include <utility>
  29. #include <vector> // for vector
  30. // the string_view include and macro are used in the deduction guide verification
  31. #if (defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201611L))
  32. #ifdef __has_include
  33. #if __has_include(<string_view>)
  34. #include <string_view>
  35. #define HAS_STRING_VIEW
  36. #endif // __has_include(<string_view>)
  37. #endif // __has_include
  38. #endif // (defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201611L))
  39. #if defined(__cplusplus) && __cplusplus >= 202002L
  40. #include <span>
  41. #endif // __cplusplus >= 202002L
  42. #include "deathTestCommon.h"
  43. using namespace gsl;
  44. namespace
  45. {
  46. struct BaseClass
  47. {
  48. };
  49. struct DerivedClass : BaseClass
  50. {
  51. };
  52. struct AddressOverloaded
  53. {
  54. #if (__cplusplus > 201402L)
  55. [[maybe_unused]]
  56. #endif
  57. AddressOverloaded
  58. operator&() const
  59. {
  60. return {};
  61. }
  62. };
  63. } // namespace
  64. TEST(span_test, constructors)
  65. {
  66. span<int> s;
  67. EXPECT_TRUE(s.size() == 0);
  68. EXPECT_TRUE(s.data() == nullptr);
  69. span<const int> cs;
  70. EXPECT_TRUE(cs.size() == 0);
  71. EXPECT_TRUE(cs.data() == nullptr);
  72. }
  73. TEST(span_test, constructors_with_extent)
  74. {
  75. span<int, 0> s;
  76. EXPECT_TRUE(s.size() == 0);
  77. EXPECT_TRUE(s.data() == nullptr);
  78. span<const int, 0> cs;
  79. EXPECT_TRUE(cs.size() == 0);
  80. EXPECT_TRUE(cs.data() == nullptr);
  81. }
  82. TEST(span_test, constructors_with_bracket_init)
  83. {
  84. span<int> s{};
  85. EXPECT_TRUE(s.size() == 0);
  86. EXPECT_TRUE(s.data() == nullptr);
  87. span<const int> cs{};
  88. EXPECT_TRUE(cs.size() == 0);
  89. EXPECT_TRUE(cs.data() == nullptr);
  90. }
  91. TEST(span_test, size_optimization)
  92. {
  93. span<int> s;
  94. EXPECT_TRUE(sizeof(s) == sizeof(int*) + sizeof(ptrdiff_t));
  95. span<int, 0> se;
  96. EXPECT_TRUE(sizeof(se) == sizeof(int*));
  97. }
  98. TEST(span_test, from_nullptr_size_constructor)
  99. {
  100. const auto terminateHandler = std::set_terminate([] {
  101. std::cerr << "Expected Death. from_nullptr_size_constructor";
  102. std::abort();
  103. });
  104. const auto expected = GetExpectedDeathString(terminateHandler);
  105. {
  106. span<int> s{nullptr, narrow_cast<span<int>::size_type>(0)};
  107. EXPECT_TRUE(s.size() == 0);
  108. EXPECT_TRUE(s.data() == nullptr);
  109. span<int> cs{nullptr, narrow_cast<span<int>::size_type>(0)};
  110. EXPECT_TRUE(cs.size() == 0);
  111. EXPECT_TRUE(cs.data() == nullptr);
  112. }
  113. {
  114. auto workaround_macro = []() {
  115. const span<int, 1> s{nullptr, narrow_cast<span<int>::size_type>(0)};
  116. };
  117. EXPECT_DEATH(workaround_macro(), expected);
  118. }
  119. {
  120. auto workaround_macro = []() { const span<int> s{nullptr, 1}; };
  121. EXPECT_DEATH(workaround_macro(), expected);
  122. auto const_workaround_macro = []() { const span<const int> s{nullptr, 1}; };
  123. EXPECT_DEATH(const_workaround_macro(), expected);
  124. }
  125. {
  126. auto workaround_macro = []() { const span<int, 0> s{nullptr, 1}; };
  127. EXPECT_DEATH(workaround_macro(), expected);
  128. auto const_workaround_macro = []() { const span<const int, 0> s{nullptr, 1}; };
  129. EXPECT_DEATH(const_workaround_macro(), expected);
  130. }
  131. {
  132. span<int*> s{nullptr, narrow_cast<span<int>::size_type>(0)};
  133. EXPECT_TRUE(s.size() == 0);
  134. EXPECT_TRUE(s.data() == nullptr);
  135. span<const int*> cs{nullptr, narrow_cast<span<int>::size_type>(0)};
  136. EXPECT_TRUE(cs.size() == 0);
  137. EXPECT_TRUE(cs.data() == nullptr);
  138. }
  139. }
  140. TEST(span_test, from_pointer_length_constructor)
  141. {
  142. const auto terminateHandler = std::set_terminate([] {
  143. std::cerr << "Expected Death. from_pointer_length_constructor";
  144. std::abort();
  145. });
  146. const auto expected = GetExpectedDeathString(terminateHandler);
  147. int arr[4] = {1, 2, 3, 4};
  148. {
  149. for (int i = 0; i < 4; ++i)
  150. {
  151. {
  152. span<int> s = {&arr[0], narrow_cast<std::size_t>(i)};
  153. EXPECT_TRUE(s.size() == narrow_cast<std::size_t>(i));
  154. EXPECT_TRUE(s.data() == &arr[0]);
  155. EXPECT_TRUE(s.empty() == (i == 0));
  156. for (int j = 0; j < i; ++j) EXPECT_TRUE(arr[j] == s[narrow_cast<std::size_t>(j)]);
  157. }
  158. {
  159. span<int> s = {&arr[i], 4 - narrow_cast<std::size_t>(i)};
  160. EXPECT_TRUE(s.size() == 4 - narrow_cast<std::size_t>(i));
  161. EXPECT_TRUE(s.data() == &arr[i]);
  162. EXPECT_TRUE(s.empty() == ((4 - i) == 0));
  163. for (int j = 0; j < 4 - i; ++j)
  164. EXPECT_TRUE(arr[j + i] == s[narrow_cast<std::size_t>(j)]);
  165. }
  166. }
  167. }
  168. {
  169. span<int, 2> s{&arr[0], 2};
  170. EXPECT_TRUE(s.size() == 2);
  171. EXPECT_TRUE(s.data() == &arr[0]);
  172. EXPECT_TRUE(s[0] == 1);
  173. EXPECT_TRUE(s[1] == 2);
  174. }
  175. {
  176. int* p = nullptr;
  177. span<int> s{p, narrow_cast<span<int>::size_type>(0)};
  178. EXPECT_TRUE(s.size() == 0);
  179. EXPECT_TRUE(s.data() == nullptr);
  180. }
  181. {
  182. int* p = nullptr;
  183. auto workaround_macro = [=]() { const span<int> s{p, 2}; };
  184. EXPECT_DEATH(workaround_macro(), expected);
  185. }
  186. }
  187. TEST(span_test, from_pointer_pointer_construction)
  188. {
  189. int arr[4] = {1, 2, 3, 4};
  190. {
  191. span<int> s{&arr[0], &arr[2]};
  192. EXPECT_TRUE(s.size() == 2);
  193. EXPECT_TRUE(s.data() == &arr[0]);
  194. EXPECT_TRUE(s[0] == 1);
  195. EXPECT_TRUE(s[1] == 2);
  196. }
  197. {
  198. span<int, 2> s{&arr[0], &arr[2]};
  199. EXPECT_TRUE(s.size() == 2);
  200. EXPECT_TRUE(s.data() == &arr[0]);
  201. EXPECT_TRUE(s[0] == 1);
  202. EXPECT_TRUE(s[1] == 2);
  203. }
  204. {
  205. span<int> s{&arr[0], &arr[0]};
  206. EXPECT_TRUE(s.size() == 0);
  207. EXPECT_TRUE(s.data() == &arr[0]);
  208. }
  209. {
  210. span<int, 0> s{&arr[0], &arr[0]};
  211. EXPECT_TRUE(s.size() == 0);
  212. EXPECT_TRUE(s.data() == &arr[0]);
  213. }
  214. // this will fail the std::distance() precondition, which asserts on MSVC debug builds
  215. //{
  216. // auto workaround_macro = [&]() { span<int> s{&arr[1], &arr[0]}; };
  217. // EXPECT_DEATH(workaround_macro(), expected);
  218. //}
  219. // this will fail the std::distance() precondition, which asserts on MSVC debug builds
  220. //{
  221. // int* p = nullptr;
  222. // auto workaround_macro = [&]() { span<int> s{&arr[0], p}; };
  223. // EXPECT_DEATH(workaround_macro(), expected);
  224. //}
  225. {
  226. int* p = nullptr;
  227. span<int> s{p, p};
  228. EXPECT_TRUE(s.size() == 0);
  229. EXPECT_TRUE(s.data() == nullptr);
  230. }
  231. {
  232. int* p = nullptr;
  233. span<int, 0> s{p, p};
  234. EXPECT_TRUE(s.size() == 0);
  235. EXPECT_TRUE(s.data() == nullptr);
  236. }
  237. // this will fail the std::distance() precondition, which asserts on MSVC debug builds
  238. //{
  239. // int* p = nullptr;
  240. // auto workaround_macro = [&]() { span<int> s{&arr[0], p}; };
  241. // EXPECT_DEATH(workaround_macro(), expected);
  242. //}
  243. }
  244. TEST(span_test, from_array_constructor)
  245. {
  246. int arr[5] = {1, 2, 3, 4, 5};
  247. {
  248. const span<int> s{arr};
  249. EXPECT_TRUE(s.size() == 5);
  250. EXPECT_TRUE(s.data() == &arr[0]);
  251. }
  252. {
  253. const span<int, 5> s{arr};
  254. EXPECT_TRUE(s.size() == 5);
  255. EXPECT_TRUE(s.data() == &arr[0]);
  256. }
  257. int arr2d[2][3] = {1, 2, 3, 4, 5, 6};
  258. #ifdef CONFIRM_COMPILATION_ERRORS
  259. {
  260. span<int, 6> s{arr};
  261. }
  262. {
  263. span<int, 0> s{arr};
  264. EXPECT_TRUE(s.size() == 0);
  265. EXPECT_TRUE(s.data() == &arr[0]);
  266. }
  267. {
  268. span<int> s{arr2d};
  269. EXPECT_TRUE(s.size() == 6);
  270. EXPECT_TRUE(s.data() == &arr2d[0][0]);
  271. EXPECT_TRUE(s[0] == 1);
  272. EXPECT_TRUE(s[5] == 6);
  273. }
  274. {
  275. span<int, 0> s{arr2d};
  276. EXPECT_TRUE(s.size() == 0);
  277. EXPECT_TRUE(s.data() == &arr2d[0][0]);
  278. }
  279. {
  280. span<int, 6> s{arr2d};
  281. }
  282. #endif
  283. {
  284. const span<int[3]> s{std::addressof(arr2d[0]), 1};
  285. EXPECT_TRUE(s.size() == 1);
  286. EXPECT_TRUE(s.data() == std::addressof(arr2d[0]));
  287. }
  288. int arr3d[2][3][2] = { { {1, 2}, {3, 4}, {5, 6} }, { {7, 8}, {9, 10}, {11, 12} } };
  289. #ifdef CONFIRM_COMPILATION_ERRORS
  290. {
  291. span<int> s{arr3d};
  292. EXPECT_TRUE(s.size() == 12);
  293. EXPECT_TRUE(s.data() == &arr3d[0][0][0]);
  294. EXPECT_TRUE(s[0] == 1);
  295. EXPECT_TRUE(s[11] == 12);
  296. }
  297. {
  298. span<int, 0> s{arr3d};
  299. EXPECT_TRUE(s.size() == 0);
  300. EXPECT_TRUE(s.data() == &arr3d[0][0][0]);
  301. }
  302. {
  303. span<int, 11> s{arr3d};
  304. }
  305. {
  306. span<int, 12> s{arr3d};
  307. EXPECT_TRUE(s.size() == 12);
  308. EXPECT_TRUE(s.data() == &arr3d[0][0][0]);
  309. EXPECT_TRUE(s[0] == 1);
  310. EXPECT_TRUE(s[5] == 6);
  311. }
  312. #endif
  313. {
  314. const span<int[3][2]> s{std::addressof(arr3d[0]), 1};
  315. EXPECT_TRUE(s.size() == 1);
  316. }
  317. AddressOverloaded ao_arr[5] = {};
  318. {
  319. const span<AddressOverloaded, 5> s{ao_arr};
  320. EXPECT_TRUE(s.size() == 5);
  321. EXPECT_TRUE(s.data() == std::addressof(ao_arr[0]));
  322. }
  323. }
  324. TEST(span_test, from_dynamic_array_constructor)
  325. {
  326. double(*arr)[3][4] = new double[100][3][4];
  327. {
  328. span<double> s(&arr[0][0][0], 10);
  329. EXPECT_TRUE(s.size() == 10);
  330. EXPECT_TRUE(s.data() == &arr[0][0][0]);
  331. }
  332. delete[] arr;
  333. }
  334. TEST(span_test, from_std_array_constructor)
  335. {
  336. std::array<int, 4> arr = {1, 2, 3, 4};
  337. {
  338. span<int> s{arr};
  339. EXPECT_TRUE(s.size() == arr.size());
  340. EXPECT_TRUE(s.data() == arr.data());
  341. span<const int> cs{arr};
  342. EXPECT_TRUE(cs.size() == arr.size());
  343. EXPECT_TRUE(cs.data() == arr.data());
  344. }
  345. {
  346. span<int, 4> s{arr};
  347. EXPECT_TRUE(s.size() == arr.size());
  348. EXPECT_TRUE(s.data() == arr.data());
  349. span<const int, 4> cs{arr};
  350. EXPECT_TRUE(cs.size() == arr.size());
  351. EXPECT_TRUE(cs.data() == arr.data());
  352. }
  353. {
  354. std::array<int, 0> empty_arr{};
  355. span<int> s{empty_arr};
  356. EXPECT_TRUE(s.size() == 0);
  357. EXPECT_TRUE(s.empty());
  358. }
  359. std::array<AddressOverloaded, 4> ao_arr{};
  360. {
  361. span<AddressOverloaded, 4> fs{ao_arr};
  362. EXPECT_TRUE(fs.size() == ao_arr.size());
  363. EXPECT_TRUE(ao_arr.data() == fs.data());
  364. }
  365. #ifdef CONFIRM_COMPILATION_ERRORS
  366. {
  367. span<int, 2> s{arr};
  368. EXPECT_TRUE(s.size() == 2);
  369. EXPECT_TRUE(s.data() == arr.data());
  370. span<const int, 2> cs{arr};
  371. EXPECT_TRUE(cs.size() == 2);
  372. EXPECT_TRUE(cs.data() == arr.data());
  373. }
  374. {
  375. span<int, 0> s{arr};
  376. EXPECT_TRUE(s.size() == 0);
  377. EXPECT_TRUE(s.data() == arr.data());
  378. span<const int, 0> cs{arr};
  379. EXPECT_TRUE(cs.size() == 0);
  380. EXPECT_TRUE(cs.data() == arr.data());
  381. }
  382. {
  383. span<int, 5> s{arr};
  384. }
  385. {
  386. auto get_an_array = []() -> std::array<int, 4> { return {1, 2, 3, 4}; };
  387. auto take_a_span = [](span<int> s) { static_cast<void>(s); };
  388. // try to take a temporary std::array
  389. take_a_span(get_an_array());
  390. }
  391. #endif
  392. {
  393. auto get_an_array = []() -> std::array<int, 4> { return {1, 2, 3, 4}; };
  394. auto take_a_span = [](span<const int> s) { static_cast<void>(s); };
  395. // try to take a temporary std::array
  396. take_a_span(get_an_array());
  397. }
  398. }
  399. TEST(span_test, from_const_std_array_constructor)
  400. {
  401. const std::array<int, 4> arr = {1, 2, 3, 4};
  402. {
  403. span<const int> s{arr};
  404. EXPECT_TRUE(s.size() == arr.size());
  405. EXPECT_TRUE(s.data() == arr.data());
  406. }
  407. {
  408. span<const int, 4> s{arr};
  409. EXPECT_TRUE(s.size() == arr.size());
  410. EXPECT_TRUE(s.data() == arr.data());
  411. }
  412. const std::array<AddressOverloaded, 4> ao_arr{};
  413. {
  414. span<const AddressOverloaded, 4> s{ao_arr};
  415. EXPECT_TRUE(s.size() == ao_arr.size());
  416. EXPECT_TRUE(s.data() == ao_arr.data());
  417. }
  418. #ifdef CONFIRM_COMPILATION_ERRORS
  419. {
  420. span<const int, 2> s{arr};
  421. EXPECT_TRUE(s.size() == 2);
  422. EXPECT_TRUE(s.data() == arr.data());
  423. }
  424. {
  425. span<const int, 0> s{arr};
  426. EXPECT_TRUE(s.size() == 0);
  427. EXPECT_TRUE(s.data() == arr.data());
  428. }
  429. {
  430. span<const int, 5> s{arr};
  431. }
  432. #endif
  433. {
  434. auto get_an_array = []() -> const std::array<int, 4> { return {1, 2, 3, 4}; };
  435. auto take_a_span = [](span<const int> s) { static_cast<void>(s); };
  436. // try to take a temporary std::array
  437. take_a_span(get_an_array());
  438. }
  439. }
  440. TEST(span_test, from_std_array_const_constructor)
  441. {
  442. std::array<const int, 4> arr = {1, 2, 3, 4};
  443. {
  444. span<const int> s{arr};
  445. EXPECT_TRUE(s.size() == arr.size());
  446. EXPECT_TRUE(s.data() == arr.data());
  447. }
  448. {
  449. span<const int, 4> s{arr};
  450. EXPECT_TRUE(s.size() == arr.size());
  451. EXPECT_TRUE(s.data() == arr.data());
  452. }
  453. #ifdef CONFIRM_COMPILATION_ERRORS
  454. {
  455. span<const int, 2> s{arr};
  456. EXPECT_TRUE(s.size() == 2);
  457. EXPECT_TRUE(s.data() == arr.data());
  458. }
  459. {
  460. span<const int, 0> s{arr};
  461. EXPECT_TRUE(s.size() == 0);
  462. EXPECT_TRUE(s.data() == arr.data());
  463. }
  464. {
  465. span<const int, 5> s{arr};
  466. }
  467. {
  468. span<int, 4> s{arr};
  469. }
  470. #endif
  471. }
  472. TEST(span_test, from_container_constructor)
  473. {
  474. std::vector<int> v = {1, 2, 3};
  475. const std::vector<int> cv = v;
  476. {
  477. span<int> s{v};
  478. EXPECT_TRUE(s.size() == v.size());
  479. EXPECT_TRUE(s.data() == v.data());
  480. span<const int> cs{v};
  481. EXPECT_TRUE(cs.size() == v.size());
  482. EXPECT_TRUE(cs.data() == v.data());
  483. }
  484. std::string str = "hello";
  485. const std::string cstr = "hello";
  486. {
  487. #ifdef CONFIRM_COMPILATION_ERRORS
  488. span<char> s{str};
  489. EXPECT_TRUE(s.size() == str.size());
  490. EXPECT_TRUE(s.data() == str.data()));
  491. #endif
  492. span<const char> cs{str};
  493. EXPECT_TRUE(cs.size() == str.size());
  494. EXPECT_TRUE(cs.data() == str.data());
  495. }
  496. {
  497. #ifdef CONFIRM_COMPILATION_ERRORS
  498. span<char> s{cstr};
  499. #endif
  500. span<const char> cs{cstr};
  501. EXPECT_TRUE(cs.size() == cstr.size());
  502. EXPECT_TRUE(cs.data() == cstr.data());
  503. }
  504. {
  505. #ifdef CONFIRM_COMPILATION_ERRORS
  506. auto get_temp_vector = []() -> std::vector<int> { return {}; };
  507. auto use_span = [](span<int> s) { static_cast<void>(s); };
  508. use_span(get_temp_vector());
  509. #endif
  510. }
  511. {
  512. auto get_temp_vector = []() -> std::vector<int> { return {}; };
  513. auto use_span = [](span<const int> s) { static_cast<void>(s); };
  514. use_span(get_temp_vector());
  515. }
  516. {
  517. #ifdef CONFIRM_COMPILATION_ERRORS
  518. auto get_temp_string = []() -> std::string { return {}; };
  519. auto use_span = [](span<char> s) { static_cast<void>(s); };
  520. use_span(get_temp_string());
  521. #endif
  522. }
  523. {
  524. auto get_temp_string = []() -> std::string { return {}; };
  525. auto use_span = [](span<const char> s) { static_cast<void>(s); };
  526. use_span(get_temp_string());
  527. }
  528. {
  529. #ifdef CONFIRM_COMPILATION_ERRORS
  530. auto get_temp_vector = []() -> const std::vector<int> { return {}; };
  531. auto use_span = [](span<const char> s) { static_cast<void>(s); };
  532. use_span(get_temp_vector());
  533. #endif
  534. }
  535. {
  536. auto get_temp_string = []() -> const std::string { return {}; };
  537. auto use_span = [](span<const char> s) { static_cast<void>(s); };
  538. use_span(get_temp_string());
  539. }
  540. {
  541. #ifdef CONFIRM_COMPILATION_ERRORS
  542. std::map<int, int> m;
  543. span<int> s{m};
  544. #endif
  545. }
  546. }
  547. TEST(span_test, from_convertible_span_constructor)
  548. {
  549. const auto terminateHandler = std::set_terminate([] {
  550. std::cerr << "Expected Death. from_convertible_span_constructor";
  551. std::abort();
  552. });
  553. const auto expected = GetExpectedDeathString(terminateHandler);
  554. {
  555. span<DerivedClass> avd;
  556. span<const DerivedClass> avcd = avd;
  557. static_cast<void>(avcd);
  558. }
  559. {
  560. std::array<DerivedClass, 2> arr{};
  561. span<DerivedClass, 2> avd{arr};
  562. span<const DerivedClass, 2> avcd = avd;
  563. static_cast<void>(avcd);
  564. }
  565. {
  566. std::array<DerivedClass, 2> arr{};
  567. span<DerivedClass, 2> avd{arr};
  568. span<const DerivedClass> avcd = avd;
  569. static_cast<void>(avcd);
  570. }
  571. {
  572. std::array<DerivedClass, 2> arr{};
  573. span<DerivedClass> avd{arr};
  574. span<const DerivedClass, 2> avcd{avd};
  575. static_cast<void>(avcd);
  576. }
  577. {
  578. std::array<DerivedClass, 2> arr{};
  579. span<DerivedClass> avd{arr};
  580. using T = span<const DerivedClass, 1>;
  581. EXPECT_DEATH(T{avd}, expected);
  582. }
  583. {
  584. std::array<DerivedClass, 1> arr{};
  585. span<DerivedClass> avd{arr};
  586. using T = span<const DerivedClass, 2>;
  587. EXPECT_DEATH(T{avd}, expected);
  588. }
  589. #ifdef CONFIRM_COMPILATION_ERRORS
  590. {
  591. std::array<DerivedClass, 2> arr{};
  592. span<DerivedClass> avd{arr};
  593. span<const DerivedClass, 2> avcd = avd;
  594. static_cast<void>(avcd);
  595. }
  596. {
  597. std::array<DerivedClass, 2> arr{};
  598. span<DerivedClass, 2> avd{arr};
  599. span<const DerivedClass, 1> avcd = avd;
  600. static_cast<void>(avcd);
  601. }
  602. {
  603. std::array<DerivedClass, 2> arr{};
  604. span<DerivedClass, 2> avd{arr};
  605. span<const DerivedClass, 3> avcd = avd;
  606. static_cast<void>(avcd);
  607. }
  608. {
  609. span<DerivedClass> avd;
  610. span<BaseClass> avb = avd;
  611. static_cast<void>(avb);
  612. }
  613. {
  614. span<int> s;
  615. span<unsigned int> s2 = s;
  616. static_cast<void>(s2);
  617. }
  618. {
  619. span<int> s;
  620. span<const unsigned int> s2 = s;
  621. static_cast<void>(s2);
  622. }
  623. {
  624. span<int> s;
  625. span<short> s2 = s;
  626. static_cast<void>(s2);
  627. }
  628. #endif
  629. }
  630. TEST(span_test, copy_move_and_assignment)
  631. {
  632. span<int> s1;
  633. EXPECT_TRUE(s1.empty());
  634. int arr[] = {3, 4, 5};
  635. span<const int> s2 = arr;
  636. EXPECT_TRUE(s2.size() == 3);
  637. EXPECT_TRUE(s2.data() == &arr[0]);
  638. s2 = s1;
  639. EXPECT_TRUE(s2.empty());
  640. auto get_temp_span = [&]() -> span<int> { return {&arr[1], 2}; };
  641. auto use_span = [&](span<const int> s) {
  642. EXPECT_TRUE(s.size() == 2);
  643. EXPECT_TRUE(s.data() == &arr[1]);
  644. };
  645. use_span(get_temp_span());
  646. s1 = get_temp_span();
  647. EXPECT_TRUE(s1.size() == 2);
  648. EXPECT_TRUE(s1.data() == &arr[1]);
  649. }
  650. TEST(span_test, first)
  651. {
  652. const auto terminateHandler = std::set_terminate([] {
  653. std::cerr << "Expected Death. first";
  654. std::abort();
  655. });
  656. const auto expected = GetExpectedDeathString(terminateHandler);
  657. int arr[5] = {1, 2, 3, 4, 5};
  658. {
  659. span<int, 5> av = arr;
  660. EXPECT_TRUE(av.first<2>().size() == 2);
  661. EXPECT_TRUE(av.first(2).size() == 2);
  662. }
  663. {
  664. span<int, 5> av = arr;
  665. EXPECT_TRUE(av.first<0>().size() == 0);
  666. EXPECT_TRUE(av.first(0).size() == 0);
  667. }
  668. {
  669. span<int, 5> av = arr;
  670. EXPECT_TRUE(av.first<5>().size() == 5);
  671. EXPECT_TRUE(av.first(5).size() == 5);
  672. }
  673. {
  674. span<int, 5> av = arr;
  675. #ifdef CONFIRM_COMPILATION_ERRORS
  676. EXPECT_TRUE(av.first<6>().size() == 6);
  677. EXPECT_TRUE(av.first<-1>().size() == -1);
  678. #endif
  679. EXPECT_DEATH(av.first(6).size(), expected);
  680. }
  681. {
  682. span<int> av;
  683. EXPECT_TRUE(av.first<0>().size() == 0);
  684. EXPECT_TRUE(av.first(0).size() == 0);
  685. }
  686. }
  687. TEST(span_test, last)
  688. {
  689. const auto terminateHandler = std::set_terminate([] {
  690. std::cerr << "Expected Death. last";
  691. std::abort();
  692. });
  693. const auto expected = GetExpectedDeathString(terminateHandler);
  694. int arr[5] = {1, 2, 3, 4, 5};
  695. {
  696. span<int, 5> av = arr;
  697. EXPECT_TRUE(av.last<2>().size() == 2);
  698. EXPECT_TRUE(av.last(2).size() == 2);
  699. }
  700. {
  701. span<int, 5> av = arr;
  702. EXPECT_TRUE(av.last<0>().size() == 0);
  703. EXPECT_TRUE(av.last(0).size() == 0);
  704. }
  705. {
  706. span<int, 5> av = arr;
  707. EXPECT_TRUE(av.last<5>().size() == 5);
  708. EXPECT_TRUE(av.last(5).size() == 5);
  709. }
  710. {
  711. span<int, 5> av = arr;
  712. #ifdef CONFIRM_COMPILATION_ERRORS
  713. EXPECT_TRUE(av.last<6>().size() == 6);
  714. #endif
  715. EXPECT_DEATH(av.last(6).size(), expected);
  716. }
  717. {
  718. span<int> av;
  719. EXPECT_TRUE(av.last<0>().size() == 0);
  720. EXPECT_TRUE(av.last(0).size() == 0);
  721. }
  722. }
  723. TEST(span_test, subspan)
  724. {
  725. const auto terminateHandler = std::set_terminate([] {
  726. std::cerr << "Expected Death. subspan";
  727. std::abort();
  728. });
  729. const auto expected = GetExpectedDeathString(terminateHandler);
  730. int arr[5] = {1, 2, 3, 4, 5};
  731. {
  732. span<int, 5> av = arr;
  733. EXPECT_TRUE((av.subspan<2, 2>().size()) == 2);
  734. EXPECT_TRUE(decltype(av.subspan<2, 2>())::extent == 2);
  735. EXPECT_TRUE(av.subspan(2, 2).size() == 2);
  736. EXPECT_TRUE(av.subspan(2, 3).size() == 3);
  737. }
  738. {
  739. span<int, 5> av = arr;
  740. EXPECT_TRUE((av.subspan<0, 0>().size()) == 0);
  741. EXPECT_TRUE(decltype(av.subspan<0, 0>())::extent == 0);
  742. EXPECT_TRUE(av.subspan(0, 0).size() == 0);
  743. }
  744. {
  745. span<int, 5> av = arr;
  746. EXPECT_TRUE((av.subspan<0, 5>().size()) == 5);
  747. EXPECT_TRUE(decltype(av.subspan<0, 5>())::extent == 5);
  748. EXPECT_TRUE(av.subspan(0, 5).size() == 5);
  749. EXPECT_DEATH(av.subspan(0, 6).size(), expected);
  750. EXPECT_DEATH(av.subspan(1, 5).size(), expected);
  751. }
  752. {
  753. span<int, 5> av = arr;
  754. EXPECT_TRUE((av.subspan<4, 0>().size()) == 0);
  755. EXPECT_TRUE(decltype(av.subspan<4, 0>())::extent == 0);
  756. EXPECT_TRUE(av.subspan(4, 0).size() == 0);
  757. EXPECT_TRUE(av.subspan(5, 0).size() == 0);
  758. EXPECT_DEATH(av.subspan(6, 0).size(), expected);
  759. }
  760. {
  761. span<int, 5> av = arr;
  762. EXPECT_TRUE(av.subspan<1>().size() == 4);
  763. EXPECT_TRUE(decltype(av.subspan<1>())::extent == 4);
  764. }
  765. {
  766. span<int> av;
  767. EXPECT_TRUE((av.subspan<0, 0>().size()) == 0);
  768. EXPECT_TRUE(decltype(av.subspan<0, 0>())::extent == 0);
  769. EXPECT_TRUE(av.subspan(0, 0).size() == 0);
  770. EXPECT_DEATH((av.subspan<1, 0>().size()), expected);
  771. }
  772. {
  773. span<int> av;
  774. EXPECT_TRUE(av.subspan(0).size() == 0);
  775. EXPECT_DEATH(av.subspan(1).size(), expected);
  776. }
  777. {
  778. span<int> av = arr;
  779. EXPECT_TRUE(av.subspan(0).size() == 5);
  780. EXPECT_TRUE(av.subspan(1).size() == 4);
  781. EXPECT_TRUE(av.subspan(4).size() == 1);
  782. EXPECT_TRUE(av.subspan(5).size() == 0);
  783. EXPECT_DEATH(av.subspan(6).size(), expected);
  784. const auto av2 = av.subspan(1);
  785. for (std::size_t i = 0; i < 4; ++i) EXPECT_TRUE(av2[i] == static_cast<int>(i) + 2);
  786. }
  787. {
  788. span<int, 5> av = arr;
  789. EXPECT_TRUE(av.subspan(0).size() == 5);
  790. EXPECT_TRUE(av.subspan(1).size() == 4);
  791. EXPECT_TRUE(av.subspan(4).size() == 1);
  792. EXPECT_TRUE(av.subspan(5).size() == 0);
  793. EXPECT_DEATH(av.subspan(6).size(), expected);
  794. const auto av2 = av.subspan(1);
  795. for (std::size_t i = 0; i < 4; ++i) EXPECT_TRUE(av2[i] == static_cast<int>(i) + 2);
  796. }
  797. }
  798. TEST(span_test, iterator_default_init)
  799. {
  800. span<int>::iterator it1;
  801. span<int>::iterator it2;
  802. EXPECT_TRUE(it1 == it2);
  803. }
  804. TEST(span_test, iterator_comparisons)
  805. {
  806. int a[] = {1, 2, 3, 4};
  807. {
  808. span<int> s = a;
  809. span<int>::iterator it = s.begin();
  810. auto it2 = it + 1;
  811. EXPECT_TRUE(it == it);
  812. EXPECT_TRUE(it == s.begin());
  813. EXPECT_TRUE(s.begin() == it);
  814. EXPECT_TRUE(it != it2);
  815. EXPECT_TRUE(it2 != it);
  816. EXPECT_TRUE(it != s.end());
  817. EXPECT_TRUE(it2 != s.end());
  818. EXPECT_TRUE(s.end() != it);
  819. EXPECT_TRUE(it < it2);
  820. EXPECT_TRUE(it <= it2);
  821. EXPECT_TRUE(it2 <= s.end());
  822. EXPECT_TRUE(it < s.end());
  823. EXPECT_TRUE(it2 > it);
  824. EXPECT_TRUE(it2 >= it);
  825. EXPECT_TRUE(s.end() > it2);
  826. EXPECT_TRUE(s.end() >= it2);
  827. }
  828. }
  829. TEST(span_test, incomparable_iterators)
  830. {
  831. const auto terminateHandler = std::set_terminate([] {
  832. std::cerr << "Expected Death. incomparable_iterators";
  833. std::abort();
  834. });
  835. const auto expected = GetExpectedDeathString(terminateHandler);
  836. int a[] = {1, 2, 3, 4};
  837. int b[] = {1, 2, 3, 4};
  838. {
  839. span<int> s = a;
  840. span<int> s2 = b;
  841. #if (__cplusplus > 201402L)
  842. EXPECT_DEATH([[maybe_unused]] bool _ = (s.begin() == s2.begin()), expected);
  843. EXPECT_DEATH([[maybe_unused]] bool _ = (s.begin() <= s2.begin()), expected);
  844. #else
  845. EXPECT_DEATH(bool _ = (s.begin() == s2.begin()), expected);
  846. EXPECT_DEATH(bool _ = (s.begin() <= s2.begin()), expected);
  847. #endif
  848. }
  849. }
  850. TEST(span_test, begin_end)
  851. {
  852. const auto terminateHandler = std::set_terminate([] {
  853. std::cerr << "Expected Death. begin_end";
  854. std::abort();
  855. });
  856. const auto expected = GetExpectedDeathString(terminateHandler);
  857. {
  858. int a[] = {1, 2, 3, 4};
  859. span<int> s = a;
  860. span<int>::iterator it = s.begin();
  861. span<int>::iterator it2 = std::begin(s);
  862. EXPECT_TRUE(it == it2);
  863. it = s.end();
  864. it2 = std::end(s);
  865. EXPECT_TRUE(it == it2);
  866. }
  867. {
  868. int a[] = {1, 2, 3, 4};
  869. span<int> s = a;
  870. auto it = s.begin();
  871. auto first = it;
  872. EXPECT_TRUE(it == first);
  873. EXPECT_TRUE(*it == 1);
  874. auto beyond = s.end();
  875. EXPECT_TRUE(it != beyond);
  876. EXPECT_DEATH(*beyond, expected);
  877. EXPECT_TRUE(beyond - first == 4);
  878. EXPECT_TRUE(first - first == 0);
  879. EXPECT_TRUE(beyond - beyond == 0);
  880. ++it;
  881. EXPECT_TRUE(it - first == 1);
  882. EXPECT_TRUE(*it == 2);
  883. *it = 22;
  884. EXPECT_TRUE(*it == 22);
  885. EXPECT_TRUE(beyond - it == 3);
  886. it = first;
  887. EXPECT_TRUE(it == first);
  888. while (it != s.end())
  889. {
  890. *it = 5;
  891. ++it;
  892. }
  893. EXPECT_TRUE(it == beyond);
  894. EXPECT_TRUE(it - beyond == 0);
  895. for (const auto& n : s) { EXPECT_TRUE(n == 5); }
  896. }
  897. }
  898. TEST(span_test, rbegin_rend)
  899. {
  900. const auto terminateHandler = std::set_terminate([] {
  901. std::cerr << "Expected Death. rbegin_rend";
  902. std::abort();
  903. });
  904. const auto expected = GetExpectedDeathString(terminateHandler);
  905. {
  906. int a[] = {1, 2, 3, 4};
  907. span<int> s = a;
  908. auto it = s.rbegin();
  909. auto first = it;
  910. EXPECT_TRUE(it == first);
  911. EXPECT_TRUE(*it == 4);
  912. auto beyond = s.rend();
  913. EXPECT_TRUE(it != beyond);
  914. #if (__cplusplus > 201402L)
  915. EXPECT_DEATH([[maybe_unused]] auto _ = *beyond, expected);
  916. #else
  917. EXPECT_DEATH(auto _ = *beyond, expected);
  918. #endif
  919. EXPECT_TRUE(beyond - first == 4);
  920. EXPECT_TRUE(first - first == 0);
  921. EXPECT_TRUE(beyond - beyond == 0);
  922. ++it;
  923. EXPECT_TRUE(it - s.rbegin() == 1);
  924. EXPECT_TRUE(*it == 3);
  925. *it = 22;
  926. EXPECT_TRUE(*it == 22);
  927. EXPECT_TRUE(beyond - it == 3);
  928. it = first;
  929. EXPECT_TRUE(it == first);
  930. while (it != s.rend())
  931. {
  932. *it = 5;
  933. ++it;
  934. }
  935. EXPECT_TRUE(it == beyond);
  936. EXPECT_TRUE(it - beyond == 0);
  937. for (const auto& n : s) { EXPECT_TRUE(n == 5); }
  938. }
  939. }
  940. TEST(span_test, as_bytes)
  941. {
  942. int a[] = {1, 2, 3, 4};
  943. {
  944. const span<const int> s = a;
  945. EXPECT_TRUE(s.size() == 4);
  946. const span<const byte> bs = as_bytes(s);
  947. EXPECT_TRUE(static_cast<const void*>(bs.data()) == static_cast<const void*>(s.data()));
  948. EXPECT_TRUE(bs.size() == s.size_bytes());
  949. }
  950. {
  951. span<int> s;
  952. const auto bs = as_bytes(s);
  953. EXPECT_TRUE(bs.size() == s.size());
  954. EXPECT_TRUE(bs.size() == 0);
  955. EXPECT_TRUE(bs.size_bytes() == 0);
  956. EXPECT_TRUE(static_cast<const void*>(bs.data()) == static_cast<const void*>(s.data()));
  957. EXPECT_TRUE(bs.data() == nullptr);
  958. }
  959. {
  960. span<int> s = a;
  961. const auto bs = as_bytes(s);
  962. EXPECT_TRUE(static_cast<const void*>(bs.data()) == static_cast<const void*>(s.data()));
  963. EXPECT_TRUE(bs.size() == s.size_bytes());
  964. }
  965. }
  966. TEST(span_test, as_writable_bytes)
  967. {
  968. int a[] = {1, 2, 3, 4};
  969. {
  970. #ifdef CONFIRM_COMPILATION_ERRORS
  971. // you should not be able to get writeable bytes for const objects
  972. span<const int> s = a;
  973. EXPECT_TRUE(s.size() == 4);
  974. span<const byte> bs = as_writable_bytes(s);
  975. EXPECT_TRUE(static_cast<void*>(bs.data()) == static_cast<void*>(s.data()));
  976. EXPECT_TRUE(bs.size() == s.size_bytes());
  977. #endif
  978. }
  979. {
  980. span<int> s;
  981. const auto bs = as_writable_bytes(s);
  982. EXPECT_TRUE(bs.size() == s.size());
  983. EXPECT_TRUE(bs.size() == 0);
  984. EXPECT_TRUE(bs.size_bytes() == 0);
  985. EXPECT_TRUE(static_cast<void*>(bs.data()) == static_cast<void*>(s.data()));
  986. EXPECT_TRUE(bs.data() == nullptr);
  987. }
  988. {
  989. span<int> s = a;
  990. const auto bs = as_writable_bytes(s);
  991. EXPECT_TRUE(static_cast<void*>(bs.data()) == static_cast<void*>(s.data()));
  992. EXPECT_TRUE(bs.size() == s.size_bytes());
  993. }
  994. }
  995. TEST(span_test, fixed_size_conversions)
  996. {
  997. const auto terminateHandler = std::set_terminate([] {
  998. std::cerr << "Expected Death. fixed_size_conversions";
  999. std::abort();
  1000. });
  1001. const auto expected = GetExpectedDeathString(terminateHandler);
  1002. int arr[] = {1, 2, 3, 4};
  1003. // converting to an span from an equal size array is ok
  1004. span<int, 4> s4 = arr;
  1005. EXPECT_TRUE(s4.size() == 4);
  1006. // converting to dynamic_range is always ok
  1007. {
  1008. span<int> s = s4;
  1009. EXPECT_TRUE(s.size() == s4.size());
  1010. static_cast<void>(s);
  1011. }
  1012. // initialization or assignment to static span that REDUCES size is NOT ok
  1013. #ifdef CONFIRM_COMPILATION_ERRORS
  1014. {
  1015. span<int, 2> s = arr;
  1016. }
  1017. {
  1018. span<int, 2> s2 = s4;
  1019. static_cast<void>(s2);
  1020. }
  1021. #endif
  1022. // even when done dynamically
  1023. {
  1024. /*
  1025. // this now results in a compile-time error, rather than runtime.
  1026. // There is no suitable conversion from dynamic span to fixed span.
  1027. span<int> s = arr;
  1028. auto f = [&]() {
  1029. const span<int, 2> s2 = s;
  1030. static_cast<void>(s2);
  1031. };
  1032. EXPECT_DEATH(f(), expected);
  1033. */
  1034. }
  1035. // but doing so explicitly is ok
  1036. // you can convert statically
  1037. {
  1038. const span<int, 2> s2{&arr[0], 2};
  1039. static_cast<void>(s2);
  1040. }
  1041. {
  1042. const span<int, 1> s1 = s4.first<1>();
  1043. static_cast<void>(s1);
  1044. }
  1045. /*
  1046. // this is not a legal operation in std::span, so we are no longer supporting it
  1047. // conversion from span<int, 4> to span<int, dynamic_extent> via call to `first`
  1048. // then convert from span<int, dynamic_extent> to span<int, 1>
  1049. // The dynamic to fixed extents are not supported in the standard
  1050. // to make this work, span<int, 1> would need to be span<int>.
  1051. {
  1052. // NB: implicit conversion to span<int,1> from span<int>
  1053. span<int, 1> s1 = s4.first(1);
  1054. static_cast<void>(s1);
  1055. }
  1056. */
  1057. // initialization or assignment to static span that requires size INCREASE is not ok.
  1058. int arr2[2] = {1, 2};
  1059. #ifdef CONFIRM_COMPILATION_ERRORS
  1060. {
  1061. span<int, 4> s3 = arr2;
  1062. }
  1063. {
  1064. span<int, 2> s2 = arr2;
  1065. span<int, 4> s4a = s2;
  1066. }
  1067. #endif
  1068. {
  1069. auto f = [&]() {
  1070. const span<int, 4> _s4{arr2, 2};
  1071. static_cast<void>(_s4);
  1072. };
  1073. EXPECT_DEATH(f(), expected);
  1074. }
  1075. /*
  1076. // This no longer compiles. There is no suitable conversion from dynamic span to a fixed size
  1077. span.
  1078. // this should fail - we are trying to assign a small dynamic span to a fixed_size larger one
  1079. span<int> av = arr2; auto f = [&]() {
  1080. const span<int, 4> _s4 = av;
  1081. static_cast<void>(_s4);
  1082. };
  1083. EXPECT_DEATH(f(), expected);
  1084. */
  1085. }
  1086. TEST(span_test, interop_with_std_regex)
  1087. {
  1088. char lat[] = {'1', '2', '3', '4', '5', '6', 'E', 'F', 'G'};
  1089. span<char> s = lat;
  1090. const auto f_it = s.begin() + 7;
  1091. std::match_results<span<char>::iterator> match;
  1092. std::regex_match(s.begin(), s.end(), match, std::regex(".*"));
  1093. EXPECT_TRUE(match.ready());
  1094. EXPECT_FALSE(match.empty());
  1095. EXPECT_TRUE(match[0].matched);
  1096. EXPECT_TRUE(match[0].first == s.begin());
  1097. EXPECT_TRUE(match[0].second == s.end());
  1098. std::regex_search(s.begin(), s.end(), match, std::regex("F"));
  1099. EXPECT_TRUE(match.ready());
  1100. EXPECT_FALSE(match.empty());
  1101. EXPECT_TRUE(match[0].matched);
  1102. EXPECT_TRUE(match[0].first == f_it);
  1103. EXPECT_TRUE(match[0].second == (f_it + 1));
  1104. }
  1105. TEST(span_test, default_constructible)
  1106. {
  1107. EXPECT_TRUE((std::is_default_constructible<span<int>>::value));
  1108. EXPECT_TRUE((std::is_default_constructible<span<int, 0>>::value));
  1109. EXPECT_FALSE((std::is_default_constructible<span<int, 42>>::value));
  1110. }
  1111. TEST(span_test, std_container_ctad)
  1112. {
  1113. #if (defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201611L))
  1114. // this test is just to verify that these compile
  1115. {
  1116. std::vector<int> v{1, 2, 3, 4};
  1117. gsl::span sp{v};
  1118. static_assert(std::is_same<decltype(sp), gsl::span<int>>::value);
  1119. }
  1120. {
  1121. std::string str{"foo"};
  1122. gsl::span sp{str};
  1123. static_assert(std::is_same<decltype(sp), gsl::span<char>>::value);
  1124. }
  1125. #ifdef HAS_STRING_VIEW
  1126. {
  1127. std::string_view sv{"foo"};
  1128. gsl::span sp{sv};
  1129. static_assert(std::is_same<decltype(sp), gsl::span<const char>>::value);
  1130. }
  1131. #endif
  1132. #endif
  1133. }
  1134. TEST(span_test, front_back)
  1135. {
  1136. int arr[5] = {1, 2, 3, 4, 5};
  1137. span<int> s{arr};
  1138. EXPECT_TRUE(s.front() == 1);
  1139. EXPECT_TRUE(s.back() == 5);
  1140. const auto terminateHandler = std::set_terminate([] {
  1141. std::cerr << "Expected Death. front_back";
  1142. std::abort();
  1143. });
  1144. const auto expected = GetExpectedDeathString(terminateHandler);
  1145. span<int> s2;
  1146. EXPECT_DEATH(s2.front(), expected);
  1147. EXPECT_DEATH(s2.back(), expected);
  1148. }
  1149. #if defined(FORCE_STD_SPAN_TESTS) || defined(__cpp_lib_span) && __cpp_lib_span >= 202002L
  1150. TEST(span_test, std_span)
  1151. {
  1152. // make sure std::span can be constructed from gsl::span
  1153. int arr[5] = {1, 2, 3, 4, 5};
  1154. gsl::span<int> gsl_span{arr};
  1155. #if defined(__cpp_lib_ranges) || (defined(_MSVC_STL_VERSION) && defined(__cpp_lib_concepts))
  1156. EXPECT_TRUE(std::to_address(gsl_span.begin()) == gsl_span.data());
  1157. EXPECT_TRUE(std::to_address(gsl_span.end()) == gsl_span.data() + gsl_span.size());
  1158. #endif // __cpp_lib_ranges
  1159. std::span<int> std_span = gsl_span;
  1160. EXPECT_TRUE(std_span.data() == gsl_span.data());
  1161. EXPECT_TRUE(std_span.size() == gsl_span.size());
  1162. }
  1163. #endif // defined(FORCE_STD_SPAN_TESTS) || defined(__cpp_lib_span) && __cpp_lib_span >= 202002L
  1164. #if defined(__cpp_lib_span) && defined(__cpp_lib_ranges)
  1165. // This test covers the changes in PR #1100
  1166. TEST(span_test, msvc_compile_error_PR1100)
  1167. {
  1168. int arr[]{1, 7, 2, 9};
  1169. gsl::span sp{arr, std::size(arr)};
  1170. std::ranges::sort(sp);
  1171. for (const auto& e : sp) {
  1172. (void)e;
  1173. }
  1174. }
  1175. #endif // defined(__cpp_lib_span) && defined(__cpp_lib_ranges)