owned.cc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file owned.cc
  3. /// @brief Array operations limited to ra::Big.
  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 "ra/test.hh"
  11. #include "ra/complex.hh"
  12. #include "mpdebug.hh"
  13. using std::cout, std::endl, std::flush, ra::TestRecorder;
  14. using real = double;
  15. int main()
  16. {
  17. TestRecorder tr;
  18. tr.section("resize first dimension");
  19. {
  20. auto test = [&tr](auto const & ref, auto & a, int newsize, int testsize)
  21. {
  22. a.resize(newsize);
  23. tr.test_eq(ref.rank(), a.rank());
  24. tr.test_eq(newsize, a.len(0));
  25. for (int i=1; i<a.rank(); ++i) {
  26. tr.test_eq(ref.len(i), a.len(i));
  27. }
  28. tr.test_eq(ref(ra::iota(testsize)), a(ra::iota(testsize)));
  29. };
  30. tr.section("rank 2");
  31. {
  32. ra::Big<int, 2> a({5, 3}, ra::_0 - ra::_1);
  33. ra::Big<int, 2> ref = a;
  34. test(ref, a, 5, 5);
  35. test(ref, a, 8, 5);
  36. test(ref, a, 3, 3);
  37. test(ref, a, 5, 3);
  38. }
  39. tr.section("rank 1");
  40. {
  41. ra::Big<int, 1> a({2}, 3);
  42. a.resize(4, 9);
  43. tr.test_eq(3, a[0]);
  44. tr.test_eq(3, a[1]);
  45. tr.test_eq(9, a[2]);
  46. tr.test_eq(9, a[3]);
  47. }
  48. tr.section("rank 3");
  49. {
  50. ra::Big<int, 3> a({0, 3, 2}, ra::_0 - ra::_1 + ra::_2); // BUG If <int, 2>, I get [can't drive] instead of [rank error].
  51. ra::Big<int, 3> ref0 = a;
  52. test(ref0, a, 3, 0);
  53. a = 77.;
  54. ra::Big<int, 3> ref1 = a;
  55. test(ref1, a, 5, 3);
  56. }
  57. }
  58. tr.section("full resize");
  59. {
  60. ra::Big<int> a({3, 3, 3, 3}, ra::_0 - ra::_1 + ra::_2 - ra::_3);
  61. ra::Big<int, 1> b = ra::reshape(a, {50});
  62. a.resize(ra::Small<int, 2>{5, 10});
  63. tr.test_eq(5, a.len(0));
  64. tr.test_eq(10, a.len(1));
  65. tr.test_eq(b, ra::reshape(a, {50}));
  66. }
  67. tr.section("push back");
  68. {
  69. real check[] = { 2, 3, 4, 7 };
  70. auto test = [&tr, &check](auto && z)
  71. {
  72. tr.test_eq(0, z.len(0));
  73. tr.test_eq(1, z.step(0));
  74. for (int i=0; i<4; ++i) {
  75. z.push_back(check[i]);
  76. tr.test_eq(i+1, z.size());
  77. for (int j=0; j<=i; ++j) {
  78. tr.test_eq(check[j], z[j]);
  79. }
  80. }
  81. };
  82. test(ra::Big<real, 1>());
  83. ra::Big<real> z = ra::Big<real, 1>();
  84. test(z);
  85. }
  86. return tr.summary();
  87. }