test-ownership.C 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // (c) Daniel Llorens - 2014
  2. // This library is free software; you can redistribute it and/or modify it under
  3. // the terms of the GNU Lesser General Public License as published by the Free
  4. // Software Foundation; either version 3 of the License, or (at your option) any
  5. // later version.
  6. /// @file test-ownership.C
  7. /// @brief Test ownership logic of array types.
  8. #include <iostream>
  9. #include <iterator>
  10. #include "ra/complex.H"
  11. #include "ra/test.H"
  12. #include "ra/big.H"
  13. #include "ra/operators.H"
  14. #include "ra/io.H"
  15. using std::cout; using std::endl;
  16. using real = double;
  17. // TODO Test construction both by-value and by-ref, and between types.
  18. // TODO Correct and complete this table, both for
  19. // array-type-A::operator=(array-type-B) and for
  20. // array-type-A::array-type::A(array-type-B).
  21. // TODO Do/organize the tests for these tables.
  22. // The constructors are all plain, the fields of the array record are copied/moved.
  23. // TODO This table contain errors; review thoroughly.
  24. /*
  25. | to\fro cons | View | Shared | Unique | Big | Small | SmallView | Tested in |
  26. |-------------+----------------+--------+-----------+-----------+----------------+------------+------------------|
  27. | R | borrow | borrow | borrow | borrow | borrow | ... | |
  28. | S | share, null d. | share | move | share | share, null d. | | test-ownership.C |
  29. | U | copy | copy | move | copy/move | copy | | |
  30. | O | copy | copy | copy/move | copy/move | copy | | |
  31. | Small | | | | | copy | | |
  32. */
  33. // operator= however copies into. This is so that array ops look natural.
  34. // The reason for the diagonal exceptions is to allow array-types to be initialized from a ref argument, which is required in operator>>(istream &, array-type). Maybe there's a better solution.
  35. /*
  36. | to\fro op= | View | Shared | Unique | Big | Small | Tested in |
  37. |------------+-----------+-----------+-----------+-------------+-----------+------------------|
  38. | View | copy into | copy into | copy into | copy into | copy into | test-operators.C |
  39. | Shared | copy into | *share* | copy into | copy into | copy into | |
  40. | Unique | copy into | copy into | *move* | copy into | copy into | |
  41. | Big | copy into | copy into | copy into | *copy/move* | copy into | |
  42. | Small | copy into | copy into | copy into | copy into | *copy* | |
  43. */
  44. // TODO Maybe I want WithStorage/View<T> const and WithStorage/View<T const> to behave differently....
  45. int main()
  46. {
  47. real const check99[5] = {99, 99, 99, 99, 99};
  48. real const check11[5] = {11, 11, 11, 11, 11};
  49. TestRecorder tr;
  50. tr.section("Unique");
  51. {
  52. ra::Unique<real, 1> o({5}, 11.);
  53. tr.test(o.store!=nullptr);
  54. // ra::Unique<real, 1> z(o); // TODO Check that it fails to compile
  55. ra::Unique<real, 1> z(std::move(o));
  56. tr.test(o.store==nullptr);
  57. tr.test(std::equal(check11, check11+5, z.begin())); // was moved
  58. ra::Unique<real, 1> const c(std::move(z));
  59. tr.test(z.store==nullptr);
  60. tr.test(std::equal(check11, check11+5, c.begin())); // was moved
  61. {
  62. ra::Unique<real, 1> o({5}, 11.);
  63. ra::Unique<real, 1> q {};
  64. q = std::move(o);
  65. tr.test(o.store==nullptr);
  66. tr.test(std::equal(check11, check11+5, q.begin())); // was moved
  67. }
  68. }
  69. tr.section("Big");
  70. {
  71. ra::Big<real, 1> o({5}, 11.);
  72. ra::Big<real, 1> z(o);
  73. o = 99.;
  74. tr.test(std::equal(check11, check11+5, z.begin()));
  75. tr.test(std::equal(check99, check99+5, o.begin()));
  76. tr.section("copy");
  77. {
  78. ra::Big<real, 1> const c(o);
  79. tr.test(std::equal(check99, check99+5, c.begin()));
  80. tr.test(c.data()!=o.data());
  81. ra::Big<real, 1> const q(c);
  82. tr.test(q.data()!=c.data());
  83. tr.test(std::equal(check99, check99+5, q.begin()));
  84. ra::Big<real, 1> p(c);
  85. tr.test(p.data()!=c.data());
  86. tr.test(std::equal(check99, check99+5, p.begin()));
  87. }
  88. tr.section("WithStorage operator=(WithStorage) replaces, unlike View [ra20]");
  89. {
  90. ra::Big<real, 1> o({5}, 11.);
  91. ra::Big<real, 1> const p({5}, 99.);
  92. {
  93. ra::Big<real, 1> q({7}, 4.);
  94. q = o;
  95. tr.test(std::equal(check11, check11+5, q.begin()));
  96. }
  97. {
  98. ra::Big<real, 1> q({7}, 4.);
  99. q = p;
  100. tr.test(std::equal(check99, check99+5, q.begin()));
  101. }
  102. {
  103. ra::Big<real, 1> q({7}, 4.);
  104. q = ra::Big<real, 1>({5}, 11.);
  105. tr.test(std::equal(check11, check11+5, q.begin()));
  106. }
  107. tr.test(std::equal(check99, check99+5, p.begin()));
  108. tr.test(std::equal(check11, check11+5, o.begin()));
  109. }
  110. tr.section("move");
  111. {
  112. ra::Big<real, 1> c(std::move(z));
  113. tr.test(z.store.size()==0); // std::vector does this on move...
  114. tr.test(std::equal(check11, check11+5, c.begin())); // was moved
  115. }
  116. }
  117. tr.section("Shared");
  118. {
  119. ra::Shared<real, 1> o({5}, 11.);
  120. ra::Shared<real, 1> z(o);
  121. o = 99.;
  122. tr.test(std::equal(check99, check99+5, z.begin()));
  123. ra::Shared<real, 1> const c(o);
  124. tr.test(std::equal(check99, check99+5, c.begin()));
  125. tr.test(c.data()==o.data());
  126. ra::Shared<real, 1> const q(c);
  127. tr.test(q.data()==c.data());
  128. ra::Shared<real, 1> p(c); // May be a BUG here; shared_ptr doesn't prevent this copy.
  129. tr.test(p.data()==c.data());
  130. }
  131. // The use of deleters allows Shared to work like View storage wise.
  132. tr.section("Shared with borrowed data");
  133. {
  134. ra::Shared<real, 1> o({5}, 11.);
  135. {
  136. ra::Shared<real, 1> borrower(ra::shared_borrowing(o));
  137. borrower = 99.;
  138. // Check that shared_borrowing really borrowed.
  139. tr.test(o.data()==borrower.data());
  140. }
  141. tr.test_eq(o, 99.);
  142. }
  143. return tr.summary();
  144. }