ownership.cc 6.4 KB

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