A C++ toolbox repo until the pair uTL/dTL arives
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*!
  2. * \file span.cpp
  3. * \brief
  4. * Unit tests for span
  5. *
  6. * \copyright Copyright (C) 2020 Christos Choutouridis <christos@choutouridis.net>
  7. *
  8. * <dl class=\"section copyright\"><dt>License</dt><dd>
  9. * The MIT License (MIT)
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in all
  19. * copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  27. * SOFTWARE.
  28. * </dd></dl>
  29. *
  30. */
  31. #include <cont/span.h>
  32. #include <gtest/gtest.h>
  33. #include <cont/deque.h>
  34. #include <vector>
  35. #include <type_traits>
  36. // tests from https://github.com/tcbrindle/span/blob/master/test/test_span.cpp
  37. namespace test_span {
  38. using namespace tbx;
  39. TEST (Tspan, default_constructors) {
  40. EXPECT_EQ (true, ( std::is_nothrow_default_constructible<span<int>>::value));
  41. EXPECT_EQ (true, ( std::is_nothrow_default_constructible<span<int, 0>>::value));
  42. EXPECT_EQ (true, (!std::is_nothrow_default_constructible<span<int, 42>>::value));
  43. constexpr span<int> s1{};
  44. EXPECT_EQ (0UL, s1.size());
  45. EXPECT_EQ (nullptr, s1.data());
  46. EXPECT_EQ (s1.begin(), s1.end());
  47. constexpr span<int, 0> s2{};
  48. EXPECT_EQ (0UL, s2.size());
  49. EXPECT_EQ (nullptr, s2.data());
  50. EXPECT_EQ (s2.begin(), s2.end());
  51. }
  52. TEST (Tspan, pointer_constructors) {
  53. // pointer length
  54. EXPECT_EQ (true, (std::is_constructible<span<int>, int*, int>::value));
  55. EXPECT_EQ (true, (std::is_constructible<span<const int>, int*, int>::value));
  56. EXPECT_EQ (true, (std::is_constructible<span<const int>, const int*, int>::value));
  57. EXPECT_EQ (true, (std::is_constructible<span<int, 42>, int*, int>::value));
  58. EXPECT_EQ (true, (std::is_constructible<span<const int, 42>, int*, int>::value));
  59. EXPECT_EQ (true, (std::is_constructible<span<const int, 42>, const int*, int>::value));
  60. int arr[] = {1, 2, 3};
  61. // dynamic size
  62. span<int> s1(arr, 3);
  63. EXPECT_EQ (3UL, s1.size());
  64. EXPECT_EQ (arr, s1.data());
  65. EXPECT_EQ (std::begin(arr), s1.begin());
  66. EXPECT_EQ (std::end(arr), s1.end());
  67. // fixed size
  68. span<int, 3> s2(arr, 3);
  69. EXPECT_EQ (3UL, s2.size());
  70. EXPECT_EQ (arr, s2.data());
  71. EXPECT_EQ (std::begin(arr), s2.begin());
  72. EXPECT_EQ (std::end(arr), s2.end());
  73. // pointer pointer
  74. EXPECT_EQ (true, (std::is_constructible<span<int>, int*, int*>::value));
  75. EXPECT_EQ (true, (std::is_constructible<span<float>, float*, float*>::value));
  76. EXPECT_EQ (true, (std::is_constructible<span<int, 42>, int*, int*>::value));
  77. EXPECT_EQ (true, (std::is_constructible<span<float, 42>, float*, float*>::value));
  78. // dynamic size
  79. span<int> s3(arr, arr + 3);
  80. EXPECT_EQ (3UL, s3.size());
  81. EXPECT_EQ (arr, s3.data());
  82. EXPECT_EQ (std::begin(arr), s3.begin());
  83. EXPECT_EQ (std::end(arr), s3.end());
  84. // fixed size
  85. span<int, 3> s4(arr, arr + 3);
  86. EXPECT_EQ (3UL, s4.size());
  87. EXPECT_EQ (arr, s4.data());
  88. EXPECT_EQ (std::begin(arr), s4.begin());
  89. EXPECT_EQ (std::end(arr), s4.end());
  90. }
  91. TEST (Tspan, C_array_constructors) {
  92. using int_array_t = int[3];
  93. using float_array_t = float[3];
  94. EXPECT_EQ (true, ( std::is_nothrow_constructible<span<int>, int_array_t&>::value));
  95. EXPECT_EQ (true, (!std::is_constructible<span<int>, int_array_t const&>::value));
  96. EXPECT_EQ (true, (!std::is_constructible<span<int>, float_array_t>::value));
  97. EXPECT_EQ (true, ( std::is_nothrow_constructible<span<const int>, int_array_t&>::value));
  98. EXPECT_EQ (true, ( std::is_nothrow_constructible<span<const int>, int_array_t const&>::value));
  99. EXPECT_EQ (true, (!std::is_constructible<span<const int>, float_array_t>::value));
  100. EXPECT_EQ (true, ( std::is_nothrow_constructible<span<int, 3>, int_array_t&>::value));
  101. EXPECT_EQ (true, (!std::is_constructible<span<int, 3>, int_array_t const&>::value));
  102. EXPECT_EQ (true, (!std::is_constructible<span<int, 3>, float_array_t&>::value));
  103. EXPECT_EQ (true, ( std::is_nothrow_constructible<span<const int, 3>, int_array_t&>::value));
  104. EXPECT_EQ (true, ( std::is_nothrow_constructible<span<const int, 3>, int_array_t const&>::value));
  105. EXPECT_EQ (true, (!std::is_constructible<span<const int, 3>, float_array_t>::value));
  106. EXPECT_EQ (true, (!std::is_constructible<span<int, 42>, int_array_t&>::value));
  107. EXPECT_EQ (true, (!std::is_constructible<span<int, 42>, int_array_t const&>::value));
  108. EXPECT_EQ (true, (!std::is_constructible<span<int, 42>, float_array_t&>::value));
  109. EXPECT_EQ (true, (!std::is_constructible<span<const int, 42>, int_array_t&>::value));
  110. EXPECT_EQ (true, (!std::is_constructible<span<const int, 42>, int_array_t const&>::value));
  111. EXPECT_EQ (true, (!std::is_constructible<span<const int, 42>, float_array_t&>::value));
  112. int arr[] = {1, 2, 3};
  113. // non-const dynamic size
  114. span<int> s1{arr};
  115. EXPECT_EQ (s1.size(), 3UL);
  116. EXPECT_EQ (s1.data(), arr);
  117. EXPECT_EQ (s1.begin(), std::begin(arr));
  118. EXPECT_EQ (s1.end(), std::end(arr));
  119. // non-const dynamic size
  120. span<const int> s2{arr};
  121. EXPECT_EQ (s2.size(), 3UL);
  122. EXPECT_EQ (s2.data(), arr);
  123. EXPECT_EQ (s2.begin(), std::begin(arr));
  124. EXPECT_EQ (s2.end(), std::end(arr));
  125. // non-const fixed size
  126. span<int, 3> s3{arr};
  127. EXPECT_EQ (s3.size(), 3UL);
  128. EXPECT_EQ (s3.data(), arr);
  129. EXPECT_EQ (s3.begin(), std::begin(arr));
  130. EXPECT_EQ (s3.end(), std::end(arr));
  131. // non-const fixed size
  132. span<const int, 3> s4{arr};
  133. EXPECT_EQ (s4.size(), 3UL);
  134. EXPECT_EQ (s4.data(), arr);
  135. EXPECT_EQ (s4.begin(), std::begin(arr));
  136. EXPECT_EQ (s4.end(), std::end(arr));
  137. }
  138. TEST (Tspan, array_constructors) {
  139. using int_array_t = std::array<int, 3>;
  140. using float_array_t = std::array<float, 3>;
  141. using zero_array_t = std::array<int, 0>;
  142. EXPECT_EQ (true, ( std::is_nothrow_constructible<span<int>, int_array_t&>::value));
  143. EXPECT_EQ (true, (!std::is_constructible<span<int>, int_array_t const&>::value));
  144. EXPECT_EQ (true, (!std::is_constructible<span<int>, float_array_t>::value));
  145. EXPECT_EQ (true, ( std::is_nothrow_constructible<span<const int>, int_array_t&>::value));
  146. EXPECT_EQ (true, ( std::is_nothrow_constructible<span<const int>, int_array_t const&>::value));
  147. EXPECT_EQ (true, (!std::is_constructible<span<const int>, float_array_t const&>::value));
  148. EXPECT_EQ (true, ( std::is_nothrow_constructible<span<int, 3>, int_array_t&>::value));
  149. EXPECT_EQ (true, (!std::is_constructible<span<int, 3>, int_array_t const&>::value));
  150. EXPECT_EQ (true, (!std::is_constructible<span<int, 3>, float_array_t>::value));
  151. EXPECT_EQ (true, ( std::is_nothrow_constructible<span<const int, 3>, int_array_t&>::value));
  152. EXPECT_EQ (true, ( std::is_nothrow_constructible<span<const int, 3>, int_array_t const&>::value));
  153. EXPECT_EQ (true, (!std::is_constructible<span<const int, 3>, float_array_t const&>::value));
  154. EXPECT_EQ (true, (!std::is_constructible<span<int, 42>, int_array_t&>::value));
  155. EXPECT_EQ (true, (!std::is_constructible<span<int, 42>, int_array_t const&>::value));
  156. EXPECT_EQ (true, (!std::is_constructible<span<int, 42>, float_array_t const&>::value));
  157. EXPECT_EQ (true, (!std::is_constructible<span<const int, 42>, int_array_t&>::value));
  158. EXPECT_EQ (true, (!std::is_constructible<span<const int, 42>, int_array_t const&>::value));
  159. EXPECT_EQ (true, (!std::is_constructible<span<const int, 42>, float_array_t&>::value));
  160. EXPECT_EQ (true, ( std::is_constructible<span<int>, zero_array_t&>::value));
  161. EXPECT_EQ (true, (!std::is_constructible<span<int>, const zero_array_t&>::value));
  162. EXPECT_EQ (true, ( std::is_constructible<span<const int>, zero_array_t&>::value));
  163. EXPECT_EQ (true, ( std::is_constructible<span<const int>, const zero_array_t&>::value));
  164. EXPECT_EQ (true, ( std::is_constructible<span<int, 0>, zero_array_t&>::value));
  165. EXPECT_EQ (true, (!std::is_constructible<span<int, 0>, const zero_array_t&>::value));
  166. EXPECT_EQ (true, ( std::is_constructible<span<const int, 0>, zero_array_t&>::value));
  167. EXPECT_EQ (true, ( std::is_constructible<span<const int, 0>, const zero_array_t&>::value));
  168. int_array_t arr = {1, 2, 3};
  169. // non-const, dynamic size
  170. span<int> s1{arr};
  171. EXPECT_EQ(s1.size(), 3UL);
  172. EXPECT_EQ(s1.data(), arr.data());
  173. EXPECT_EQ(s1.begin(), arr.data());
  174. EXPECT_EQ(s1.end(), arr.data() + 3);
  175. //const, dynamic size
  176. span<int const> s2{arr};
  177. EXPECT_EQ(s2.size(), 3UL);
  178. EXPECT_EQ(s2.data(), arr.data());
  179. EXPECT_EQ(s2.begin(), arr.data());
  180. EXPECT_EQ(s2.end(), arr.data() + 3);
  181. // non-const, static size
  182. span<int, 3> s3{arr};
  183. EXPECT_EQ(s3.size(), 3UL);
  184. EXPECT_EQ(s3.data(), arr.data());
  185. EXPECT_EQ(s3.begin(), arr.data());
  186. EXPECT_EQ(s3.end(), arr.data() + 3);
  187. // const, dynamic size
  188. span<int const, 3> s4{arr};
  189. EXPECT_EQ(s4.size(), 3UL);
  190. EXPECT_EQ(s4.data(), arr.data());
  191. EXPECT_EQ(s4.begin(), arr.data());
  192. EXPECT_EQ(s4.end(), arr.data() + 3);
  193. }
  194. // TEST (Tspan, containter_constructors) {
  195. // using container_t = tbx::deque<int, 3>;
  196. //
  197. // EXPECT_EQ (true, ( std::is_constructible<span<int>, container_t&>::value));
  198. // EXPECT_EQ (true, (!std::is_constructible<span<int>, const container_t&>::value));
  199. //
  200. // EXPECT_EQ (true, ( std::is_constructible<span<const int>, container_t&>::value));
  201. // EXPECT_EQ (true, ( std::is_constructible<span<const int>, const container_t&>::value));
  202. //
  203. // EXPECT_EQ (true, (!std::is_constructible<span<int, 3>, container_t&>::value));
  204. // EXPECT_EQ (true, (!std::is_constructible<span<int, 3>, const container_t&>::value));
  205. //
  206. // EXPECT_EQ (true, (!std::is_constructible<span<const int, 3>, container_t&>::value));
  207. // EXPECT_EQ (true, (!std::is_constructible<span<const int, 3>, const container_t&>::value));
  208. //
  209. // container_t cont = {1, 2, 3};
  210. // const container_t ccont = {1, 2, 3};
  211. //
  212. // // non-const, dynamic size
  213. // span<int> s1(cont);
  214. // EXPECT_EQ(s1.size(), 3UL);
  215. // EXPECT_EQ(s1.data(), cont.data());
  216. // EXPECT_EQ(s1.begin(), cont.data());
  217. // EXPECT_EQ(s1.end(), cont.data() + 3);
  218. //
  219. //
  220. // //const, dynamic size
  221. // span<int const> s2(cont);
  222. // EXPECT_EQ(s2.size(), 3UL);
  223. // EXPECT_EQ(s2.data(), cont.data());
  224. // EXPECT_EQ(s2.begin(), cont.data());
  225. // EXPECT_EQ(s2.end(), cont.data() + 3);
  226. //
  227. // }
  228. }