test-ra-nested.C 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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-ra-nested.C
  7. /// @brief Using nested arrays as if they were arrays if higher rank.
  8. // @TODO Make more things work and work efficiently.
  9. #include <iostream>
  10. #include <sstream>
  11. #include <iterator>
  12. #include "ra/complex.H"
  13. #include "ra/test.H"
  14. #include "ra/ra-large.H"
  15. #include "ra/ra-operators.H"
  16. using std::cout; using std::endl; using std::flush;
  17. template <class T> using Vec = ra::Owned<T, 1>;
  18. int main()
  19. {
  20. TestRecorder tr;
  21. section("operators on nested arrays");
  22. {
  23. // default init is required to make vector-of-vector, but I still want Vec {} to mean 'an empty vector' and not a default-init vector.
  24. {
  25. auto c = Vec<int> {};
  26. tr.test_eq(0, c.size(0));
  27. tr.test_eq(0, c.size());
  28. }
  29. {
  30. auto c = Vec<Vec<int>> { Vec<int> {}, Vec<int> {1}, Vec<int> {1, 2} };
  31. std::ostringstream os;
  32. cout << "c: " << c << "\n" << endl;
  33. os << c;
  34. std::istringstream is(os.str());
  35. Vec<Vec<int>> d;
  36. is >> d;
  37. cout << "d: " << d << "\n" << endl;
  38. tr.test_eq(3, d.size());
  39. tr.test_eq(d[0], Vec<int>{});
  40. tr.test_eq(d[1], Vec<int>{1});
  41. tr.test_eq(d[2], Vec<int>{1, 2});
  42. // @TODO Actually nested 'as if higher rank' should allow just (every(c==d)). This is explicit nesting.
  43. tr.test(every(ra::expr([](auto & c, auto & d) { return every(c==d); }, c.iter(), d.iter())));
  44. }
  45. }
  46. return tr.summary();
  47. }