stl-compat.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/test - Using STL algos & types together with ra::.
  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 <ranges>
  9. #include <iostream>
  10. #include <iterator>
  11. #include <span>
  12. #include <version>
  13. #include "ra/test.hh"
  14. using std::cout, std::endl, std::flush, ra::TestRecorder;
  15. int main()
  16. {
  17. TestRecorder tr;
  18. tr.section("random access iterators");
  19. {
  20. // TODO rank-0 begin()/end() in ra::Small
  21. // TODO others?
  22. }
  23. tr.section("copyable iterators, but not random access");
  24. {
  25. {
  26. ra::Big<int, 1> a = { 1, 2, 3 };
  27. ra::Big<int, 1> b = { 0, 0, 0 };
  28. std::transform(a.begin(), a.end(), b.begin(), [](int a) { return -a; });
  29. tr.test_eq(a, -b);
  30. }
  31. {
  32. ra::Big<int, 2> a({2, 3}, ra::_0 - 2*ra::_1);
  33. ra::Big<int, 2> b({2, 3}, 99);
  34. std::transform(a.begin(), a.end(), b.begin(), [](int a) { return -a; });
  35. tr.test_eq(a, -b);
  36. }
  37. {
  38. ra::Small<int, 2, 3> a(ra::_0 - 2*ra::_1);
  39. ra::Small<int, 2, 3> b(99);
  40. std::transform(a.begin(), a.end(), b.begin(), [](int a) { return -a; });
  41. tr.test_eq(a, -b);
  42. }
  43. }
  44. tr.section("raw pointers");
  45. {
  46. ra::Big<int, 1> a = {1, 2, 3};
  47. int b[] = { +1, -1, +1 };
  48. tr.test_eq(ra::Small<int, 3> {2, 1, 4}, a + ra::ptr(b));
  49. ra::ptr(b) = ra::Small<int, 3> {7, 4, 5};
  50. tr.test_eq(ra::Small<int, 3> {7, 4, 5}, ra::ptr(b));
  51. int cp[3] = {1, 2, 3};
  52. // ra::Big<int, 1> c({3}, &cp[0]); // forbidden, confusing for higher rank c (pointer matches as rank 1).
  53. ra::Big<int, 1> c({3}, ra::ptr(cp));
  54. tr.test_eq(ra::Small<int, 3> {1, 2, 3}, c);
  55. ra::Big<int, 1> d(3, ra::ptr(cp)); // alt shape
  56. tr.test_eq(ra::Small<int, 3> {1, 2, 3}, d);
  57. }
  58. tr.section("raw pointers");
  59. {
  60. ra::Big<int, 1> a = {1, 2, 3};
  61. ra::ptr(a.data()) = map([](auto const & a) { return -a; }, ra::iota(3, 1, 9));
  62. tr.test_eq(ra::start({-1, -10, -19}), a);
  63. }
  64. tr.section("ptr with other iterators");
  65. {
  66. std::vector a = {1, 2, 3};
  67. ra::Small<int, 3> b = ra::ptr(a.begin());
  68. tr.test_eq(ra::Small<int, 3> {1, 2, 3}, b);
  69. }
  70. tr.section("[ra12] check that begin() and end() match for empty views");
  71. {
  72. ra::Big<int, 3> aa({0, 2, 3}, 0.);
  73. auto a = aa(ra::all, 1);
  74. tr.test(aa.empty());
  75. tr.test(a.begin()==a.end());
  76. }
  77. tr.section("foreign vectors from std::");
  78. {
  79. tr.info("adapted std::array has static size").test_eq(3, size_s(ra::start(std::array {1, 2, 0})));
  80. tr.info("adapted std::vector has dynamic size").test_eq(ra::ANY, ra::size_s<decltype(ra::start(std::vector {1, 2, 0}))>());
  81. }
  82. tr.section("std::string");
  83. {
  84. tr.info("std::string is is_foreign_vector unless registered as is_scalar")
  85. .test_eq(ra::is_scalar<std::string> ? 0 : 1, ra::rank_s<decltype(std::string("hello"))>());
  86. tr.info("explicit adaption to rank 1 is possible").test_eq(5, size(ra::ptr(std::string("hello"))));
  87. tr.info("note the difference with a char array").test_eq(6, ra::size("hello"));
  88. }
  89. tr.section("other std::ranges");
  90. {
  91. tr.test_eq(15, size(ra::start(std::ranges::iota_view(-5, 10))));
  92. tr.info("adapted std::ranges::iota_view has dynamic size")
  93. .test_eq(ra::ANY, size_s(ra::start(std::ranges::iota_view(-5, 10))));
  94. tr.test_eq(ra::iota(15, -5), std::ranges::iota_view(-5, 10));
  95. }
  96. // tr.section("STLIterator works with arbitrary expr not just views");
  97. // {
  98. // ra::Big<int, 3> aa({4, 2, 3}, ra::_0 - ra::_1 + ra::_2);
  99. // ra::STLIterator(ra::start(aa))
  100. // }
  101. #if __cpp_lib_span >= 202002L
  102. tr.section("std::span");
  103. {
  104. std::vector a = {1, 2, 3, 4};
  105. auto b = std::span(a);
  106. tr.test_eq(1, ra::rank(b));
  107. tr.test_eq(ra::iota(4, 1), b);
  108. }
  109. #endif
  110. return tr.summary();
  111. }