big-1.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/test - Tests specific to Container.
  3. // (c) Daniel Llorens - 2017, 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 <iostream>
  9. #include <iterator>
  10. #include "ra/test.hh"
  11. using std::cout, std::endl, std::flush, ra::TestRecorder;
  12. template <class T, ra::rank_t RANK=ra::ANY> using BigValueInit = ra::Container<std::vector<T>, RANK>;
  13. using int2 = ra::Small<int, 2>;
  14. int main()
  15. {
  16. TestRecorder tr;
  17. tr.section("push_back");
  18. {
  19. std::vector<int2> a;
  20. a.push_back({1, 2});
  21. ra::Big<int2, 1> b;
  22. b.push_back({1, 2});
  23. int2 check[1] = {{1, 2}};
  24. tr.test_eq(check, ra::start(a));
  25. tr.test_eq(check, b);
  26. tr.test_eq(check[0], b.back());
  27. tr.test_eq(int2 {1, 2}, b().back()); // back on views
  28. }
  29. tr.section(".back() is last element not last item");
  30. {
  31. ra::Big<int2, 0> b({}, ra::scalar(int2 {1, 3})); // cf [ma116]
  32. tr.test_eq(int2 {1, 3}, b.back());
  33. }
  34. tr.section("behavior of resize with default Container");
  35. {
  36. {
  37. ra::Big<int, 1> a = {1, 2, 3, 4, 5, 6};
  38. a.resize(3);
  39. a.resize(6);
  40. tr.test_eq(ra::iota(6, 1), a);
  41. }
  42. {
  43. BigValueInit<int, 1> a = {1, 2, 3, 4, 5, 6};
  44. a.resize(3);
  45. a.resize(6);
  46. tr.test_eq(ra::start({1, 2, 3, 0, 0, 0}), a);
  47. }
  48. }
  49. tr.section("resize works on first dimension");
  50. {
  51. {
  52. ra::Big<int, 2> a({3, 2}, {1, 2, 3, 4, 5, 6});
  53. a.resize(2);
  54. tr.test_eq(1+ra::_1 + 2*ra::_0, a);
  55. }
  56. {
  57. ra::Big<int, 2> a({3, 2}, {1, 2, 3, 4, 5, 6});
  58. resize(a, 2);
  59. tr.test_eq(1+ra::_1 + 2*ra::_0, a);
  60. }
  61. }
  62. tr.section("operator=");
  63. {
  64. ra::Big<int, 2> a = {{0, 1, 2}, {3, 4, 5}};
  65. a = {{4, 5, 6}, {7, 8, 9}}; // unbraced
  66. tr.test_eq(ra::iota(6, 4), ra::ptr(a.data()));
  67. a = {{{4, 5, 6}, {7, 8, 9}}}; // braced :-/
  68. tr.test_eq(ra::iota(6, 4), ra::ptr(a.data()));
  69. // a = {{4, 5}, {7, 8}}; // operator= works as view (so this fails), but cannot verify [ra42].
  70. // tr.test_eq(a, ra::Small<int, 2, 2> {{4, 5}, {7, 8}});
  71. }
  72. tr.section("behavior of forced Fortran array");
  73. {
  74. ra::Big<int, 2> a ({2, 3}, {0, 1, 2, 3, 4, 5});
  75. ra::Big<int, 2> b ({2, 3}, {0, 1, 2, 3, 4, 5});
  76. auto c = transpose({1, 0}, ra::ViewBig<int, 2>({3, 2}, a.data()));
  77. a.dimv = c.dimv;
  78. for (int k=0; k!=c.rank(); ++k) {
  79. std::cout << "CSTRIDE " << k << " " << c.step(k) << std::endl;
  80. std::cout << "CLEN " << k << " " << c.len(k) << std::endl;
  81. }
  82. cout << endl;
  83. for (int k=0; k!=a.rank(); ++k) {
  84. std::cout << "ASTRIDE " << k << " " << a.step(k) << std::endl;
  85. std::cout << "ALEN " << k << " " << a.len(k) << std::endl;
  86. }
  87. cout << endl;
  88. c = b;
  89. // FIXME this clobbers the steps of a, which is surprising -> Container should behave as View. Or, what happens to a shouldn't depend on the container vs view-ness of b.
  90. a = b;
  91. for (int k=0; k!=c.rank(); ++k) {
  92. std::cout << "CSTRIDE " << k << " " << c.step(k) << std::endl;
  93. std::cout << "CLEN " << k << " " << c.len(k) << std::endl;
  94. }
  95. cout << endl;
  96. for (int k=0; k!=a.rank(); ++k) {
  97. std::cout << "ASTRIDE " << k << " " << a.step(k) << std::endl;
  98. std::cout << "ALEN " << k << " " << a.len(k) << std::endl;
  99. }
  100. cout << endl;
  101. std::cout << "a: " << a << std::endl;
  102. std::cout << "b: " << b << std::endl;
  103. std::cout << "c: " << c << std::endl;
  104. }
  105. tr.section("using shape as expr");
  106. {
  107. ra::Unique<double, 3> v({3, 2, 4}, ra::_0 + 15*ra::_1);
  108. tr.test_eq(ra::start({3, 2}), ra::shape(v, ra::iota(2)));
  109. tr.test_eq(ra::start({3, 2}), ra::shape(v, ra::iota(ra::len-1)));
  110. tr.test_eq(ra::start({2, 4}), ra::shape(v, ra::iota(2, 1)));
  111. tr.test_eq(ra::start({2, 4}), ra::shape(v, ra::iota(2, ra::len-2)));
  112. ra::Small<int, 4> i = {0, 2, 1, 0};
  113. tr.test_eq(ra::start({3, 4}), ra::shape(v, i(ra::iota(ra::len-2)))); // len is i's
  114. }
  115. tr.section("casts from fixed rank View");
  116. {
  117. ra::Unique<double, 3> a({3, 2, 4}, ra::_0 + 15*ra::_1);
  118. ra::ViewBig<double> b(a);
  119. tr.test_eq(ra::scalar(a.data()), ra::scalar(b.data())); // FIXME? pointers are not ra::scalars.
  120. tr.test_eq(a.rank(), b.rank());
  121. tr.test_eq(a.len(0), b.len(0));
  122. tr.test_eq(a.len(1), b.len(1));
  123. tr.test_eq(a.len(2), b.len(2));
  124. tr.test(every(a==b));
  125. auto test = [&tr](ra::ViewBig<double, 3> a, double * p)
  126. {
  127. tr.test_eq(ra::Small<int, 3> {3, 2, 4}, shape(a));
  128. tr.test(p==a.data());
  129. };
  130. auto test_const = [&tr](ra::ViewBig<double const, 3> a, double * p)
  131. {
  132. tr.test_eq(ra::Small<int, 3> {3, 2, 4}, shape(a));
  133. tr.test(p==a.data());
  134. };
  135. auto test_const_ref = [&tr](ra::ViewBig<double const, 3> const & a, double * p)
  136. {
  137. tr.test_eq(ra::Small<int, 3> {3, 2, 4}, shape(a));
  138. tr.test(p==a.data());
  139. };
  140. test(b, b.data()); // var rank to fixed rank
  141. test_const(b, b.data()); // non-const to const, var rank to fixed rank
  142. test_const_ref(a(), a.data()); // non-const to const, keeping fixed rank
  143. }
  144. tr.section("casts from var rank View");
  145. {
  146. ra::Unique<double> a({3, 2, 4}, ra::none);
  147. ra::ViewBig<double, 3> b(a);
  148. tr.test_eq(ra::scalar(a.data()), ra::scalar(b.data())); // FIXME? pointers are not ra::scalars.
  149. tr.test_eq(a.rank(), b.rank());
  150. tr.test_eq(a.len(0), b.len(0));
  151. tr.test_eq(a.len(1), b.len(1));
  152. tr.test_eq(a.len(2), b.len(2));
  153. tr.test(every(a==b));
  154. auto test = [&tr](ra::ViewBig<double> a, double * p)
  155. {
  156. tr.test_eq(ra::Small<int, 3> {3, 2, 4}, shape(a));
  157. tr.test(p==a.data());
  158. };
  159. auto test_const = [&tr](ra::ViewBig<double const> a, double * p)
  160. {
  161. tr.test_eq(ra::Small<int, 3> {3, 2, 4}, shape(a));
  162. tr.test(p==a.data());
  163. };
  164. auto test_const_ref = [&tr](ra::ViewBig<double const> const & a, double * p)
  165. {
  166. tr.test_eq(ra::Small<int, 3> {3, 2, 4}, shape(a));
  167. tr.test(p==a.data());
  168. };
  169. test(b, b.data()); // fixed rank to var rank
  170. test_const(b, b.data()); // non-const to const, fixed rank to var rank
  171. test_const_ref(a, a.data()); // non-const to const, keeping var rank
  172. }
  173. tr.section("multidimensional []");
  174. {
  175. ra::Unique<int> a({3, 2, 4}, ra::_0 + ra::_1 - ra::_2);
  176. tr.test_eq(a(ra::all, 0), a[ra::all, 0]);
  177. }
  178. return tr.summary();
  179. }