tuple_utils.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. // TODO: need a test file per header in tuple_utils and include the header first, to detect potentially missing includes
  2. #include "simple/support/tuple_utils.hpp"
  3. #include "simple/support/function_utils.hpp"
  4. #include <cassert>
  5. #include <string>
  6. #include <sstream>
  7. #include <array>
  8. using namespace simple::support;
  9. using namespace std::literals;
  10. struct one_t { static constexpr int value = 1; } one;
  11. struct five_t { static constexpr int value = 5; } five;
  12. struct seventy_seven_t { static constexpr int value = 77; } seventy_seven;
  13. void ApplyFor()
  14. {
  15. {
  16. auto t = std::tuple("minus one ", 0," one ",2," three");
  17. std::stringstream ss;
  18. apply_for<std::is_arithmetic>(true, [&ss](auto&& a)
  19. {
  20. ss << a;
  21. }, t);
  22. assert(ss.str() == "0");
  23. }
  24. {
  25. auto t = std::tuple(seventy_seven, five, one);
  26. auto and_nothing_else = [](auto){ assert(false); };
  27. apply_for(5, overload{ [](five_t a) { assert(a.value == 5); }, and_nothing_else }, t);
  28. apply_for(77, overload{ [](seventy_seven_t a) { assert(a.value == 77); }, and_nothing_else }, t);
  29. apply_for(1, overload{ [](one_t a) { assert(a.value == 1); }, and_nothing_else }, t);
  30. assert( apply_for(5, [](auto&& a) { return a.value + 1; }, t) == 6 );
  31. }
  32. }
  33. void ApplyTo()
  34. {
  35. auto t = std::tuple(0," one ",2," three");
  36. std::stringstream ss;
  37. apply_to({0,4}, [&ss](auto&& a)
  38. {
  39. ss << a;
  40. }, t);
  41. assert(ss.str() == "0 one 2 three");
  42. ss.str("");
  43. ss.clear();
  44. apply_to({1,3}, [&ss](auto&& a)
  45. {
  46. ss << a;
  47. }, t);
  48. assert(ss.str() == " one 2");
  49. ss.str("");
  50. ss.clear();
  51. apply_to(3, [&ss](auto&& a)
  52. {
  53. ss << a;
  54. }, t);
  55. assert(ss.str() == " three");
  56. ss.str("");
  57. ss.clear();
  58. auto t2 = std::tuple(0,1.1,2,3);
  59. apply_to(2, [](auto&& x)
  60. { x += 2; }, t2) ;
  61. assert( std::tuple(0,1.1,4,3) == t2 );
  62. apply_to({1, 3}, [](auto&& x)
  63. { x += 2; }, t2) ;
  64. assert( std::tuple(0,3.1,6,3) == t2 );
  65. auto t3 = std::tuple(0,1,2,3);
  66. assert( 4 == apply_to(2, [](auto&& x)
  67. { return x + 2; }, t3) );
  68. }
  69. template <typename... T>
  70. constexpr auto tuple_tie(std::tuple<T...>& t)
  71. {
  72. return std::apply(std::tie<T...>, t);
  73. }
  74. template <typename... T>
  75. constexpr auto tuple_tie_for_ubsan(std::tuple<T...>& t)
  76. {
  77. return std::apply(&std::tie<T...>, t);
  78. }
  79. void CarCdr()
  80. {
  81. { auto t = std::tuple(true, 1.5, 5);
  82. assert(tuple_car(t));
  83. assert(1.5 == tuple_car(tuple_cdr(t)));
  84. }
  85. { auto t = std::tuple(1, 2, 3);
  86. // auto tref = std::tuple<int&, int&, int&>(
  87. // std::get<0>(t),
  88. // std::get<1>(t),
  89. // std::get<2>(t));
  90. // // ubsan: ok
  91. // auto tref = std::apply([](auto&&... x) { return std::tie(x...); }, t);
  92. // // ubsan: ok
  93. // auto tref = tuple_tie_for_ubsan(t);
  94. // // ubsan: ok
  95. auto tref = tuple_tie(t);
  96. // ubsan:
  97. // tuple_utils.cpp:62:37: runtime error: reference binding to misaligned address 0x00022897 for type '<unknown>', which requires 2 byte alignment
  98. // /usr/include/c++/7/tuple:1673:21: runtime error: reference binding to misaligned address 0x00022897 for type 'type', which requires 2 byte alignment
  99. // /usr/include/c++/7/bits/move.h:74:36: runtime error: reference binding to misaligned address 0x00022897 for type '<unknown>', which requires 2 byte alignment
  100. // /usr/include/c++/7/tuple:1663:51: runtime error: reference binding to misaligned address 0x00022897 for type 'type', which requires 2 byte alignment
  101. // /usr/include/c++/7/bits/invoke.h:96:36: runtime error: reference binding to misaligned address 0x00022897 for type 'type', which requires 2 byte alignment
  102. // /usr/include/c++/7/bits/invoke.h:60:31: runtime error: reference binding to misaligned address 0x00022897 for type 'type', which requires 2 byte alignment
  103. // happens on:
  104. // Linux localhost 3.0.101-g1a2acd3 #1 SMP PREEMPT Wed Mar 23 04:06:52 PDT 2016 armv7l armv7l armv7l GNU/Linux
  105. // g++-7 (Ubuntu/Linaro 7.4.0-1ubuntu1~14.04~ppa1) 7.4.0
  106. // or
  107. // g++-9 (Ubuntu 9.3.0-11ubuntu0~14.04) 9.3.0
  108. assert(t == tref);
  109. tuple_car(tref) = -4;
  110. assert(tuple_car(t) == -4);
  111. assert(t == tref);
  112. auto tref_cdr = tuple_tie_cdr(tref);
  113. tuple_car(tref_cdr) = 13;
  114. assert(std::get<1>(t) == 13);
  115. assert(tref_cdr == std::tuple(13,3));
  116. auto tref_cdr2 = tuple_tie_cdr(t);
  117. tuple_car(tref_cdr2) = 12;
  118. assert(std::get<1>(t) == 12);
  119. assert(tref_cdr2 == tref_cdr);
  120. assert(tref_cdr2 == std::tuple(12,3));
  121. assert(tref_cdr == std::tuple(12,3));
  122. auto tref_cdr3 = tuple_tie_cdr(tref_cdr2);
  123. tuple_car(tref_cdr3) = 99;
  124. assert(std::get<2>(t) == 99);
  125. assert(tref_cdr3 == std::tuple(99));
  126. auto tref_cdr4 = tuple_cdr(tref_cdr2);
  127. tuple_car(tref_cdr4) = 88;
  128. assert(std::get<2>(t) != 88);
  129. assert(tref_cdr4 == std::tuple(88));
  130. using short_int_long_nil = tuple_cdr_t<
  131. std::tuple<char, short, int, long>>;
  132. static_assert(std::is_same_v<
  133. short_int_long_nil, std::tuple<short, int, long>>);
  134. static_assert(std::is_same_v<
  135. tuple_car_t<short_int_long_nil>, short>);
  136. using int_long_nil = tuple_cdr_t<short_int_long_nil>;
  137. static_assert(std::is_same_v<
  138. int_long_nil, std::tuple<int, long>>);
  139. static_assert(std::is_same_v<
  140. tuple_car_t<int_long_nil>, int>);
  141. using long_nil = tuple_cdr_t<int_long_nil>;
  142. static_assert(std::is_same_v<
  143. long_nil, std::tuple<long>>);
  144. static_assert(std::is_same_v<
  145. tuple_car_t<long_nil>, long>);
  146. using nil = tuple_cdr_t<long_nil>;
  147. static_assert(std::is_same_v<
  148. nil, std::tuple<>>);
  149. }
  150. }
  151. void TransformBasic()
  152. {
  153. assert
  154. ((
  155. transform([](auto x) { return x + x; },
  156. std::tuple{1, "2"s, 3.4})
  157. == std::tuple{2, "22"s, 6.8}
  158. ));
  159. assert
  160. ((
  161. transform(std::plus<>{},
  162. std::tuple{1, "2"s, 3.4}, std::tuple{4, "3"s, 2.1})
  163. == std::tuple{5, "23"s, 5.5}
  164. ));
  165. assert
  166. ((
  167. transform(std::plus<>{},
  168. std::array{1, 2, 3}, std::tuple{3, 2, 1})
  169. == std::tuple{4, 4, 4}
  170. ));
  171. assert
  172. ((
  173. transform(std::plus<>(),
  174. std::tuple{1, 2, 3.3}, std::array{3, 2, 1})
  175. == std::tuple{4, 4, 4.3}
  176. ));
  177. assert
  178. ((
  179. transform(std::plus<>{},
  180. std::tuple{1, 2, 3.3, 4, 5}, std::array{3, 2, 1})
  181. == std::tuple{4, 4, 4.3}
  182. ));
  183. assert
  184. ((
  185. transform
  186. (
  187. [](auto... x) { return (x + ...); },
  188. std::tuple{"1"s, 2},
  189. std::tuple{"3"s, 4},
  190. std::tuple{"5"s, 6}
  191. )
  192. == std::tuple{"135"s, 12}
  193. ));
  194. auto longstr = "wow am so stateful look at me ~ "s;
  195. assert
  196. ((
  197. transform(
  198. overload
  199. {
  200. [state = longstr]
  201. (auto x) { return state.size() + x; },
  202. [state = longstr]
  203. (std::string x) { return state + x;}
  204. },
  205. std::tuple{1, "2"s, 3.4})
  206. == std::tuple
  207. {
  208. longstr.size() + 1,
  209. longstr + "2"s,
  210. longstr.size() + 3.4
  211. }
  212. ));
  213. }
  214. struct counter
  215. {
  216. static size_t inst;
  217. static size_t copy;
  218. static size_t move;
  219. counter() { ++inst; };
  220. counter(const counter&) { ++copy; }
  221. counter(counter&&) { ++move; }
  222. };
  223. size_t counter::inst = 0;
  224. size_t counter::copy = 0;
  225. size_t counter::move = 0;
  226. void TransformCopyCount()
  227. {
  228. auto x = transform([](auto&& x) -> decltype(auto) { return std::forward<decltype(x)>(x); },
  229. std::tuple{counter{}, counter{}, counter{}} );
  230. assert(3 == get<0>(x).inst);
  231. assert(0 == get<1>(x).copy);
  232. assert(6 == get<2>(x).move);
  233. assert(3 == counter::inst);
  234. assert(0 == counter::copy);
  235. assert(6 == counter::move);
  236. }
  237. void TransformReturnVoid()
  238. {
  239. assert
  240. ((
  241. transform([](auto...){},
  242. std::tuple{1, 2, 3.3}, std::array{3, 2, 1})
  243. == std::tuple{tuple_void, tuple_void, tuple_void}
  244. ));
  245. assert
  246. ((
  247. transform
  248. (
  249. overload
  250. {
  251. [](std::string, int){},
  252. std::plus<>{},
  253. },
  254. std::tuple{1, "2"s, "3"s},
  255. std::tuple{3, 2, "1"s}
  256. )
  257. == std::tuple{4, tuple_void, "31"s}
  258. ));
  259. }
  260. void TransformInPlace()
  261. {
  262. {
  263. const char * ptr = "ptr";
  264. std::tuple t{1,ptr,3.0};
  265. transform([](auto&& x) { ++x; }, t);
  266. assert(( t == std::tuple{2, ptr + 1, 4.0} ));
  267. }
  268. {
  269. const char * ptr = "ptr";
  270. std::tuple t{1,ptr,3.0};
  271. assert(( transform([](auto&& x) { return x++; }, t) == std::tuple{1, ptr, 3.0} ));
  272. assert(( t == std::tuple{2, ptr + 1, 4.0} ));
  273. }
  274. }
  275. template <size_t I, size_t S>
  276. constexpr iteration_state<I,std::make_index_sequence<S>> i_state{};
  277. struct
  278. {
  279. template <size_t Index, typename Range, typename... Values>
  280. auto operator()(iteration_state<Index,Range>, Values... v)
  281. {
  282. return (Range::size() - Index) * (v + ...);
  283. }
  284. } sum_mul_sub_index;
  285. void TransformIterationState()
  286. {
  287. assert
  288. ((
  289. transform([](auto i, auto, auto) { return i; },
  290. std::tuple{1, 2, 3.3}, std::array{3, 2, 1})
  291. == std::tuple{i_state<0,3>, i_state<1,3>, i_state<2,3>}
  292. ));
  293. assert
  294. ((
  295. transform([](auto i, auto, auto) { return i; },
  296. std::tuple{1, 2, 3.3, 4, 5}, std::array{3, 2, 1})
  297. == std::tuple{i_state<0,3>, i_state<1,3>, i_state<2,3>}
  298. ));
  299. assert
  300. ((
  301. transform([](auto i, auto x, auto y) { return i.index() + x + y; },
  302. std::tuple{1, 2, 3.3}, std::array{3, 2, 1})
  303. == std::tuple{4, 5, 6.3}
  304. ));
  305. assert
  306. ((
  307. transform(sum_mul_sub_index,
  308. std::tuple{1, 2, 3.3}, std::array{3, 2, 1, 0, -1, -2})
  309. == std::tuple{4*3,4*2,4.3*1}
  310. ));
  311. assert
  312. ((
  313. transform([](auto i, auto x, auto y) { return (i.range.size() - i.index()) * (x + y); },
  314. std::tuple{1, 2, 3.3}, std::array{3, 2, 1})
  315. == std::tuple{4*3,4*2,4.3*1}
  316. ));
  317. assert
  318. ((
  319. transform([](auto i, auto x, auto y)
  320. {
  321. if constexpr (i.index() % 2)
  322. return x + y;
  323. else
  324. return x - y;
  325. },
  326. std::tuple{1, 2, 3.3}, std::array{3, 2, 1})
  327. == std::tuple{-2, 4, 2.3}
  328. ));
  329. }
  330. struct unlike_tuple {};
  331. template <typename F>
  332. std::string transform(F&&, const unlike_tuple&)
  333. {return "Don't be too greedy!"; }
  334. struct like_tuple
  335. {
  336. int a;
  337. std::string b;
  338. bool operator==(const like_tuple& other) const
  339. { return a == other.a && b == other.b; }
  340. };
  341. template <>
  342. struct std::tuple_size<like_tuple> : public std::integral_constant<size_t, 2> {};
  343. template <size_t I, typename T,
  344. std::enable_if_t<std::is_same_v<std::decay_t<T>,like_tuple>>* = nullptr>
  345. decltype(auto) get(T&& t) noexcept
  346. {
  347. static_assert(I < 2);
  348. if constexpr (I == 0)
  349. {
  350. return std::forward<T>(t).a;
  351. }
  352. else
  353. {
  354. return std::forward<T>(t).b;
  355. }
  356. }
  357. void TransformNonTuple()
  358. {
  359. assert
  360. ((
  361. from_tuple<like_tuple>(transform(
  362. std::plus<>{}, like_tuple{1,"1"s}, std::tuple{2,"2"s} ))
  363. ==
  364. like_tuple{3, "12"s}
  365. ));
  366. assert( transform([](){ return "Too greedy!"; }, unlike_tuple{}) == "Don't be too greedy!" );
  367. }
  368. int main()
  369. {
  370. ApplyFor();
  371. ApplyTo();
  372. CarCdr();
  373. TransformBasic();
  374. TransformCopyCount();
  375. TransformReturnVoid();
  376. TransformInPlace();
  377. TransformIterationState();
  378. TransformNonTuple();
  379. return 0;
  380. }