len.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ek/box - Special object len
  3. // (c) Daniel Llorens - 2023
  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 "ra/test.hh"
  9. #include "mpdebug.hh"
  10. #include <iomanip>
  11. #include <chrono>
  12. #include <span>
  13. using std::cout, std::endl, std::flush;
  14. namespace ra {
  15. template <auto A, auto B> constexpr auto operator-(ic_t<A> const &, ic_t<B> const &) { return ic<A-B>; } // (*)
  16. } // namespace ra
  17. int main()
  18. {
  19. ra::TestRecorder tr(std::cout);
  20. tr.section("type predicates");
  21. {
  22. tr.test(ra::IteratorConcept<ra::Len>);
  23. tr.test(!ra::is_scalar<ra::Len>);
  24. tr.test(ra::is_zero_or_scalar<ra::Len>);
  25. tr.test(!ra::is_ra_pos<ra::Len>);
  26. tr.test(ra::is_special<ra::Len>);
  27. tr.test(ra::tomap<ra::Len>);
  28. tr.test(!ra::toreduce<ra::Len>);
  29. tr.test(ra::tomap<ra::Len, ra::Len>);
  30. tr.test(!ra::toreduce<ra::Len, ra::Len>);
  31. tr.test(ra::tomap<decltype((ra::len + ra::len) + (ra::len + ra::len))>);
  32. tr.test(!ra::toreduce<decltype((ra::len + ra::len) + (ra::len + ra::len))>);
  33. tr.test(ra::is_zero_or_scalar<decltype((ra::len + ra::len) + (ra::len + ra::len))>);
  34. tr.test(ra::IteratorConcept<decltype((ra::len + ra::len) + (ra::len + ra::len))>);
  35. }
  36. tr.section("bare len");
  37. {
  38. static_assert(ra::has_len<decltype(ra::len+1)>);
  39. tr.test_eq(0, (ra::len + ra::len).rank());
  40. tr.test_eq(0, ra::map(std::plus(), ra::len, ra::len).rank());
  41. tr.test_eq(3, with_len(5, ra::scalar(3)));
  42. tr.test_eq(5, with_len(5, ra::len));
  43. tr.test_eq(10, with_len(5, ra::len + ra::len));
  44. tr.test_eq(20, ra::with_len(5, (ra::len + ra::len) + (ra::len + ra::len)));
  45. tr.test_eq(100, ra::with_len(5, ra::map(std::plus(), 99, 1)));
  46. tr.test_eq(10, ra::with_len(5, ra::map(std::plus(), ra::len, ra::len)));
  47. tr.test_eq(104, ra::with_len(5, ra::map(std::plus(), 99, ra::len)));
  48. tr.test_eq(10, ra::with_len(5, ra::map(std::plus(), ra::len, ra::len)));
  49. tr.test_eq(19, ra::with_len(8, ra::map(std::plus(), ra::len, ra::map(std::plus(), 3, ra::len))));
  50. tr.test_eq(ra::iota(10, 11), with_len(8, ra::map(std::plus(), ra::iota(10), ra::map(std::plus(), 3, ra::len))));
  51. tr.test_eq(ra::iota(10, 3), with_len(8, ra::map(std::plus(), ra::iota(10), 3)));
  52. tr.test_eq(11, sum(with_len(4, ra::pick(std::array {0, 1, 0}, ra::len, 3))));
  53. }
  54. tr.section("constexpr");
  55. {
  56. constexpr int val = sum(with_len(4, ra::pick(std::array {0, 1, 0}, ra::len, 3)));
  57. tr.test_eq(11, val);
  58. }
  59. tr.section("len in iota");
  60. {
  61. static_assert(ra::has_len<decltype(ra::iota(ra::len+1, ra::len+ra::len))>);
  62. static_assert(std::is_integral_v<decltype(with_len(5, ra::iota(ra::len)).i)>);
  63. static_assert(std::is_integral_v<decltype(with_len(5, ra::iota(ra::len)).n)>);
  64. static_assert(std::is_integral_v<decltype(with_len(10, ra::iota(ra::len+1, ra::len+ra::len)).i)>);
  65. static_assert(std::is_integral_v<decltype(with_len(10, ra::iota(ra::len+1, ra::len+ra::len)).n)>);
  66. tr.test_eq(ra::iota(5), with_len(5, ra::iota(ra::len)));
  67. tr.test_eq(ra::iota(5, 5), with_len(5, ra::iota(ra::len, ra::len)));
  68. tr.test_eq(ra::iota(5, 20), with_len(10, ra::iota(5, ra::len+ra::len)));
  69. tr.test_eq(ra::iota(10, 20), with_len(10, ra::iota(ra::len, ra::len+ra::len)));
  70. tr.test_eq(ra::iota(11, 20), with_len(10, ra::iota(ra::len+1, ra::len+ra::len)));
  71. tr.test_eq(ra::iota(10, 20, 2), with_len(10, ra::iota(ra::len, ra::len+ra::len, ra::len/5)));
  72. }
  73. tr.section("len in ptr");
  74. {
  75. int aa[] = { 1, 2, 3, 4, 5, 6 };
  76. int * a = aa;
  77. static_assert(ra::has_len<decltype(ra::ptr(a, ra::len))>);
  78. static_assert(std::is_integral_v<decltype(with_len(5, ra::ptr(a, ra::len)).n)>);
  79. static_assert(std::is_integral_v<decltype(with_len(5, ra::ptr(a, ra::len-1)).n)>);
  80. tr.test_eq(ra::ptr(a, 5), with_len(5, ra::ptr(a, ra::len)));
  81. tr.info("len in step argument").test_eq(ra::ptr(a, 3)*2, with_len(6, ra::ptr(a+1, ra::len/2, ra::len/3)));
  82. }
  83. tr.section("static len is preserved");
  84. {
  85. tr.test_eq(5, with_len(ra::ic<5>, ra::iota(ra::len)).len_s(0));
  86. // tr.test_eq(4, std::decay<decltype(*(with_len(ra::ic<5>, ra::len-ra::ic<1>)))>::type::value); // FIXME (*) with_len
  87. // tr.test_eq(5, with_len(ra::ic<5>, ra::iota(ra::len-ra::ic<1>)).ronk()); // FIXME
  88. // tr.test_eq(5, with_len(ra::ic<6>, ra::iota(ra::len-ra::ic<1>)).len_s(0)); // FIXME
  89. }
  90. return tr.summary();
  91. }