ownership.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/test - Test ownership logic of array types.
  3. // (c) Daniel Llorens - 2014
  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. #include "ra/complex.hh"
  12. using std::cout, std::endl, ra::TestRecorder;
  13. using real = double;
  14. // TODO Test construction both by-value and by-ref, and between types.
  15. // TODO Maybe I want Container/View<T> const and Container/View<T const> to behave differently....
  16. int main()
  17. {
  18. real const check99[5] = {99, 99, 99, 99, 99};
  19. real const check11[5] = {11, 11, 11, 11, 11};
  20. TestRecorder tr;
  21. tr.section("Unique");
  22. {
  23. ra::Unique<real, 1> o({5}, 11.);
  24. tr.test(o.store!=nullptr);
  25. // ra::Unique<real, 1> z(o); // TODO Check that it fails to compile
  26. ra::Unique<real, 1> z(std::move(o));
  27. tr.test(o.store==nullptr);
  28. tr.test(std::equal(check11, check11+5, z.begin())); // was moved
  29. ra::Unique<real, 1> const c(std::move(z));
  30. tr.test(z.store==nullptr);
  31. tr.test(std::equal(check11, check11+5, c.begin())); // was moved
  32. {
  33. ra::Unique<real, 1> o({5}, 11.);
  34. ra::Unique<real, 1> q {};
  35. q = std::move(o);
  36. tr.test(o.store==nullptr);
  37. tr.test(std::equal(check11, check11+5, q.begin())); // was moved
  38. }
  39. }
  40. tr.section("Big");
  41. {
  42. ra::Big<real, 1> o({5}, 11.);
  43. ra::Big<real, 1> z(o);
  44. o = 99.;
  45. tr.test(std::equal(check11, check11+5, z.begin()));
  46. tr.test(std::equal(check99, check99+5, o.begin()));
  47. tr.section("copy");
  48. {
  49. ra::Big<real, 1> const c(o);
  50. tr.test(std::equal(check99, check99+5, c.begin()));
  51. tr.test(c.data()!=o.data());
  52. ra::Big<real, 1> const q(c);
  53. tr.test(q.data()!=c.data());
  54. tr.test(std::equal(check99, check99+5, q.begin()));
  55. ra::Big<real, 1> p(c);
  56. tr.test(p.data()!=c.data());
  57. tr.test(std::equal(check99, check99+5, p.begin()));
  58. }
  59. auto test_container_assigment_op =
  60. [&]<class T>(T type, char const * tag)
  61. {
  62. tr.section("Container operator=(Container) replaces, unlike View [ra20] ", tag);
  63. {
  64. T o({5}, 11.);
  65. T const p({5}, 99.);
  66. {
  67. T q({7}, 4.);
  68. q = o;
  69. tr.test(std::equal(check11, check11+5, q.begin()));
  70. }
  71. {
  72. T q({7}, 4.);
  73. q = p;
  74. tr.test(std::equal(check99, check99+5, q.begin()));
  75. }
  76. {
  77. T q({7}, 4.);
  78. q = T({5}, 11.);
  79. tr.test(std::equal(check11, check11+5, q.begin()));
  80. }
  81. tr.test(std::equal(check99, check99+5, p.begin()));
  82. tr.test(std::equal(check11, check11+5, o.begin()));
  83. }
  84. tr.section("move");
  85. {
  86. T zz = z;
  87. T c(std::move(zz));
  88. tr.test(zz.store.size()==0); // std::vector does this on move...
  89. tr.test(std::equal(check11, check11+5, c.begin())); // was moved
  90. }
  91. };
  92. test_container_assigment_op(ra::Big<real, 1>(), "static rank");
  93. test_container_assigment_op(ra::Big<real>(), "dynamic rank");
  94. }
  95. tr.section("Shared");
  96. {
  97. ra::Shared<real, 1> o({5}, 11.);
  98. ra::Shared<real, 1> z(o);
  99. o = 99.;
  100. tr.test_eq(5, o.size());
  101. tr.test_eq(5, z.size());
  102. tr.test_eq(99, z);
  103. tr.test_eq(99, o);
  104. ra::Shared<real, 1> const c(o);
  105. tr.test_eq(5, c.size());
  106. tr.test_eq(99, c);
  107. tr.test(c.data()==o.data());
  108. ra::Shared<real, 1> const q(c);
  109. tr.test(q.data()==c.data());
  110. ra::Shared<real, 1> p(c); // May be a BUG here; shared_ptr doesn't prevent this copy.
  111. tr.test(p.data()==c.data());
  112. }
  113. // The use of deleters allows Shared to work like View storage wise.
  114. tr.section("Shared with borrowed data");
  115. {
  116. ra::Shared<real, 1> o({5}, 11.);
  117. {
  118. ra::Shared<real, 1> borrower(ra::shared_borrowing(o));
  119. borrower = 99.;
  120. // Check that shared_borrowing really borrowed.
  121. tr.test(o.data()==borrower.data());
  122. }
  123. tr.test_eq(o, 99.);
  124. }
  125. return tr.summary();
  126. }