iota.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/test - Arrays, iterators.
  3. // (c) Daniel Llorens - 2013-2024
  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 <iterator>
  10. #include "ra/test.hh"
  11. using std::cout, std::endl, std::flush, ra::TestRecorder;
  12. int main()
  13. {
  14. TestRecorder tr(std::cout);
  15. static_assert(ra::IteratorConcept<decltype(ra::iota(10))>, "bad type pred for iota");
  16. tr.section("straight cases");
  17. {
  18. ra::Big<int, 1> a = ra::iota(4, 1);
  19. tr.test_eq(1, a[0]);
  20. tr.test_eq(2, a[1]);
  21. tr.test_eq(3, a[2]);
  22. tr.test_eq(4, a[3]);
  23. }
  24. tr.section("work with operators");
  25. {
  26. tr.test(every(ra::iota(4)==ra::Big<int, 1> {0, 1, 2, 3}));
  27. tr.test(every(ra::iota(4, 1)==ra::Big<int, 1> {1, 2, 3, 4}));
  28. tr.test(every(ra::iota(4, 1, 2)==ra::Big<int, 1> {1, 3, 5, 7}));
  29. }
  30. // TODO actually whether unroll is avoided depends on ply(), have a way to require it [ra3]
  31. tr.section("frame-matching, forbidding unroll");
  32. {
  33. ra::Big<int, 3> b ({3, 4, 2}, ra::none);
  34. transpose({0, 2, 1}, b) = ra::iota(3, 1);
  35. cout << b << endl;
  36. tr.test(every(b(0)==1));
  37. tr.test(every(b(1)==2));
  38. tr.test(every(b(2)==3));
  39. }
  40. {
  41. ra::Big<int, 3> b ({3, 4, 2}, ra::none);
  42. transpose<0, 2, 1>(b) = ra::iota(3, 1);
  43. cout << b << endl;
  44. tr.test(every(b(0)==1));
  45. tr.test(every(b(1)==2));
  46. tr.test(every(b(2)==3));
  47. }
  48. tr.section("indefinite length I");
  49. {
  50. tr.test_eq(4, (ra::iota() + ra::iota(4)).len(0));
  51. ra::Big<int, 1> a = ra::iota() + ra::iota(4);
  52. tr.test_eq(0, a[0]);
  53. tr.test_eq(2, a[1]);
  54. tr.test_eq(4, a[2]);
  55. tr.test_eq(6, a[3]);
  56. }
  57. tr.section("indefinite length II");
  58. {
  59. ra::Big<int, 1> b = ra::Small<int, 4> { 3, 5, 0, -1 } + ra::iota();
  60. tr.test_eq(3, b[0]);
  61. tr.test_eq(6, b[1]);
  62. tr.test_eq(2, b[2]);
  63. tr.test_eq(2, b[3]);
  64. tr.test_eq(99, ra::iota().at(std::array {99}));
  65. }
  66. tr.section("deduced types");
  67. {
  68. tr.test(std::is_same_v<double, decltype(ra::iota(5, double(4)).i)>);
  69. }
  70. tr.section("ra::iota with static members");
  71. {
  72. tr.test_eq(sizeof(ra::iota().i), sizeof(ra::dim_t));
  73. tr.test_eq(sizeof(ra::iota(4, 0, 2).i), sizeof(0));
  74. tr.test_eq(sizeof(ra::iota().i), sizeof(ra::iota()));
  75. tr.test_eq(sizeof(ra::iota().i), sizeof(ra::iota(ra::dim_c<4> {})));
  76. // sizeof might still be > sizeof(i) + sizeof(n) because of alignment
  77. tr.test_eq(1, decltype(ra::iota(4).s)::value);
  78. tr.test_eq(sizeof(ra::iota(4, 0, 2)), sizeof(ra::iota(4, 0, 2).i) + sizeof(ra::iota(4, 0, 2).n) + sizeof(ra::iota(4, 0, 2).s));
  79. }
  80. tr.section("iota simulation with ptr(iota_view)");
  81. {
  82. tr.strictshape().test_eq(ra::iota(10, 0, 3), ra::ptr(std::ranges::iota_view(0, 10))*3);
  83. }
  84. tr.section("reverse(iota)");
  85. {
  86. auto i = ra::iota(ra::dim_c<5> {}, 3, ra::dim_c<-2> {});
  87. auto ri = reverse(i);
  88. static_assert(5==size(ri));
  89. static_assert(2==ri.s);
  90. tr.strictshape().test_eq(reverse(ra::Big<int, 1>(i)), reverse(i));
  91. }
  92. {
  93. auto i = ra::iota(ra::dim_c<5> {}, 3, -2);
  94. tr.strictshape().test_eq(reverse(ra::Big<int, 1>(i)), reverse(i));
  95. }
  96. {
  97. auto i = ra::iota(5, 3, -2);
  98. tr.strictshape().test_eq(reverse(ra::Big<int, 1>(i)), reverse(i));
  99. }
  100. return tr.summary();
  101. }