at.cc 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/test - Tests for .at().
  3. // (c) Daniel Llorens - 2021
  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 <vector>
  9. #include <iostream>
  10. #include "ra/test.hh"
  11. using std::cout, std::endl;
  12. int main()
  13. {
  14. ra::TestRecorder tr(std::cout);
  15. auto test = [&](auto && C, auto && A, auto && B, auto && I1, auto && I2)
  16. {
  17. A = 0;
  18. map([&A](auto && i) -> decltype(auto) { return A.at(i); }, I2)
  19. = map([&B](auto && i) -> decltype(auto) { return B.at(i); }, I2);
  20. tr.test_eq(C, A);
  21. A = 0;
  22. map([&A](auto && i) -> decltype(auto) { return A.at(i); }, I2)
  23. = at(B, I2);
  24. tr.test_eq(C, A);
  25. // check that at() takes arbitrary rank-1 expr as index
  26. using int2 = ra::Small<int, 2>;
  27. for (int i=0; i<C.len(0); ++i) {
  28. for (int j=0; j<C.len(1); ++j) {
  29. tr.test_eq(C(i, j), C.at(std::array {i, j}));
  30. tr.test_eq(C(i, j), C.at(int2 {i, 0} + int2 {0, j}));
  31. }
  32. }
  33. A = 0;
  34. at(A, I2)
  35. = map([&B](auto && i) -> decltype(auto) { return B.at(i); }, I2);
  36. tr.test_eq(C, A);
  37. A = 0;
  38. at(A, I2) = at(B, I2);
  39. tr.test_eq(C, A);
  40. A = 0;
  41. at(A, I2) = at(B+1, I2);
  42. tr.test_eq(at(C, I2)+1, at(A, I2));
  43. tr.test_eq(35, sum(at(A, I2)));
  44. tr.test_eq(276, prod(at(A, I2)));
  45. at(A, I1) = std::array { 10, 20 };
  46. tr.test_eq(std::array { 20, 12, 10, 0 }, A(ra::all, 1));
  47. tr.test_eq(std::array { 20, 0, 10, 0 }, A(ra::all, 3));
  48. };
  49. tr.section("Big");
  50. {
  51. ra::Big<int, 2> C = {{0, 0, 0, 0}, {0, 11, 0, 0}, {0, 0, 22, 0}, {0, 0, 0, 0}};
  52. ra::Big<int, 2> A({4, 4}, 0), B({4, 4}, 10*ra::_0 + ra::_1);
  53. ra::Big<ra::Small<int, 1>, 1> I1 = { {2}, {0} };
  54. ra::Big<ra::Small<int, 2>, 1> I2 = { {1, 1}, {2, 2} };
  55. test(C, A, B, I1, I2);
  56. }
  57. tr.section("Small basic");
  58. {
  59. ra::Small<double, 3, 2> s { 1, 4, 2, 5, 3, 6 };
  60. ra::Small<int, 2> i2 { 1, 1 };
  61. ra::Small<int, 1> i1 { 1 };
  62. ra::Small<int, 0> i0 { };
  63. tr.test_eq(2, ra::rank_s<decltype(s.at(i0))>());
  64. tr.test_eq(1, ra::rank_s<decltype(s.at(i1))>());
  65. tr.test_eq(0, ra::rank_s<decltype(s.at(i2))>());
  66. }
  67. tr.section("Small ops");
  68. {
  69. ra::Small<int, 4, 4> C = {{0, 0, 0, 0}, {0, 11, 0, 0}, {0, 0, 22, 0}, {0, 0, 0, 0}};
  70. ra::Small<int, 4, 4> A = 0;
  71. ra::Small<int, 4, 4> B = 10*ra::_0 + ra::_1;
  72. ra::Big<ra::Small<int, 1>, 1> I1 = { {2}, {0} };
  73. ra::Big<ra::Small<int, 2>, 1> I2 = { {1, 1}, {2, 2} };
  74. test(C, A, B, I1, I2);
  75. }
  76. return tr.summary();
  77. }