big-1.C 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file big-1.C
  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.H"
  12. #include "ra/test.H"
  13. using std::cout, std::endl, std::flush;
  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()));
  72. a.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::none);
  103. ra::View<double> b(a);
  104. tr.test_eq(ra::scalar(a.data()), ra::scalar(b.data())); // FIXME? pointers are not ra::scalars.
  105. tr.test_eq(a.rank(), b.rank());
  106. tr.test_eq(a.size(0), b.size(0));
  107. tr.test_eq(a.size(1), b.size(1));
  108. tr.test_eq(a.size(2), b.size(2));
  109. tr.test(every(a==b));
  110. auto test = [&tr](ra::View<double, 3> a, double * p)
  111. {
  112. tr.test_eq(ra::Small<int, 3> {3, 2, 4}, shape(a));
  113. tr.test(p==a.data());
  114. };
  115. auto test_const = [&tr](ra::View<double const, 3> a, double * p)
  116. {
  117. tr.test_eq(ra::Small<int, 3> {3, 2, 4}, shape(a));
  118. tr.test(p==a.data());
  119. };
  120. auto test_const_ref = [&tr](ra::View<double const, 3> const & a, double * p)
  121. {
  122. tr.test_eq(ra::Small<int, 3> {3, 2, 4}, shape(a));
  123. tr.test(p==a.data());
  124. };
  125. test(b, b.data()); // var rank to fixed rank
  126. test_const(b, b.data()); // non-const to const, var rank to fixed rank
  127. test_const_ref(a(), a.data()); // non-const to const, keeping fixed rank
  128. }
  129. tr.section("casts from var rank View");
  130. {
  131. ra::Unique<double> a({3, 2, 4}, ra::none);
  132. ra::View<double, 3> b(a);
  133. tr.test_eq(ra::scalar(a.data()), ra::scalar(b.data())); // FIXME? pointers are not ra::scalars.
  134. tr.test_eq(a.rank(), b.rank());
  135. tr.test_eq(a.size(0), b.size(0));
  136. tr.test_eq(a.size(1), b.size(1));
  137. tr.test_eq(a.size(2), b.size(2));
  138. tr.test(every(a==b));
  139. auto test = [&tr](ra::View<double> a, double * p)
  140. {
  141. tr.test_eq(ra::Small<int, 3> {3, 2, 4}, shape(a));
  142. tr.test(p==a.data());
  143. };
  144. auto test_const = [&tr](ra::View<double const> 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_ref = [&tr](ra::View<double const> 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. test(b, b.data()); // fixed rank to var rank
  155. test_const(b, b.data()); // non-const to const, fixed rank to var rank
  156. test_const_ref(a, a.data()); // non-const to const, keeping var rank
  157. }
  158. return tr.summary();
  159. }