ra-6.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file ra-6.cc
  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/ra.hh"
  12. #include "ra/test.hh"
  13. using std::cout, std::endl, ra::TestRecorder;
  14. int main()
  15. {
  16. int N = 4;
  17. ra::Big<float, 3> A({N, N, N}, ra::_2 + ra::_1*4 + ra::_0*16);
  18. ra::Big<float, 2> B({N, N}, ra::_1 + ra::_0*4);
  19. ra::Big<float, 1> C({N}, ra::_0);
  20. cout << "A: " << N << endl;
  21. cout << "B: " << N << endl;
  22. cout << "C: " << N << endl;
  23. // beatable.
  24. ra::Iota<int> i(2, 1);
  25. // making unbeatable on purpose, but still depends on internal Iota counter.
  26. auto j = ra::map([](int i) { return i; }, i);
  27. // naturally unbeatable.
  28. ra::Small<int, 2> k { 1, 2 };
  29. auto l = std::array<int, 2> { 1, 2 };
  30. auto ll = ra::vector(l);
  31. cout << "X0: " << ra::from([](int i, int j, int k) { return ra::Small<int, 3>{i, j, k}; }, i, i, i) << endl;
  32. cout << "X1: " << ra::from([](int i, int j, int k) { return ra::Small<int, 3>{i, j, k}; }, j, j, j) << endl;
  33. cout << "X2: " << ra::from([](int i, int j, int k) { return ra::Small<int, 3>{i, j, k}; }, k, k, k) << endl;
  34. 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;
  35. cout << "X4: " << ra::from([](int i, int j, int k) { return ra::Small<int, 3>{i, j, k}; }, l, l, l) << endl;
  36. // FIXME start() doesn't restart the ra::Vector object, it probably should. :-/ [ra9]
  37. // cout << "X5: " << ra::from([](int i, int j, int k) { return ra::Small<int, 3>{i, j, k}; }, ll, ll, ll) << endl;
  38. cout << endl;
  39. cout << "Y0: " << ra::from(A, i, i, i) << endl;
  40. cout << "Y1: " << ra::from(A, j, j, j) << endl;
  41. cout << "Y2: " << ra::from(A, k, k, k) << endl;
  42. cout << "Y3: " << ra::from(A, ra::vector(l), ra::vector(l), ra::vector(l)) << endl;
  43. cout << "Y4: " << ra::from(A, l, l, l) << endl;
  44. // FIXME start() doesn't restart the ra::Vector object, it probably should. :-/ [ra9]. (This one fails intermittently :-/)
  45. // cout << "Y5: " << ra::from(A, ll, ll, ll) << endl;
  46. cout << B(i, i) << endl;
  47. TestRecorder tr(std::cout);
  48. tr.section("op= with Iota");
  49. {
  50. ra::Iota<int> i(2, 1, 3);
  51. std::cout << "i.i_" << i.i_ << std::endl;
  52. i += 4;
  53. std::cout << "i.i_" << i.i_ <<std::endl;
  54. tr.test_eq(5, i.at(ra::Small<int, 1>{0}));
  55. tr.test_eq(8, i.at(ra::Small<int, 1>{1}));
  56. i -= 1;
  57. std::cout << "i.i_" << i.i_ <<std::endl;
  58. tr.test_eq(4, i.at(ra::Small<int, 1>{0}));
  59. tr.test_eq(7, i.at(ra::Small<int, 1>{1}));
  60. }
  61. tr.section("subs 1");
  62. {
  63. ra::Small<int, 2> ref1 {1, 2};
  64. tr.test_eq(ref1, C(i));
  65. tr.test_eq(ref1, C(j));
  66. tr.test_eq(ref1, C(k));
  67. tr.test_eq(ref1, C(ra::vector(l)));
  68. tr.info("ll").test_eq(ref1, C(ll));
  69. }
  70. tr.section("subs 2");
  71. {
  72. ra::Small<int, 2, 2> ref2 {5, 6, 9, 10};
  73. tr.test_eq(ref2, B(i, i));
  74. tr.test_eq(ref2, B(j, j));
  75. tr.test_eq(ref2, B(k, k));
  76. tr.test_eq(ref2, B(ra::vector(l), ra::vector(l)));
  77. tr.test_eq(ref2, B(l, l));
  78. // FIXME start() doesn't restart the ra::Vector object, it probably should. :-/ [ra9]
  79. // tr.info("ll").test_eq(ref2, B(ll, ll));
  80. }
  81. // TODO have a proper rank / shape match error when comparing these with ref3
  82. tr.section("subs 3");
  83. {
  84. ra::Small<int, 2, 2, 2> ref3 {21, 22, 25, 26, 37, 38, 41, 42};
  85. tr.test_eq(ref3, A(i, i, i));
  86. tr.test_eq(ref3, A(j, j, j));
  87. tr.test_eq(ref3, A(k, k, k));
  88. tr.test_eq(ref3, A(ra::vector(l), ra::vector(l), ra::vector(l)));
  89. tr.test_eq(ref3, A(l, l, l));
  90. // FIXME start() doesn't restart the ra::Vector object, it probably should. :-/ [ra9]
  91. // tr.info("ll").test_eq(ref3, A(ll, ll, ll));
  92. }
  93. return tr.summary();
  94. }