big-1.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. int main()
  14. {
  15. TestRecorder tr;
  16. tr.section("push_back");
  17. {
  18. using int2 = ra::Small<int, 2>;
  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. }
  27. tr.section("behavior of resize with default Container");
  28. {
  29. {
  30. ra::Big<int, 1> a = {1, 2, 3, 4, 5, 6};
  31. a.resize(3);
  32. a.resize(6);
  33. tr.test_eq(ra::iota(6, 1), a);
  34. }
  35. {
  36. BigValueInit<int, 1> a = {1, 2, 3, 4, 5, 6};
  37. a.resize(3);
  38. a.resize(6);
  39. tr.test_eq(ra::start({1, 2, 3, 0, 0, 0}), a);
  40. }
  41. }
  42. tr.section("resize works on first dimension");
  43. {
  44. {
  45. ra::Big<int, 2> a({3, 2}, {1, 2, 3, 4, 5, 6});
  46. a.resize(2);
  47. tr.test_eq(1+ra::_1 + 2*ra::_0, a);
  48. }
  49. {
  50. ra::Big<int, 2> a({3, 2}, {1, 2, 3, 4, 5, 6});
  51. resize(a, 2);
  52. tr.test_eq(1+ra::_1 + 2*ra::_0, a);
  53. }
  54. }
  55. tr.section("operator=");
  56. {
  57. ra::Big<int, 2> a = {{0, 1, 2}, {3, 4, 5}};
  58. a = {{4, 5, 6}, {7, 8, 9}}; // unbraced
  59. tr.test_eq(ra::iota(6, 4), ra::ptr(a.data()));
  60. a = {{{4, 5, 6}, {7, 8, 9}}}; // braced :-/
  61. tr.test_eq(ra::iota(6, 4), ra::ptr(a.data()));
  62. // a = {{4, 5}, {7, 8}}; // operator= works as view (so this fails), but cannot verify [ra42].
  63. // tr.test_eq(a, ra::Small<int, 2, 2> {{4, 5}, {7, 8}});
  64. }
  65. tr.section("behavior of forced Fortran array");
  66. {
  67. ra::Big<int, 2> a ({2, 3}, {0, 1, 2, 3, 4, 5});
  68. ra::Big<int, 2> b ({2, 3}, {0, 1, 2, 3, 4, 5});
  69. auto c = transpose({1, 0}, ra::View<int, 2>({3, 2}, a.data()));
  70. a.dimv = c.dimv;
  71. for (int k=0; k!=c.rank(); ++k) {
  72. std::cout << "CSTRIDE " << k << " " << c.step(k) << std::endl;
  73. std::cout << "CLEN " << k << " " << c.len(k) << std::endl;
  74. }
  75. cout << endl;
  76. for (int k=0; k!=a.rank(); ++k) {
  77. std::cout << "ASTRIDE " << k << " " << a.step(k) << std::endl;
  78. std::cout << "ALEN " << k << " " << a.len(k) << std::endl;
  79. }
  80. cout << endl;
  81. c = b;
  82. // 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.
  83. a = b;
  84. for (int k=0; k!=c.rank(); ++k) {
  85. std::cout << "CSTRIDE " << k << " " << c.step(k) << std::endl;
  86. std::cout << "CLEN " << k << " " << c.len(k) << std::endl;
  87. }
  88. cout << endl;
  89. for (int k=0; k!=a.rank(); ++k) {
  90. std::cout << "ASTRIDE " << k << " " << a.step(k) << std::endl;
  91. std::cout << "ALEN " << k << " " << a.len(k) << std::endl;
  92. }
  93. cout << endl;
  94. std::cout << "a: " << a << std::endl;
  95. std::cout << "b: " << b << std::endl;
  96. std::cout << "c: " << c << std::endl;
  97. }
  98. tr.section("casts from fixed rank View");
  99. {
  100. ra::Unique<double, 3> a({3, 2, 4}, ra::_0 + 15*ra::_1);
  101. ra::View<double> b(a);
  102. tr.test_eq(ra::scalar(a.data()), ra::scalar(b.data())); // FIXME? pointers are not ra::scalars.
  103. tr.test_eq(a.rank(), b.rank());
  104. tr.test_eq(a.len(0), b.len(0));
  105. tr.test_eq(a.len(1), b.len(1));
  106. tr.test_eq(a.len(2), b.len(2));
  107. tr.test(every(a==b));
  108. auto test = [&tr](ra::View<double, 3> a, double * p)
  109. {
  110. tr.test_eq(ra::Small<int, 3> {3, 2, 4}, shape(a));
  111. tr.test(p==a.data());
  112. };
  113. auto test_const = [&tr](ra::View<double const, 3> a, double * p)
  114. {
  115. tr.test_eq(ra::Small<int, 3> {3, 2, 4}, shape(a));
  116. tr.test(p==a.data());
  117. };
  118. auto test_const_ref = [&tr](ra::View<double const, 3> const & a, double * p)
  119. {
  120. tr.test_eq(ra::Small<int, 3> {3, 2, 4}, shape(a));
  121. tr.test(p==a.data());
  122. };
  123. test(b, b.data()); // var rank to fixed rank
  124. test_const(b, b.data()); // non-const to const, var rank to fixed rank
  125. test_const_ref(a(), a.data()); // non-const to const, keeping fixed rank
  126. }
  127. tr.section("casts from var rank View");
  128. {
  129. ra::Unique<double> a({3, 2, 4}, ra::none);
  130. ra::View<double, 3> b(a);
  131. tr.test_eq(ra::scalar(a.data()), ra::scalar(b.data())); // FIXME? pointers are not ra::scalars.
  132. tr.test_eq(a.rank(), b.rank());
  133. tr.test_eq(a.len(0), b.len(0));
  134. tr.test_eq(a.len(1), b.len(1));
  135. tr.test_eq(a.len(2), b.len(2));
  136. tr.test(every(a==b));
  137. auto test = [&tr](ra::View<double> a, double * p)
  138. {
  139. tr.test_eq(ra::Small<int, 3> {3, 2, 4}, shape(a));
  140. tr.test(p==a.data());
  141. };
  142. auto test_const = [&tr](ra::View<double const> a, double * p)
  143. {
  144. tr.test_eq(ra::Small<int, 3> {3, 2, 4}, shape(a));
  145. tr.test(p==a.data());
  146. };
  147. auto test_const_ref = [&tr](ra::View<double const> const & a, double * p)
  148. {
  149. tr.test_eq(ra::Small<int, 3> {3, 2, 4}, shape(a));
  150. tr.test(p==a.data());
  151. };
  152. test(b, b.data()); // fixed rank to var rank
  153. test_const(b, b.data()); // non-const to const, fixed rank to var rank
  154. test_const_ref(a, a.data()); // non-const to const, keeping var rank
  155. }
  156. #if __cpp_multidimensional_subscript >= 202110L
  157. tr.section("multidimensional []");
  158. {
  159. ra::Unique<int> a({3, 2, 4}, ra::_0 + ra::_1 - ra::_2);
  160. tr.test_eq(a(ra::all, 0), a[ra::all, 0]);
  161. }
  162. #endif
  163. return tr.summary();
  164. }