ownership.cc 4.7 KB

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