owned.cc 2.8 KB

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