frame-old.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/test - Frame-matching tests for pre v10 Expr, previously in test/ra-0.cc.
  3. // (c) Daniel Llorens - 2013-2014, 2019
  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 <numeric>
  9. #include <iostream>
  10. #include <iterator>
  11. #include "ra/test.hh"
  12. #include "ra/complex.hh"
  13. using std::cout, std::endl, std::flush, std::string, ra::TestRecorder;
  14. using real = double;
  15. int main()
  16. {
  17. TestRecorder tr;
  18. tr.section("frame matching - undef-len-iota/Scalar");
  19. {
  20. // driver is highest rank, which is ra::_0 (1).
  21. constexpr auto e = ra::_0+1;
  22. static_assert(ra::rank_s<decltype(e)>()==1, "bad rank_s");
  23. static_assert(e.rank()==1, "bad rank");
  24. static_assert(e.len_s(0)==ra::BAD, "bad len");
  25. }
  26. tr.section("frame matching - Unique/undef-len-iota");
  27. {
  28. ra::Unique<real, 2> c({3, 2}, ra::none);
  29. ra::Unique<real, 2> a({3, 2}, ra::none);
  30. real check[3][2] = { {0, 1}, {1, 2}, {2, 3} };
  31. std::iota(a.begin(), a.end(), 1);
  32. std::fill(c.begin(), c.end(), 0);
  33. ply(expr([](real & c, real a, int b) { c = a-(b+1); },
  34. c.iter(), a.iter(), ra::start(ra::_0)));
  35. tr.test_eq(check, c);
  36. std::fill(c.begin(), c.end(), 0);
  37. ply(expr([](real & c, int a, real b) { c = b-(a+1); },
  38. c.iter(), ra::start(ra::_0), a.iter()));
  39. tr.test_eq(check, c);
  40. }
  41. tr.section("frame matching - Unique/undef-len-iota - undef-len-iota can't be driving arg");
  42. {
  43. ra::Unique<real, 2> c({3, 2}, ra::none);
  44. ra::Unique<real, 2> a({3, 2}, ra::none);
  45. real check[3][2] = { {0, 0}, {2, 2}, {4, 4} };
  46. std::iota(a.begin(), a.end(), 1);
  47. std::fill(c.begin(), c.end(), 0);
  48. ply(expr([](real a, int b, real & c) { c = a-(b+1); },
  49. a.iter(), ra::start(ra::_1), c.iter()));
  50. tr.test_eq(check, c);
  51. std::fill(c.begin(), c.end(), 0);
  52. ply(expr([](int a, real b, real & c) { c = b-(a+1); },
  53. ra::start(ra::_1), a.iter(), c.iter()));
  54. tr.test_eq(check, c);
  55. }
  56. #define TEST(plier) \
  57. std::fill(c.begin(), c.end(), 0); \
  58. plier(expr([](real & c, real a, real b) { c = a-b; }, \
  59. c.iter(), a.iter(), b.iter())); \
  60. tr.test_eq(check, c); \
  61. \
  62. std::fill(c.begin(), c.end(), 0); \
  63. plier(expr([](real & c, real a, real b) { c = b-a; }, \
  64. c.iter(), b.iter(), a.iter())); \
  65. tr.test_eq(check, c);
  66. tr.section("frame matching - Unique/Unique");
  67. {
  68. ra::Unique<real, 2> c({3, 2}, ra::none);
  69. ra::Unique<real, 2> a({3, 2}, ra::none);
  70. ra::Unique<real, 1> b({3}, ra::none);
  71. std::iota(a.begin(), a.end(), 1);
  72. std::iota(b.begin(), b.end(), 1);
  73. real check[3][2] = { {0, 1}, {1, 2}, {2, 3} };
  74. TEST(ply_ravel);
  75. }
  76. tr.section("frame matching - Unique/Small");
  77. {
  78. ra::Unique<real, 2> c({3, 2}, ra::none);
  79. ra::Unique<real, 2> a({3, 2}, ra::none);
  80. ra::Small<real, 3> b;
  81. std::iota(a.begin(), a.end(), 1);
  82. std::iota(b.begin(), b.end(), 1);
  83. real check[3][2] = { {0, 1}, {1, 2}, {2, 3} };
  84. TEST(ply_ravel);
  85. }
  86. tr.section("frame matching - Small/Small");
  87. {
  88. ra::Small<real, 3, 2> c;
  89. ra::Small<real, 3, 2> a;
  90. ra::Small<real, 3> b;
  91. std::iota(a.begin(), a.end(), 1);
  92. std::iota(b.begin(), b.end(), 1);
  93. real check[3][2] = { {0, 1}, {1, 2}, {2, 3} };
  94. TEST(ply_ravel);
  95. }
  96. #undef TEST
  97. tr.section("frame match is good only for full expr, so test on ply, not construction");
  98. {
  99. ra::Unique<real, 2> a({2, 2}, 0.);
  100. ra::Unique<real, 1> b {1., 2.};
  101. // note that b-c has no driver, but all that matters is that the full expression does.
  102. auto e = expr([](real & a, real bc) { a = bc; },
  103. a.iter(), expr([](real b, real c) { return b-c; }, b.iter(), ra::start(ra::_1)));
  104. ply(e);
  105. tr.test_eq(1, a(0, 0));
  106. tr.test_eq(0, a(0, 1));
  107. tr.test_eq(2, a(1, 0));
  108. tr.test_eq(1, a(1, 1));
  109. }
  110. tr.section("FIXME nested expressions from mixing unbeaten/beaten subscripts [ra33]");
  111. {
  112. ra::Big<int, 1> i = {0, 1, 2};
  113. ra::Big<double, 2> A({3, 2}, ra::_0 - ra::_1);
  114. ra::Big<double, 2> F({3, 2}, 0.);
  115. iter<-1>(F) = A(i); // A(i) returns a nested expression.
  116. tr.test_eq(A, F);
  117. }
  118. // See also test/checks.cc.
  119. return tr.summary();
  120. }