frame-old.cc 4.8 KB

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