ra-6.C 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file ra-6.C
  3. /// @brief A regression test.
  4. // (c) Daniel Llorens - 2014-2015
  5. // This library is free software; you can redistribute it and/or modify it under
  6. // the terms of the GNU Lesser General Public License as published by the Free
  7. // Software Foundation; either version 3 of the License, or (at your option) any
  8. // later version.
  9. // Regression test for a bug with > 2 non-beatable selectors. The bug was due to
  10. // bad assumptions in ra::Iota::adv() and ra::Vector::adv().
  11. #include "ra/operators.H"
  12. #include "ra/io.H"
  13. #include "ra/test.H"
  14. using std::cout, std::endl;
  15. int main()
  16. {
  17. int N = 4;
  18. ra::Big<float, 3> A({N, N, N}, ra::_2 + ra::_1*4 + ra::_0*16);
  19. ra::Big<float, 2> B({N, N}, ra::_1 + ra::_0*4);
  20. ra::Big<float, 1> C({N}, ra::_0);
  21. cout << "A: " << N << endl;
  22. cout << "B: " << N << endl;
  23. cout << "C: " << N << endl;
  24. // beatable.
  25. ra::Iota<int> i(2, 1);
  26. // making unbeatable on purpose, but still depends on internal Iota counter.
  27. auto j = ra::map([](int i) { return i; }, i);
  28. // naturally unbeatable.
  29. ra::Small<int, 2> k { 1, 2 };
  30. auto l = std::array<int, 2> { 1, 2 };
  31. auto ll = ra::vector(l);
  32. cout << "X0: " << ra::from([](int i, int j, int k) { return ra::Small<int, 3>{i, j, k}; }, i, i, i) << endl;
  33. cout << "X1: " << ra::from([](int i, int j, int k) { return ra::Small<int, 3>{i, j, k}; }, j, j, j) << endl;
  34. cout << "X2: " << ra::from([](int i, int j, int k) { return ra::Small<int, 3>{i, j, k}; }, k, k, k) << endl;
  35. cout << "X3: " << ra::from([](int i, int j, int k) { return ra::Small<int, 3>{i, j, k}; }, ra::vector(l), ra::vector(l), ra::vector(l)) << endl;
  36. cout << "X4: " << ra::from([](int i, int j, int k) { return ra::Small<int, 3>{i, j, k}; }, ll, ll, ll) << endl;
  37. cout << endl;
  38. cout << "Y0: " << ra::from(A, i, i, i) << endl;
  39. cout << "Y1: " << ra::from(A, j, j, j) << endl;
  40. cout << "Y2: " << ra::from(A, k, k, k) << endl;
  41. cout << "Y3: " << ra::from(A, ra::vector(l), ra::vector(l), ra::vector(l)) << endl;
  42. cout << "Y4: " << ra::from(A, ll, ll, ll) << endl;
  43. cout << B(i, i) << endl;
  44. TestRecorder tr(std::cout);
  45. tr.section("op= with Iota");
  46. {
  47. ra::Iota<int> i(2, 1, 3);
  48. std::cout << "i.i_" << i.i_ << std::endl;
  49. i += 4;
  50. std::cout << "i.i_" << i.i_ <<std::endl;
  51. tr.test_eq(5, i.at(ra::Small<int, 1>{0}));
  52. tr.test_eq(8, i.at(ra::Small<int, 1>{1}));
  53. i -= 1;
  54. std::cout << "i.i_" << i.i_ <<std::endl;
  55. tr.test_eq(4, i.at(ra::Small<int, 1>{0}));
  56. tr.test_eq(7, i.at(ra::Small<int, 1>{1}));
  57. }
  58. tr.section("subs 1");
  59. {
  60. ra::Small<int, 2> ref1 {1, 2};
  61. tr.test_eq(ref1, C(i));
  62. tr.test_eq(ref1, C(j));
  63. tr.test_eq(ref1, C(k));
  64. tr.test_eq(ref1, C(ra::vector(l)));
  65. tr.info("ll").test_eq(ref1, C(ll));
  66. }
  67. tr.section("subs 2");
  68. {
  69. ra::Small<int, 2, 2> ref2 {5, 6, 9, 10};
  70. tr.test_eq(ref2, B(i, i));
  71. tr.test_eq(ref2, B(j, j));
  72. tr.test_eq(ref2, B(k, k));
  73. tr.test_eq(ref2, B(ra::vector(l), ra::vector(l)));
  74. // FIXME thinking this should work anyway, as start() copies the ra::Vector object :-/
  75. // tr.info("ll").test_eq(ref2, B(ll, ll));
  76. }
  77. // TODO have a proper rank / shape match error when comparing these with ref3
  78. tr.section("subs 3");
  79. {
  80. ra::Small<int, 2, 2, 2> ref3 {21, 22, 25, 26, 37, 38, 41, 42};
  81. tr.test_eq(ref3, A(i, i, i));
  82. // tr.test_eq(ref3, A(j, j, j));
  83. tr.test_eq(ref3, A(k, k, k));
  84. tr.test_eq(ref3, A(ra::vector(l), ra::vector(l), ra::vector(l)));
  85. // FIXME thinking this should work anyway, as start() copies the ra::Vector object :-/
  86. // tr.info("ll").test_eq(ref3, A(ll, ll, ll));
  87. }
  88. return tr.summary();
  89. }