tuples.cc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/test - Test type list library based on tuples.
  3. // (c) Daniel Llorens - 2010
  4. // This library is free software; you can redistribute it and/or modify it under
  5. // the terms of the GNU Lesser General Public License as published by the Free
  6. // Software Foundation; either version 3 of the License, or (at your option) any
  7. // later version.
  8. #include <iostream>
  9. #include "ra/test.hh"
  10. #include "mpdebug.hh"
  11. using std::tuple, std::tuple_element, std::is_same_v;
  12. using std::cout, std::endl, ra::TestRecorder;
  13. using ra::int_c, ra::mp::ref, ra::mp::int_list;
  14. template <class A>
  15. struct Inc1
  16. {
  17. using type = int_c<A::value+1>;
  18. };
  19. template <class A, class B>
  20. struct Sum2
  21. {
  22. using type = int_c<A::value+B::value>;
  23. };
  24. template <class ... A> struct SumV;
  25. template <class A0, class ... A>
  26. struct SumV<A0, A ...>
  27. {
  28. constexpr static int value = A0::value + SumV<A ...>::value;
  29. };
  30. template <>
  31. struct SumV<>
  32. {
  33. constexpr static int value = 0;
  34. };
  35. template <class A>
  36. struct SamePP
  37. {
  38. template <class B>
  39. struct type
  40. {
  41. constexpr static bool value = is_same_v<A, B>;
  42. };
  43. };
  44. struct True
  45. {
  46. constexpr static bool value = true;
  47. };
  48. int main()
  49. {
  50. TestRecorder tr(std::cout);
  51. // Booleans.
  52. {
  53. static_assert(True::value, "bad True");
  54. }
  55. // Map
  56. {
  57. using A = int_list<5, 6, 3>;
  58. using B = int_list<2, 3, -1>;
  59. static_assert(ra::mp::check_idx<ra::mp::map<ra::mp::sum, A>, 5, 6, 3>::value, "");
  60. static_assert(ra::mp::check_idx<ra::mp::map<ra::mp::sum, A, B>, 7, 9, 2>::value, "");
  61. }
  62. // fold.
  63. {
  64. using A = int_list<5, 6, 3>;
  65. using B = int_list<2, 3, -1>;
  66. static_assert(ra::mp::fold<ra::mp::sum, int_c<1>, A>::value==15, "");
  67. static_assert(ra::mp::fold<ra::mp::sum, int_c<3>, B>::value==7, "");
  68. static_assert(ra::mp::fold<ra::mp::max, int_c<4>, A>::value==6, "");
  69. static_assert(ra::mp::fold<ra::mp::max, void, A>::value==6, "");
  70. static_assert(ra::mp::fold<ra::mp::max, int_c<9>, A>::value==9, "");
  71. static_assert(ra::mp::fold<ra::mp::min, int_c<4>, A>::value==3, "");
  72. static_assert(ra::mp::fold<ra::mp::min, void, A>::value==3, "");
  73. static_assert(ra::mp::fold<ra::mp::min, int_c<1>, A>::value==1, "");
  74. }
  75. // Reductions.
  76. {
  77. using list_ = int_list<>;
  78. using list_1 = int_list<1>;
  79. using list_0 = int_list<0>;
  80. using list_10 = int_list<1, 0>;
  81. using list_01 = int_list<0, 1>;
  82. using list_11 = int_list<1, 1>;
  83. using list_00 = int_list<0, 0>;
  84. static_assert(ra::mp::apply<ra::mp::andb, list_>::value, "bad And");
  85. static_assert(ra::mp::apply<ra::mp::andb, list_1>::value, "bad And");
  86. static_assert(!ra::mp::apply<ra::mp::andb, list_0>::value, "bad And");
  87. static_assert(!ra::mp::apply<ra::mp::andb, list_10>::value, "bad And");
  88. static_assert(!ra::mp::apply<ra::mp::andb, list_01>::value, "bad And");
  89. static_assert(ra::mp::apply<ra::mp::andb, list_11>::value, "bad And");
  90. static_assert(!ra::mp::apply<ra::mp::andb, list_00>::value, "bad And");
  91. static_assert(!ra::mp::apply<ra::mp::orb, list_>::value, "bad Or");
  92. static_assert(ra::mp::apply<ra::mp::orb, list_1>::value, "bad Or");
  93. static_assert(!ra::mp::apply<ra::mp::orb, list_0>::value, "bad Or");
  94. static_assert(ra::mp::apply<ra::mp::orb, list_10>::value, "bad Or");
  95. static_assert(ra::mp::apply<ra::mp::orb, list_01>::value, "bad Or");
  96. static_assert(ra::mp::apply<ra::mp::orb, list_11>::value, "bad Or");
  97. static_assert(!ra::mp::apply<ra::mp::orb, list_00>::value, "bad Or");
  98. static_assert(ra::mp::apply<ra::mp::sum, list_>::value==0, "bad Sum");
  99. static_assert(ra::mp::apply<ra::mp::sum, int_list<2, 4>>::value==6, "bad Sum");
  100. static_assert(ra::mp::apply<ra::mp::prod, list_>::value==1, "bad Prod");
  101. static_assert(ra::mp::apply<ra::mp::prod, int_list<2, 3>>::value==6, "bad Prod");
  102. }
  103. // append.
  104. using A = int_list<0, 2, 3>;
  105. using B = int_list<5, 6, 7>;
  106. using C = int_list<9, 8>;
  107. static_assert(is_same_v<int_list<>, ra::mp::nil>, "");
  108. static_assert(is_same_v<C, tuple<int_c<9>, int_c<8>>>, "");
  109. using O = ra::mp::nil;
  110. using A_B = ra::mp::append<A, B>;
  111. using A_C = ra::mp::append<A, C>;
  112. using C_B = ra::mp::append<C, B>;
  113. static_assert(ra::mp::check_idx<A_B, 0, 2, 3, 5, 6, 7>::value, "bad AB");
  114. static_assert(ra::mp::check_idx<A_C, 0, 2, 3, 9, 8>::value, "bad AB");
  115. static_assert(ra::mp::check_idx<C_B, 9, 8, 5, 6, 7>::value, "bad AB");
  116. static_assert(is_same_v<A, ra::mp::append<A, O>>, "bad A+empty");
  117. static_assert(is_same_v<A, ra::mp::append<O, A>>, "bad empty+A");
  118. // mp::iota.
  119. static_assert(ra::mp::check_idx<ra::mp::iota<4, 0>, 0, 1, 2, 3>::value, "0a");
  120. static_assert(ra::mp::check_idx<ra::mp::iota<4, 3>, 3, 4, 5, 6>::value, "0b");
  121. static_assert(ra::mp::check_idx<ra::mp::iota<0, 3>>::value, "0c");
  122. static_assert(ra::mp::check_idx<ra::mp::iota<1, 3>, 3>::value, "0d");
  123. static_assert(ra::mp::check_idx<ra::mp::iota<3, -2>, -2, -1, 0>::value, "0e");
  124. static_assert(ra::mp::check_idx<ra::mp::iota<4, 3, -1>, 3, 2, 1, 0>::value, "0a");
  125. // makelist
  126. static_assert(ra::mp::check_idx<ra::mp::makelist<2, int_c<9>>, 9, 9>::value, "1a");
  127. static_assert(ra::mp::check_idx<ra::mp::makelist<0, int_c<9>>>::value, "1b");
  128. // ref
  129. static_assert(ref<tuple<A, B, C>, 0, 0>::value==0, "3a");
  130. static_assert(ref<tuple<A, B, C>, 0, 1>::value==2, "3b");
  131. static_assert(ref<tuple<A, B, C>, 0, 2>::value==3, "3c");
  132. static_assert(ref<tuple<A, B, C>, 1, 0>::value==5, "3d");
  133. static_assert(ref<tuple<A, B, C>, 1, 1>::value==6, "3e");
  134. static_assert(ref<tuple<A, B, C>, 1, 2>::value==7, "3f");
  135. static_assert(ref<tuple<A, B, C>, 2, 0>::value==9, "3g");
  136. static_assert(ref<tuple<A, B, C>, 2, 1>::value==8, "3h");
  137. static_assert(ra::mp::first<B>::value==5 && ra::mp::first<C>::value==9, "3i");
  138. static_assert(ra::mp::last<B>::value==7 && ra::mp::last<C>::value==8, "3j");
  139. // Useful default.
  140. static_assert(ra::mp::len<ref<ra::mp::nil>> ==0, "3i");
  141. // 3-indices.
  142. using S2AB = tuple<A, B>;
  143. using S2BC = tuple<B, C>;
  144. using S3 = tuple<S2AB, S2BC>;
  145. // in S2AB.
  146. static_assert(ref<S3, 0, 0, 0>::value==0, "3j");
  147. static_assert(ref<S3, 0, 0, 1>::value==2, "3k");
  148. static_assert(ref<S3, 0, 0, 2>::value==3, "3l");
  149. static_assert(ref<S3, 0, 1, 0>::value==5, "3m");
  150. static_assert(ref<S3, 0, 1, 1>::value==6, "3n");
  151. static_assert(ref<S3, 0, 1, 2>::value==7, "3o");
  152. // in S2BC.
  153. static_assert(ref<S3, 1, 0, 0>::value==5, "3p");
  154. static_assert(ref<S3, 1, 0, 1>::value==6, "3q");
  155. static_assert(ref<S3, 1, 0, 2>::value==7, "3r");
  156. static_assert(ref<S3, 1, 1, 0>::value==9, "3s");
  157. static_assert(ref<S3, 1, 1, 1>::value==8, "3t");
  158. // index.
  159. static_assert(ra::mp::index<A, int_c<0>>::value==0, "4a");
  160. static_assert(ra::mp::index<A, int_c<2>>::value==1, "4b");
  161. static_assert(ra::mp::index<A, int_c<3>>::value==2, "4c");
  162. static_assert(ra::mp::index<A, int_c<4>>::value==-1, "4d");
  163. static_assert(ra::mp::index<S3, S2BC>::value==1, "4e");
  164. // InvertIndex
  165. {
  166. using II0 = int_list<4, 6, 7, 1>;
  167. using II1 = ra::mp::InvertIndex<II0>;
  168. static_assert(is_same_v<int_list<-1, 3, -1, -1, 0, -1, 1, 2>, II1>);
  169. }
  170. {
  171. using II0 = int_list<3>;
  172. using II1 = ra::mp::InvertIndex<II0>;
  173. static_assert(is_same_v<int_list<-1, -1, -1, 0>, II1>);
  174. }
  175. {
  176. using II0 = int_list<>;
  177. using II1 = ra::mp::InvertIndex<II0>;
  178. static_assert(is_same_v<int_list<>, II1>);
  179. }
  180. // IndexIf.
  181. static_assert(ra::mp::IndexIf<A, SamePP<int_c<0>>::type>::value==0, "5a");
  182. static_assert(ra::mp::IndexIf<A, SamePP<int_c<2>>::type>::value==1, "5b");
  183. static_assert(ra::mp::IndexIf<A, SamePP<int_c<3>>::type>::value==2, "5c");
  184. static_assert(ra::mp::IndexIf<A, SamePP<int_c<9>>::type>::value==-1, "5d");
  185. // findtail.
  186. static_assert(is_same_v<ra::mp::findtail<A, int_c<0>>, A>, "4a");
  187. static_assert(ra::mp::check_idx<ra::mp::findtail<A, int_c<2>>, 2, 3>::value, "4b");
  188. static_assert(ra::mp::check_idx<ra::mp::findtail<A, int_c<3>>, 3>::value, "4c");
  189. static_assert(ra::mp::nilp<ra::mp::findtail<A, int_c<4>>>, "4d");
  190. static_assert(is_same_v<ra::mp::findtail<S3, S2BC>, tuple<S2BC>>, "4e");
  191. // reverse.
  192. static_assert(ra::mp::check_idx<ra::mp::reverse<A_B>, 7, 6, 5, 3, 2, 0>::value, "5a");
  193. static_assert(ra::mp::check_idx<ra::mp::reverse<O>>::value, "5b");
  194. static_assert(is_same_v<ra::mp::reverse<ra::mp::nil>, ra::mp::nil>, "bad reverse");
  195. // drop & take
  196. static_assert(ra::mp::check_idx<ra::mp::drop<A, 0>, 0, 2, 3>::value, "bad 6a");
  197. static_assert(ra::mp::check_idx<ra::mp::drop<A, 1>, 2, 3>::value, "bad 6b");
  198. static_assert(ra::mp::check_idx<ra::mp::drop<A, 2>, 3>::value, "bad 6c");
  199. static_assert(ra::mp::check_idx<ra::mp::drop<A, 3>>::value, "bad 6d");
  200. static_assert(ra::mp::check_idx<ra::mp::take<A, 0>>::value, "bad 6e");
  201. static_assert(ra::mp::check_idx<ra::mp::take<A, 1>, 0>::value, "bad 6f");
  202. static_assert(ra::mp::check_idx<ra::mp::take<A, 2>, 0, 2>::value, "bad 6g");
  203. static_assert(ra::mp::check_idx<ra::mp::take<A, 3>, 0, 2, 3>::value, "bad 6h");
  204. // complement.
  205. {
  206. using case1 = int_list<1>;
  207. static_assert(ra::mp::check_idx<ra::mp::complement<case1, 0>>::value, "");
  208. static_assert(ra::mp::check_idx<ra::mp::complement<case1, 1>, 0>::value, "");
  209. static_assert(ra::mp::check_idx<ra::mp::complement<case1, 2>, 0>::value, "");
  210. static_assert(ra::mp::check_idx<ra::mp::complement<case1, 3>, 0, 2>::value, "");
  211. using list3 = ra::mp::iota<3>;
  212. static_assert(ra::mp::check_idx<ra::mp::complement<list3, 3>>::value, "");
  213. using c36 = ra::mp::complement<list3, 6>;
  214. static_assert(ra::mp::check_idx<c36, 3, 4, 5>::value, "");
  215. static_assert(ra::mp::check_idx<ra::mp::complement<c36, 6>, 0, 1, 2>::value, "");
  216. using case0 = tuple<int_c<0>>;
  217. static_assert(ra::mp::check_idx<ra::mp::complement<case0, 0>>::value, "");
  218. static_assert(ra::mp::check_idx<ra::mp::complement<case0, 1>>::value, "");
  219. static_assert(ra::mp::check_idx<ra::mp::complement<case0, 2>, 1>::value, "");
  220. }
  221. // complement_sorted_list && complement_list.
  222. {
  223. #define _ ,
  224. #define CHECK_COMPLEMENT_SLIST( A, B, C ) \
  225. static_assert(ra::mp::check_idx<ra::mp::complement_sorted_list<int_list A , B > C >::value, "a");
  226. CHECK_COMPLEMENT_SLIST( <1>, int_list<>, )
  227. CHECK_COMPLEMENT_SLIST( <1>, int_list<0>, _ 0)
  228. CHECK_COMPLEMENT_SLIST( <1>, int_list<0 _ 1>, _ 0)
  229. CHECK_COMPLEMENT_SLIST( <1>, int_list<0 _ 1 _ 2>, _ 0 _ 2)
  230. using l2 = ra::mp::iota<2>;
  231. CHECK_COMPLEMENT_SLIST( <0>, l2, _ 1 )
  232. CHECK_COMPLEMENT_SLIST( <1>, l2, _ 0 )
  233. CHECK_COMPLEMENT_SLIST( <>, l2, _ 0 _ 1 )
  234. using l3 = ra::mp::iota<3>;
  235. CHECK_COMPLEMENT_SLIST( <0 _ 1>, l3, _ 2 )
  236. CHECK_COMPLEMENT_SLIST( <0 _ 2>, l3, _ 1 )
  237. CHECK_COMPLEMENT_SLIST( <1 _ 2>, l3, _ 0 )
  238. CHECK_COMPLEMENT_SLIST( <0>, l3, _ 1 _ 2 )
  239. CHECK_COMPLEMENT_SLIST( <1>, l3, _ 0 _ 2 )
  240. CHECK_COMPLEMENT_SLIST( <2>, l3, _ 0 _ 1 )
  241. CHECK_COMPLEMENT_SLIST( <>, l3, _ 0 _ 1 _ 2 )
  242. CHECK_COMPLEMENT_SLIST( <>, ra::mp::nil, )
  243. #undef CHECK_COMPLEMENT_SLIST
  244. #undef _
  245. #define _ ,
  246. #define CHECK_COMPLEMENT_LIST( A, B, C ) \
  247. static_assert(ra::mp::check_idx<ra::mp::complement_list<int_list A , B > C >::value, "a");
  248. using l2 = ra::mp::iota<2>;
  249. CHECK_COMPLEMENT_LIST( <0>, l2, _ 1 )
  250. CHECK_COMPLEMENT_LIST( <1>, l2, _ 0 )
  251. CHECK_COMPLEMENT_LIST( <>, l2, _ 0 _ 1 )
  252. using l3 = ra::mp::iota<3>;
  253. CHECK_COMPLEMENT_LIST( <0 _ 1>, l3, _ 2 )
  254. CHECK_COMPLEMENT_LIST( <0 _ 2>, l3, _ 1 )
  255. CHECK_COMPLEMENT_LIST( <1 _ 2>, l3, _ 0 )
  256. CHECK_COMPLEMENT_LIST( <0>, l3, _ 1 _ 2 )
  257. CHECK_COMPLEMENT_LIST( <1>, l3, _ 0 _ 2 )
  258. CHECK_COMPLEMENT_LIST( <2>, l3, _ 0 _ 1 )
  259. CHECK_COMPLEMENT_LIST( <>, l3, _ 0 _ 1 _ 2 )
  260. CHECK_COMPLEMENT_LIST( <>, ra::mp::nil, )
  261. // this must also work on unserted lists.
  262. CHECK_COMPLEMENT_LIST( <1 _ 0>, l3, _ 2 )
  263. CHECK_COMPLEMENT_LIST( <2 _ 1>, l3, _ 0 )
  264. CHECK_COMPLEMENT_LIST( <2 _ 0>, l3, _ 1 )
  265. using x3 = int_list<2, 0, 1>;
  266. CHECK_COMPLEMENT_LIST( <1 _ 0>, x3, _ 2 )
  267. CHECK_COMPLEMENT_LIST( <2 _ 0>, x3, _ 1 )
  268. CHECK_COMPLEMENT_LIST( <2 _ 1>, x3, _ 0 )
  269. #undef CHECK_COMPLEMENT_LIST
  270. #undef _
  271. }
  272. // MapCons.
  273. {
  274. using a = ra::mp::iota<2>;
  275. using b = ra::mp::iota<2, 1>;
  276. using mc = ra::mp::MapCons<int_c<9>, tuple<a, b>>::type;
  277. static_assert(ra::mp::check_idx<ref<mc, 0>, 9, 0, 1>::value, "a");
  278. static_assert(ra::mp::check_idx<ref<mc, 1>, 9, 1, 2>::value, "b");
  279. }
  280. // Combinations.
  281. {
  282. static_assert(ra::mp::len<ra::mp::combinations<ra::mp::nil, 0>> == 1, "");
  283. using l3 = ra::mp::iota<3>;
  284. using c31 = ra::mp::combinations<l3, 1>;
  285. using c32 = ra::mp::combinations<l3, 2>;
  286. static_assert(ra::mp::len<c31> == 3, "a");
  287. static_assert(ra::mp::check_idx<ref<c31, 0>, 0>::value, "a");
  288. static_assert(ra::mp::check_idx<ref<c31, 1>, 1>::value, "b");
  289. static_assert(ra::mp::check_idx<ref<c31, 2>, 2>::value, "c");
  290. static_assert(ra::mp::len<c32> == 3, "b");
  291. static_assert(ra::mp::check_idx<ref<c32, 0>, 0, 1>::value, "d");
  292. static_assert(ra::mp::check_idx<ref<c32, 1>, 0, 2>::value, "e");
  293. static_assert(ra::mp::check_idx<ref<c32, 2>, 1, 2>::value, "f");
  294. using l4 = ra::mp::iota<4>;
  295. using c40 = ra::mp::combinations<l4, 0>;
  296. using c41 = ra::mp::combinations<l4, 1>;
  297. using c42 = ra::mp::combinations<l4, 2>;
  298. using c43 = ra::mp::combinations<l4, 3>;
  299. using c44 = ra::mp::combinations<l4, 4>;
  300. static_assert(ra::mp::len<c40> == 1, "a");
  301. static_assert(ra::mp::check_idx<ref<c40, 0>>::value, "a");
  302. static_assert(ra::mp::len<c41> == 4, "b");
  303. static_assert(ra::mp::check_idx<ref<c41, 0>, 0>::value, "b");
  304. static_assert(ra::mp::check_idx<ref<c41, 1>, 1>::value, "b");
  305. static_assert(ra::mp::check_idx<ref<c41, 2>, 2>::value, "b");
  306. static_assert(ra::mp::check_idx<ref<c41, 3>, 3>::value, "b");
  307. static_assert(ra::mp::len<c42> == 6, "c");
  308. static_assert(ra::mp::check_idx<ref<c42, 0>, 0, 1>::value, "c");
  309. static_assert(ra::mp::check_idx<ref<c42, 1>, 0, 2>::value, "c");
  310. static_assert(ra::mp::check_idx<ref<c42, 2>, 0, 3>::value, "c");
  311. static_assert(ra::mp::check_idx<ref<c42, 3>, 1, 2>::value, "c");
  312. static_assert(ra::mp::check_idx<ref<c42, 4>, 1, 3>::value, "c");
  313. static_assert(ra::mp::check_idx<ref<c42, 5>, 2, 3>::value, "c");
  314. static_assert(ra::mp::len<c43> == 4, "d");
  315. static_assert(ra::mp::check_idx<ref<c43, 0>, 0, 1, 2>::value, "d");
  316. static_assert(ra::mp::check_idx<ref<c43, 1>, 0, 1, 3>::value, "d");
  317. static_assert(ra::mp::check_idx<ref<c43, 2>, 0, 2, 3>::value, "d");
  318. static_assert(ra::mp::check_idx<ref<c43, 3>, 1, 2, 3>::value, "d");
  319. static_assert(ra::mp::len<c44> == 1, "e");
  320. static_assert(ra::mp::check_idx<ref<c44, 0>, 0, 1, 2, 3>::value, "e");
  321. }
  322. // MapPrepend & ProductAppend.
  323. {
  324. using la = ra::mp::iota<3>;
  325. using ca = ra::mp::combinations<la, 1>;
  326. using lb = ra::mp::iota<3>;
  327. using cb = ra::mp::combinations<lb, 1>;
  328. using test0 = ra::mp::MapPrepend<ra::mp::nil, cb>::type;
  329. static_assert(is_same_v<test0, cb>, "");
  330. using test1 = ra::mp::MapPrepend<la, cb>::type;
  331. static_assert(ra::mp::len<test1> == int(ra::mp::len<cb>), "");
  332. static_assert(ra::mp::check_idx<ref<test1, 0>, 0, 1, 2, 0>::value, "");
  333. static_assert(ra::mp::check_idx<ref<test1, 1>, 0, 1, 2, 1>::value, "");
  334. static_assert(ra::mp::check_idx<ref<test1, 2>, 0, 1, 2, 2>::value, "");
  335. using test2 = ra::mp::ProductAppend<ca, cb>::type;
  336. static_assert(ra::mp::len<test2> == 9, "");
  337. static_assert(ra::mp::check_idx<ref<test2, 0>, 0, 0>::value, "");
  338. static_assert(ra::mp::check_idx<ref<test2, 1>, 0, 1>::value, "");
  339. static_assert(ra::mp::check_idx<ref<test2, 2>, 0, 2>::value, "");
  340. static_assert(ra::mp::check_idx<ref<test2, 3>, 1, 0>::value, "");
  341. static_assert(ra::mp::check_idx<ref<test2, 4>, 1, 1>::value, "");
  342. static_assert(ra::mp::check_idx<ref<test2, 5>, 1, 2>::value, "");
  343. static_assert(ra::mp::check_idx<ref<test2, 6>, 2, 0>::value, "");
  344. static_assert(ra::mp::check_idx<ref<test2, 7>, 2, 1>::value, "");
  345. static_assert(ra::mp::check_idx<ref<test2, 8>, 2, 2>::value, "");
  346. }
  347. // PermutationSign.
  348. {
  349. #define _ ,
  350. #define CHECK_PERM_SIGN( A, B, C ) \
  351. static_assert(ra::mp::PermutationSign<int_list A , int_list B >::value == C, "");
  352. CHECK_PERM_SIGN( <0 _ 1 _ 2>, <0 _ 1 _ 2>, +1 );
  353. CHECK_PERM_SIGN( <0 _ 1 _ 2>, <0 _ 2 _ 1>, -1 );
  354. CHECK_PERM_SIGN( <0 _ 1 _ 2>, <1 _ 2 _ 0>, +1 );
  355. CHECK_PERM_SIGN( <0 _ 1 _ 2>, <2 _ 1 _ 0>, -1 );
  356. CHECK_PERM_SIGN( <0 _ 1 _ 2>, <1 _ 0 _ 2>, -1 );
  357. CHECK_PERM_SIGN( <0 _ 1 _ 2>, <2 _ 0 _ 1>, +1 );
  358. // inverted.
  359. CHECK_PERM_SIGN( <0 _ 1 _ 2>, <0 _ 1 _ 2>, +1 );
  360. CHECK_PERM_SIGN( <0 _ 2 _ 1>, <0 _ 1 _ 2>, -1 );
  361. CHECK_PERM_SIGN( <1 _ 2 _ 0>, <0 _ 1 _ 2>, +1 );
  362. CHECK_PERM_SIGN( <2 _ 1 _ 0>, <0 _ 1 _ 2>, -1 );
  363. CHECK_PERM_SIGN( <1 _ 0 _ 2>, <0 _ 1 _ 2>, -1 );
  364. CHECK_PERM_SIGN( <2 _ 0 _ 1>, <0 _ 1 _ 2>, +1 );
  365. // other cases.
  366. CHECK_PERM_SIGN( <0>, <0>, +1 );
  367. CHECK_PERM_SIGN( <>, <>, +1 );
  368. CHECK_PERM_SIGN( <0>, <1>, 0 );
  369. CHECK_PERM_SIGN( <0>, <>, 0 );
  370. CHECK_PERM_SIGN( <>, <0>, 0 );
  371. #undef CHECK_PERM_SIGN
  372. #undef _
  373. }
  374. // inc
  375. {
  376. using l = int_list<7, 8, 2>;
  377. static_assert(ra::mp::check_idx<l, 7, 8, 2>::value, "bad");
  378. static_assert(ra::mp::check_idx<ra::mp::inc<l, 0>, 8, 8, 2>::value, "bad");
  379. static_assert(ra::mp::check_idx<ra::mp::inc<l, 1>, 7, 9, 2>::value, "bad");
  380. static_assert(ra::mp::check_idx<ra::mp::inc<l, 2>, 7, 8, 3>::value, "bad");
  381. }
  382. // Prod & Sum
  383. {
  384. using l = int_list<3, 5, 7>;
  385. static_assert(ra::mp::apply<ra::mp::prod, l>::value==105, "bad");
  386. static_assert(ra::mp::apply<ra::mp::sum, l>::value==15, "bad");
  387. }
  388. // tuples in dynamic context
  389. {
  390. using l = int_list<3, 4, 5>;
  391. tr.test_eq(0, ra::mp::int_list_index<l>(3));
  392. tr.test_eq(1, ra::mp::int_list_index<l>(4));
  393. tr.test_eq(2, ra::mp::int_list_index<l>(5));
  394. tr.test_eq(-1, ra::mp::int_list_index<l>(7));
  395. }
  396. return tr.summary();
  397. };