owned.C 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file owned.C
  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/complex.H"
  11. #include "ra/test.H"
  12. #include "ra/big.H"
  13. #include "ra/operators.H"
  14. #include "ra/io.H"
  15. #include "ra/mpdebug.H"
  16. #include "ra/format.H"
  17. using std::cout, std::endl, std::flush;
  18. using real = double;
  19. int main()
  20. {
  21. TestRecorder tr;
  22. tr.section("resize first dimension");
  23. {
  24. auto test = [&tr](auto const & ref, auto & a, int newsize, int testsize)
  25. {
  26. a.resize(newsize);
  27. tr.test_eq(ref.rank(), a.rank());
  28. tr.test_eq(newsize, a.size(0));
  29. for (int i=1; i<a.rank(); ++i) {
  30. tr.test_eq(ref.size(i), a.size(i));
  31. }
  32. tr.test_eq(ref(ra::iota(testsize)), a(ra::iota(testsize)));
  33. };
  34. {
  35. ra::Big<int, 2> a({5, 3}, ra::_0 - ra::_1);
  36. ra::Big<int, 2> ref = a;
  37. test(ref, a, 5, 5);
  38. test(ref, a, 8, 5);
  39. test(ref, a, 3, 3);
  40. test(ref, a, 5, 3);
  41. }
  42. {
  43. ra::Big<int, 1> a({2}, 3);
  44. a.resize(4, 9);
  45. tr.test_eq(3, a[0]);
  46. tr.test_eq(3, a[1]);
  47. tr.test_eq(9, a[2]);
  48. tr.test_eq(9, a[3]);
  49. }
  50. {
  51. 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].
  52. ra::Big<int, 3> ref0 = a;
  53. test(ref0, a, 3, 0);
  54. a = 77.;
  55. ra::Big<int, 3> ref1 = a;
  56. test(ref1, a, 5, 3);
  57. }
  58. }
  59. tr.section("push back");
  60. {
  61. real check[] = { 2, 3, 4, 7 };
  62. auto test = [&tr, &check](auto && z)
  63. {
  64. tr.test_eq(0, z.size(0));
  65. tr.test_eq(1, z.stride(0));
  66. for (int i=0; i<4; ++i) {
  67. z.push_back(check[i]);
  68. tr.test_eq(i+1, z.size());
  69. for (int j=0; j<=i; ++j) {
  70. tr.test_eq(check[j], z[j]);
  71. }
  72. }
  73. };
  74. test(ra::Big<real, 1>());
  75. ra::Big<real> z = ra::Big<real, 1>();
  76. test(z);
  77. }
  78. return tr.summary();
  79. }