test-types.C 6.1 KB

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