ra-5.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/test - Regression tests (2).
  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. // Regression test for a bug ... caught first in fold_mat @ array.cc.
  9. // Caused by d139794396a0d51dc0c25b0b03b2a2ef0e2760b5 : Remove set() from CellBig, CellSmall.
  10. #include <iostream>
  11. #include "ra/test.hh"
  12. #include "mpdebug.hh"
  13. using std::cout, std::endl, ra::TestRecorder;
  14. int main()
  15. {
  16. TestRecorder tr(std::cout);
  17. tr.section("looking into Small");
  18. // [ra14] A(b) which is from(A, b) can require CellBig to copy its source Dimv.
  19. tr.section("Big");
  20. {
  21. ra::Big<int, 1> b = { 2, 1 };
  22. ra::Big<int, 2> A({3, 5}, ra::_0 - ra::_1);
  23. ra::Big<int, 2> F({2, 5}, 0);
  24. // This creates View & CellBig on each call of A(b(0) ...) as the driver is b and A is handled as generic object with operator().
  25. // But I should be able to create a single CellBig and just bump a pointer as I move through b. Hmm.
  26. iter<-1>(F) = b*A(b);
  27. int Fcheck[2][5] = { {4, 2, 0, -2, -4}, {1, 0, -1, -2, -3} };
  28. tr.test_eq(Fcheck, F);
  29. }
  30. // Why: if x(0) is a temp, as in here, CellBig needs a copy of x(0).dim.
  31. // This is achieved by forwarding in start() -> iter() -> View.iter().
  32. tr.section("CellBig handling of temps");
  33. {
  34. auto demo = [](auto & x) { return iter<0>(x(0)); };
  35. ra::Big<int, 2> A({3, 5}, 0);
  36. auto z = demo(A);
  37. tr.test_eq(5, z.dimv[0].len);
  38. tr.test_eq(false, std::is_reference_v<decltype(z.dimv)>);
  39. auto y = A(0);
  40. auto yi = iter<0>(y);
  41. tr.test_eq(true, std::is_reference_v<decltype(yi.dimv)>);
  42. }
  43. // On Small, the container and the view are separate objects, so we must make sure to forward any temporary views(). See SmallView::operator().
  44. tr.section("Small");
  45. {
  46. ra::Small<int, 2> b = { 2, 1 };
  47. ra::Small<int, 3, 5> A = ra::_0 - ra::_1;
  48. ra::Small<int, 2, 5> F = 0;
  49. iter<-1>(F) = b*A(b);
  50. int Fcheck[2][5] = { {4, 2, 0, -2, -4}, {1, 0, -1, -2, -3} };
  51. tr.test_eq(Fcheck, F);
  52. }
  53. // const/nonconst begin :p
  54. {
  55. ra::Big<int> A({2, 3}, 3);
  56. auto const b = A();
  57. int x[6] = { 0, 0, 0, 0, 0, 0 };
  58. std::ranges::copy(b.begin(), b.end(), x);
  59. tr.test_eq(3, ra::start(x));
  60. }
  61. return tr.summary();
  62. }