big-1.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file big-1.cc
  3. /// @brief Tests specific to Container.
  4. // (c) Daniel Llorens - 2017, 2019
  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. #include <iostream>
  10. #include <iterator>
  11. #include "ra/ra.hh"
  12. #include "ra/test.hh"
  13. using std::cout, std::endl, std::flush, ra::TestRecorder;
  14. template <class T, ra::rank_t RANK=ra::RANK_ANY> using BigValueInit = ra::Container<std::vector<T>, RANK>;
  15. int main()
  16. {
  17. TestRecorder tr;
  18. tr.section("push_back");
  19. {
  20. using int2 = ra::Small<int, 2>;
  21. std::vector<int2> a;
  22. a.push_back({1, 2});
  23. ra::Big<int2, 1> b;
  24. b.push_back({1, 2});
  25. int2 check[1] = {{1, 2}};
  26. tr.test_eq(check, ra::start(a));
  27. tr.test_eq(check, b);
  28. }
  29. tr.section("behavior of resize with default Container");
  30. {
  31. {
  32. ra::Big<int, 1> a = {1, 2, 3, 4, 5, 6};
  33. a.resize(3);
  34. a.resize(6);
  35. tr.test_eq(ra::iota(6, 1), a);
  36. }
  37. {
  38. BigValueInit<int, 1> a = {1, 2, 3, 4, 5, 6};
  39. a.resize(3);
  40. a.resize(6);
  41. tr.test_eq(ra::start({1, 2, 3, 0, 0, 0}), a);
  42. }
  43. }
  44. tr.section("resize works on first dimension");
  45. {
  46. {
  47. ra::Big<int, 2> a({3, 2}, {1, 2, 3, 4, 5, 6});
  48. a.resize(2);
  49. tr.test_eq(1+ra::_1 + 2*ra::_0, a);
  50. }
  51. {
  52. ra::Big<int, 2> a({3, 2}, {1, 2, 3, 4, 5, 6});
  53. resize(a, 2);
  54. tr.test_eq(1+ra::_1 + 2*ra::_0, a);
  55. }
  56. }
  57. tr.section("operator=");
  58. {
  59. ra::Big<int, 2> a = {{0, 1, 2}, {3, 4, 5}};
  60. a = {{4, 5, 6}, {7, 8, 9}}; // unbraced
  61. tr.test_eq(ra::iota(6, 4), ra::ptr(a.data()));
  62. a = {{{4, 5, 6}, {7, 8, 9}}}; // braced :-/
  63. tr.test_eq(ra::iota(6, 4), ra::ptr(a.data()));
  64. // a = {{4, 5}, {7, 8}}; // operator= works as view (so this fails), but cannot verify [ra42].
  65. // tr.test_eq(a, ra::Small<int, 2, 2> {{4, 5}, {7, 8}});
  66. }
  67. tr.section("behavior of forced Fortran array");
  68. {
  69. ra::Big<int, 2> a ({2, 3}, {0, 1, 2, 3, 4, 5});
  70. ra::Big<int, 2> b ({2, 3}, {0, 1, 2, 3, 4, 5});
  71. auto c = transpose({1, 0}, ra::View<int *, 2>({3, 2}, a.data())); // [ra28] TODO make_view to deduce P
  72. a.view().dim = c.dim;
  73. for (int k=0; k!=c.rank(); ++k) {
  74. std::cout << "CSTRIDE " << k << " " << c.stride(k) << std::endl;
  75. std::cout << "CSIZE " << k << " " << c.size(k) << std::endl;
  76. }
  77. cout << endl;
  78. for (int k=0; k!=a.rank(); ++k) {
  79. std::cout << "ASTRIDE " << k << " " << a.stride(k) << std::endl;
  80. std::cout << "ASIZE " << k << " " << a.size(k) << std::endl;
  81. }
  82. cout << endl;
  83. c = b;
  84. // FIXME this clobbers the strides 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.
  85. a = b;
  86. for (int k=0; k!=c.rank(); ++k) {
  87. std::cout << "CSTRIDE " << k << " " << c.stride(k) << std::endl;
  88. std::cout << "CSIZE " << k << " " << c.size(k) << std::endl;
  89. }
  90. cout << endl;
  91. for (int k=0; k!=a.rank(); ++k) {
  92. std::cout << "ASTRIDE " << k << " " << a.stride(k) << std::endl;
  93. std::cout << "ASIZE " << k << " " << a.size(k) << std::endl;
  94. }
  95. cout << endl;
  96. std::cout << "a: " << a << std::endl;
  97. std::cout << "b: " << b << std::endl;
  98. std::cout << "c: " << c << std::endl;
  99. }
  100. tr.section("casts from fixed rank View");
  101. {
  102. ra::Unique<double, 3> a({3, 2, 4}, ra::_0 + 15*ra::_1);
  103. ra::View<double *> b(a.view()); // FIXME [ra28]
  104. ra::View<double const *> c(a.view());
  105. tr.test_eq(ra::scalar(a.data()), ra::scalar(b.data())); // FIXME? pointers are not ra::scalars.
  106. tr.test_eq(a.rank(), b.rank());
  107. tr.test_eq(a.size(0), b.size(0));
  108. tr.test_eq(a.size(1), b.size(1));
  109. tr.test_eq(a.size(2), b.size(2));
  110. tr.test(every(a==b));
  111. tr.test(every(a==c));
  112. auto test = [&tr](ra::View<double *, 3> a, double * p)
  113. {
  114. tr.test_eq(ra::Small<int, 3> {3, 2, 4}, shape(a));
  115. tr.test(p==a.data());
  116. };
  117. auto test_const = [&tr](ra::View<double const *, 3> a, double * p)
  118. {
  119. tr.test_eq(ra::Small<int, 3> {3, 2, 4}, shape(a));
  120. tr.test(p==a.data());
  121. };
  122. auto test_const_ref = [&tr](ra::View<double const *, 3> const & a, double * p)
  123. {
  124. tr.test_eq(ra::Small<int, 3> {3, 2, 4}, shape(a));
  125. tr.test(p==a.data());
  126. };
  127. test(b, a.data()); // var rank to fixed rank [ra18]
  128. test_const(c, a.data()); // var rank to fixed rank [ra18]
  129. test_const(b, a.data()); // non-const to const, var rank to fixed rank [ra29]
  130. test_const_ref(a(), a.data()); // non-const to const, keeping fixed rank [ra29]
  131. }
  132. tr.section("casts from var rank View");
  133. {
  134. ra::Unique<double> a({3, 2, 4}, ra::none);
  135. ra::View<double *, 3> b(a.view()); // FIXME [ra28]
  136. ra::View<double const *, 3> c(a.view());
  137. tr.test_eq(ra::scalar(a.data()), ra::scalar(b.data())); // FIXME? pointers are not ra::scalars.
  138. tr.test_eq(a.rank(), b.rank());
  139. tr.test_eq(a.size(0), b.size(0));
  140. tr.test_eq(a.size(1), b.size(1));
  141. tr.test_eq(a.size(2), b.size(2));
  142. tr.test(every(a==b));
  143. tr.test(every(a==c));
  144. auto test = [&tr](ra::View<double *> a, double * p)
  145. {
  146. tr.test_eq(ra::Small<int, 3> {3, 2, 4}, shape(a));
  147. tr.test(p==a.data());
  148. };
  149. auto test_const = [&tr](ra::View<double const *> a, double * p)
  150. {
  151. tr.test_eq(ra::Small<int, 3> {3, 2, 4}, shape(a));
  152. tr.test(p==a.data());
  153. };
  154. auto test_const_ref = [&tr](ra::View<double const *> const & 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. test(b, a.data()); // fixed rank to var rank [ra18]
  160. test_const(c, a.data()); // fixed rank to var rank [ra18]
  161. test_const(b, a.data()); // non-const to const, fixed rank to var rank [ra29]
  162. test_const_ref(a, a.data()); // non-const to const, keeping var rank [ra29]
  163. }
  164. return tr.summary();
  165. }