test-owned.C 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // (c) Daniel Llorens - 2014
  2. // This library is free software; you can redistribute it and/or modify it under
  3. // the terms of the GNU Lesser General Public License as published by the Free
  4. // Software Foundation; either version 3 of the License, or (at your option) any
  5. // later version.
  6. /// @file test-owned.C
  7. /// @brief Array operations limited to ra::Big.
  8. #include <iostream>
  9. #include "ra/complex.H"
  10. #include "ra/test.H"
  11. #include "ra/big.H"
  12. #include "ra/operators.H"
  13. #include "ra/io.H"
  14. #include "ra/mpdebug.H"
  15. #include "ra/format.H"
  16. using std::cout; using std::endl; using std::flush;
  17. using real = double;
  18. int main()
  19. {
  20. TestRecorder tr;
  21. tr.section("resize first dimension");
  22. {
  23. auto test = [&tr](auto const & ref, auto & a, int newsize, int testsize)
  24. {
  25. a.resize(newsize);
  26. tr.test_eq(ref.rank(), a.rank());
  27. tr.test_eq(newsize, a.size(0));
  28. for (int i=1; i<a.rank(); ++i) {
  29. tr.test_eq(ref.size(i), a.size(i));
  30. }
  31. tr.test_eq(ref(ra::iota(testsize)), a(ra::iota(testsize)));
  32. };
  33. {
  34. ra::Big<int, 2> a({5, 3}, ra::_0 - ra::_1);
  35. ra::Big<int, 2> ref = a;
  36. test(ref, a, 5, 5);
  37. test(ref, a, 8, 5);
  38. test(ref, a, 3, 3);
  39. test(ref, a, 5, 3);
  40. }
  41. {
  42. ra::Big<int, 1> a({2}, 3);
  43. a.resize(4, 9);
  44. tr.test_eq(3, a[0]);
  45. tr.test_eq(3, a[1]);
  46. tr.test_eq(9, a[2]);
  47. tr.test_eq(9, a[3]);
  48. }
  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("push back");
  59. {
  60. real check[] = { 2, 3, 4, 7 };
  61. auto test = [&tr, &check](auto && z)
  62. {
  63. tr.test_eq(0, z.size(0));
  64. tr.test_eq(1, z.stride(0));
  65. for (int i=0; i<4; ++i) {
  66. z.push_back(check[i]);
  67. tr.test_eq(i+1, z.size());
  68. for (int j=0; j<=i; ++j) {
  69. tr.test_eq(check[j], z[j]);
  70. }
  71. }
  72. };
  73. test(ra::Big<real, 1>());
  74. ra::Big<real> z = ra::Big<real, 1>();
  75. test(z);
  76. }
  77. return tr.summary();
  78. }