types.cc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file types.cc
  3. /// @brief Show what types conform to what type predicates.
  4. // (c) Daniel Llorens - 2016-2017
  5. // This library is free software; you can redistribute it and/or modify it under
  6. // the terms of the GNU Lesser General Public License as published by the Free
  7. // Software Foundation; either version 3 of the License, or (at your option) any
  8. // later version.
  9. #include <iostream>
  10. #include <iterator>
  11. #include <numeric>
  12. #include "ra/test.hh"
  13. #include "ra/ra.hh"
  14. #include <typeinfo>
  15. #include <string>
  16. #include <chrono>
  17. using std::cout, std::endl, std::flush, ra::TestRecorder;
  18. #define TEST_PREDICATES(A) \
  19. [&tr](bool ra, bool slice, bool array_iterator, bool scalar, bool foreign_vector) \
  20. { \
  21. tr.info(STRINGIZE(A)).info("ra").test_eq(ra, ra::is_ra<A>); \
  22. tr.info(STRINGIZE(A)).info("slice").test_eq(slice, ra::is_slice<A>); \
  23. tr.info(STRINGIZE(A)).info("RaIterator").test_eq(array_iterator, ra::RaIterator<A>); \
  24. tr.info(STRINGIZE(A)).info("scalar").test_eq(scalar, ra::is_scalar<A>); \
  25. tr.info(STRINGIZE(A)).info("foreign_vector").test_eq(foreign_vector, ra::is_foreign_vector<A>); \
  26. }
  27. int main()
  28. {
  29. TestRecorder tr(std::cout, TestRecorder::NOISY);
  30. {
  31. using T = std::chrono::duration<long int, std::ratio<1, 1000000000>>;
  32. ra::Big<T, 1> a;
  33. static_assert(std::is_same_v<ra::value_t<decltype(a)>, T>);
  34. }
  35. {
  36. // SmallBase is excluded in is_slice by ra_traits TODO
  37. TEST_PREDICATES(decltype(std::declval<ra::SmallBase<ra::SmallArray, ra::Dim, mp::int_list<3>, mp::int_list<1>>>()))
  38. (false, false, false, false, false);
  39. TEST_PREDICATES(int)
  40. (false, false, false, true, false);
  41. TEST_PREDICATES(std::complex<double>)
  42. (false, false, false, true, false);
  43. TEST_PREDICATES(decltype(ra::Unique<int, 2>()))
  44. (true, true, false, false, false);
  45. TEST_PREDICATES(decltype(ra::Big<int, 2>()))
  46. (true, true, false, false, false);
  47. TEST_PREDICATES(decltype(ra::View<int *, 2>()))
  48. (true, true, false, false, false);
  49. TEST_PREDICATES(decltype(ra::Unique<int, 2>().iter()))
  50. (true, false, true, false, false);
  51. static_assert(ra::RaIterator<decltype(ra::Unique<int, 2>().iter())>);
  52. {
  53. ra::Unique<int, 1> A = {1, 2, 3};
  54. auto i = A.iter();
  55. auto & ii = i;
  56. TEST_PREDICATES(decltype(ii))
  57. (true, false, true, false, false);
  58. }
  59. TEST_PREDICATES(ra::Iota<int>)
  60. (true, false, true, false, false);
  61. TEST_PREDICATES(ra::TensorIndex<0>)
  62. (true, false, true, false, false);
  63. // FIXME in type.hh, prevents replacing is_iterator by RaIterator. Perhaps an additional concept is needed.
  64. TEST_PREDICATES(ra::TensorIndex<0> const)
  65. (true, false, false, false, false);
  66. TEST_PREDICATES(ra::TensorIndex<0> &)
  67. (true, false, true, false, false);
  68. TEST_PREDICATES(decltype(ra::Small<int, 2>()))
  69. (true, true, false, false, false);
  70. TEST_PREDICATES(decltype(ra::Small<int, 2>().iter()))
  71. (true, false, true, false, false);
  72. TEST_PREDICATES(decltype(ra::Small<int, 2, 2>()()))
  73. (true, true, false, false, false);
  74. TEST_PREDICATES(decltype(ra::Small<int, 2, 2>().iter()))
  75. (true, false, true, false, false);
  76. TEST_PREDICATES(decltype(ra::Small<int, 2>()+3))
  77. (true, false, true, false, false);
  78. TEST_PREDICATES(decltype(3+ra::Big<int>()))
  79. (true, false, true, false, false);
  80. TEST_PREDICATES(std::vector<int>)
  81. (false, false, false, false, true);
  82. TEST_PREDICATES(decltype(ra::start(std::vector<int> {})))
  83. (true, false, true, false, false);
  84. TEST_PREDICATES(int *)
  85. (false, false, false, false, false);
  86. }
  87. tr.section("establish meaning of selectors (TODO / convert to TestRecorder)");
  88. {
  89. static_assert(ra::is_slice<ra::View<int *, 0>>);
  90. static_assert(ra::is_slice<ra::View<int *, 2>>);
  91. static_assert(ra::is_slice<ra::SmallView<int, mp::int_list<>, mp::int_list<>>>);
  92. static_assert(ra::is_ra<ra::Small<int>>, "bad is_ra Small");
  93. static_assert(ra::is_ra<ra::SmallView<int, mp::nil, mp::nil>>, "bad is_ra SmallView");
  94. static_assert(ra::is_ra<ra::Unique<int, 0>>, "bad is_ra Unique");
  95. static_assert(ra::is_ra<ra::View<int *, 0>>, "bad is_ra View");
  96. static_assert(ra::is_ra<ra::Small<int, 1>>, "bad is_ra Small");
  97. static_assert(ra::is_ra<ra::SmallView<int, mp::int_list<1>, mp::int_list<1>>>, "bad is_ra SmallView");
  98. static_assert(ra::is_ra<ra::Unique<int, 1>>, "bad is_ra Unique");
  99. static_assert(ra::is_ra<ra::View<int *, 1>>, "bad is_ra View");
  100. static_assert(ra::is_ra<ra::View<int *>>, "bad is_ra View");
  101. using Scalar = decltype(ra::scalar(3));
  102. using Vector = decltype(ra::start({1, 2, 3}));
  103. static_assert(ra::is_ra_scalar<Scalar>, "bad is_ra_scalar Scalar");
  104. static_assert(ra::is_ra<decltype(ra::scalar(3))>, "bad is_ra Scalar");
  105. static_assert(ra::is_ra<Vector>, "bad is_ra Vector");
  106. static_assert(!ra::is_ra<int *>, "bad is_ra int *");
  107. static_assert(ra::is_scalar<double>, "bad is_scalar real");
  108. static_assert(ra::is_scalar<std::complex<double>>, "bad is_scalar complex");
  109. static_assert(ra::is_scalar<int>, "bad is_scalar int");
  110. static_assert(!ra::is_scalar<decltype(ra::scalar(3))>, "bad is_scalar Scalar");
  111. static_assert(!ra::is_scalar<Vector>, "bad is_scalar Scalar");
  112. static_assert(!ra::is_scalar<decltype(ra::start(3))>, "bad is_scalar Scalar");
  113. int a = 3;
  114. static_assert(!ra::is_scalar<decltype(ra::start(a))>, "bad is_scalar Scalar");
  115. // a regression.
  116. static_assert(ra::is_ra_zero_rank<ra::Scalar<int>>, "bad");
  117. static_assert(!ra::is_ra_pos_rank<ra::Scalar<int>>, "bad");
  118. static_assert(!ra::is_ra_zero_rank<ra::TensorIndex<0>>, "bad");
  119. static_assert(ra::is_ra_pos_rank<ra::TensorIndex<0>>, "bad");
  120. static_assert(!ra::ra_zero<ra::TensorIndex<0>>, "bad");
  121. static_assert(ra::is_ra_pos_rank<ra::Expr<ra::plus, std::tuple<ra::TensorIndex<0, int>, ra::Scalar<int>>>>, "bad");
  122. static_assert(ra::is_ra_pos_rank<ra::Pick<std::tuple<Vector, Vector, Vector>>>, "bad");
  123. }
  124. tr.section("builtin arrays I");
  125. {
  126. int const a[] = {1, 2, 3};
  127. int b[] = {1, 2, 3};
  128. int c[][2] = {{1, 2}, {4, 5}};
  129. static_assert(ra::is_builtin_array<decltype(a) &>);
  130. static_assert(ra::is_builtin_array<decltype(a)>);
  131. static_assert(ra::is_builtin_array<decltype(b) &>);
  132. static_assert(ra::is_builtin_array<decltype(b)>);
  133. static_assert(ra::is_builtin_array<decltype(c) &>);
  134. static_assert(ra::is_builtin_array<decltype(c)>);
  135. static_assert(requires { ra::ra_traits<decltype(a)>::size(a); });
  136. static_assert(requires { ra::ra_traits<decltype(b)>::size(b); });
  137. static_assert(requires { ra::ra_traits<decltype(c)>::size(c); });
  138. }
  139. tr.section("adaptors");
  140. {
  141. tr.test_eq(2, size_s(ra::start(std::array<int, 2> { 1, 2 })));
  142. tr.test_eq(2, ra::size_s(std::array<int, 2> { 1, 2 }));
  143. tr.test_eq(ra::DIM_ANY, size_s(ra::start(std::vector<int> { 1, 2, 3})));
  144. tr.test_eq(ra::DIM_ANY, ra::size_s(std::vector<int> { 1, 2, 3}));
  145. tr.test_eq(2, ra::start(std::array<int, 2> { 1, 2 }).size_s(0));
  146. tr.test_eq(ra::DIM_ANY, ra::start(std::vector<int> { 1, 2, 3 }).size_s(0));
  147. tr.test_eq(1, ra::start(std::array<int, 2> { 1, 2 }).rank_s());
  148. tr.test_eq(1, ra::start(std::vector<int> { 1, 2, 3 }).rank_s());
  149. tr.test_eq(1, ra::start(std::array<int, 2> { 1, 2 }).rank());
  150. tr.test_eq(1, ra::start(std::vector<int> { 1, 2, 3 }).rank());
  151. }
  152. return tr.summary();
  153. }