iterator-small.cc 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file iterator-small.cc
  3. /// @brief Higher-rank iterator for SmallArray/SmallView.
  4. // (c) Daniel Llorens - 2014, 2016
  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 <iterator>
  11. #include "ra/test.hh"
  12. #include "ra/complex.hh"
  13. #include "mpdebug.hh"
  14. using std::cout, std::endl, std::flush, ra::TestRecorder;
  15. int main()
  16. {
  17. TestRecorder tr;
  18. {
  19. tr.section("fiddling");
  20. {
  21. using A = ra::Small<int, 2, 3>;
  22. A a = {{1, 2, 3}, {4, 5, 6}};
  23. cout << a << endl;
  24. using AI0 = typename A::iterator<0>;
  25. cout << "AI0 " << std::is_same_v<int, AI0> << endl;
  26. cout << AI0::rank() << endl;
  27. using AI1 = typename A::iterator<1>;
  28. cout << "AI1 " << std::is_same_v<int, AI1> << endl;
  29. cout << AI1::rank() << endl;
  30. AI0 bi(a.data());
  31. cout << bi.c.data() << endl;
  32. }
  33. tr.section("STL style, should be a pointer for default steps");
  34. {
  35. using A = ra::Small<int, 2, 3>;
  36. A a = {{1, 2, 3}, {4, 5, 6}};
  37. tr.test(std::is_same_v<int *, decltype(a.begin())>);
  38. for (int i=0; auto && ai: a) {
  39. tr.test_eq(i+1, ai);
  40. ++i;
  41. }
  42. }
  43. tr.section("STL style with non-default steps, which keeps indices (as internal detail)");
  44. {
  45. ra::Small<int, 2, 3> a = {{1, 2, 3}, {4, 5, 6}};
  46. auto b = transpose<1, 0>(a);
  47. tr.test_eq(ra::start({0, 0}), ra::start(b.begin().ind));
  48. tr.test_eq(ra::start({0, 1}), ra::start((++b.begin()).ind));
  49. tr.test(!std::is_same_v<int *, decltype(b.begin())>);
  50. int bref[6] = {1, 4, 2, 5, 3, 6};
  51. for (int i=0; auto && bi: b) {
  52. tr.test_eq(bref[i], bi);
  53. ++i;
  54. }
  55. }
  56. tr.section("rank>0");
  57. {
  58. auto test_over
  59. = [&](auto && a)
  60. {
  61. auto test = [&](std::string ref, auto cr)
  62. {
  63. std::ostringstream o;
  64. for_each([&o](auto && a) { o << a << "|"; }, iter<decltype(cr)::value>(a));
  65. tr.test_eq(ra::scalar(ref), ra::scalar(o.str()));
  66. };
  67. test("1|2|3|4|5|6|", ra::int_c<-2>());
  68. test("1 2 3|4 5 6|", ra::int_c<-1>());
  69. test("1|2|3|4|5|6|", ra::int_c<0>());
  70. test("1 2 3|4 5 6|", ra::int_c<1>());
  71. test("1 2 3\n4 5 6|", ra::int_c<2>());
  72. };
  73. tr.section("default steps");
  74. test_over(ra::Small<int, 2, 3> {{1, 2, 3}, {4, 5, 6}});
  75. tr.section("non-default steps");
  76. test_over(ra::transpose<1, 0>(ra::Small<int, 3, 2> {{1, 4}, {2, 5}, {3, 6}}));
  77. }
  78. }
  79. return tr.summary();
  80. }