owned.cc 2.5 KB

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