swap.cc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/test - Test for swap(containers).
  3. // (c) Daniel Llorens - 2021
  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, std::flush, ra::TestRecorder;
  11. int main()
  12. {
  13. TestRecorder tr;
  14. auto test =
  15. [&](auto & a0, auto & a1, auto & b0, auto & b1)
  16. {
  17. auto ap = a1.data();
  18. auto bp = b1.data();
  19. tr.test_eq(a0, a1);
  20. tr.test_eq(b0, b1);
  21. swap(a1, b1);
  22. tr.test_eq(a0, b1);
  23. tr.test_eq(b0, a1);
  24. tr.test_eq(ra::scalar(ap), ra::scalar(b1.data()));
  25. tr.test_eq(ra::scalar(bp), ra::scalar(a1.data()));
  26. swap(b1, a1);
  27. tr.test_eq(a0, a1);
  28. tr.test_eq(b0, b1);
  29. tr.test_eq(ra::scalar(ap), ra::scalar(a1.data()));
  30. tr.test_eq(ra::scalar(bp), ra::scalar(b1.data()));
  31. };
  32. tr.section("swap ct and rt");
  33. {
  34. ra::Small<int, 2, 3> a0 = 1 + ra::_0 - ra::_1;
  35. ra::Big<int, 2> a1 ({2, 3}, 1 + ra::_0 - ra::_1);
  36. ra::Small<int, 3, 2> b0 = 1 - ra::_0 + ra::_1;
  37. ra::Big<int> b1 ({3, 2}, 1 - ra::_0 + ra::_1);
  38. test(a0, a1, b0, b1);
  39. }
  40. tr.section("swap ct and ct");
  41. {
  42. ra::Small<int, 2, 3> a0 = 1 + ra::_0 - ra::_1;
  43. ra::Big<int, 2> a1 ({2, 3}, 1 + ra::_0 - ra::_1);
  44. ra::Small<int, 3, 2> b0 = 1 - ra::_0 + ra::_1;
  45. ra::Big<int, 2> b1 ({3, 2}, 1 - ra::_0 + ra::_1);
  46. test(a0, a1, b0, b1);
  47. }
  48. tr.section("swap rt and rt");
  49. {
  50. ra::Small<int, 2, 3> a0 = 1 + ra::_0 - ra::_1;
  51. ra::Big<int> a1 ({2, 3}, 1 + ra::_0 - ra::_1);
  52. ra::Small<int, 2, 3, 4> b0 = 1 - ra::_0 + ra::_1 + ra::_2;
  53. ra::Big<int> b1 ({2, 3, 4}, 1 - ra::_0 + ra::_1 + ra::_2);
  54. test(a0, a1, b0, b1);
  55. }
  56. return tr.summary();
  57. }