iota.cc 3.3 KB

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