iterator-small.cc 3.1 KB

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