ra-9.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/test - Regressions in array-in-ra::scalar.
  3. // (c) Daniel Llorens - 2017
  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 <iterator>
  10. #include "ra/test.hh"
  11. #include "mpdebug.hh"
  12. using std::cout, std::endl, std::flush, std::tuple, ra::TestRecorder;
  13. using int2 = ra::Small<int, 2>;
  14. using int1 = ra::Small<int, 1>;
  15. int main()
  16. {
  17. TestRecorder tr(std::cout);
  18. tr.section("regression [ra21]");
  19. {
  20. ra::Big<int2, 1> b { {1, 10}, {2, 20}, {3, 30} };
  21. b += ra::scalar(int2 { 1, 0 });
  22. tr.test_eq(ra::Big<int2, 1> { {2, 10}, {3, 20}, {4, 30} }, b);
  23. ra::Big<int2, 1> c { {1, 0}, {2, 0}, {3, 0} };
  24. }
  25. tr.section("regression [ra37]");
  26. {
  27. ra::Big<int2, 1> b { {0, 10}, {0, 20}, {0, 30} };
  28. ra::Big<int2, 1> c { {1, 0}, {2, 0}, {3, 0} };
  29. tr.test_eq(ra::scalar("3\n2 0 3 0 4 0"), ra::scalar(format(c + ra::scalar(int2 { 1, 0 })))); // FIXME try both -O3 -O0
  30. b = c + ra::scalar(int2 { 1, 0 });
  31. tr.test_eq(ra::Big<int2, 1> { {2, 0}, {3, 0}, {4, 0} }, b);
  32. }
  33. tr.section("regression [ra24]");
  34. {
  35. {
  36. tr.test_eq(int1{91}, ra::scalar(int1{88}) + ra::scalar(int1{3}));
  37. }
  38. {
  39. auto a = int1{88};
  40. auto b = int1{3};
  41. tr.test_eq(int1{91}, a+b);
  42. }
  43. {
  44. auto a = ra::scalar(int1{88});
  45. auto b = ra::scalar(int1{3});
  46. tr.test_eq(int1{91}, a+b);
  47. }
  48. {
  49. auto a = ra::scalar(int1{88});
  50. auto b = ra::scalar(int1{3});
  51. tr.test_eq(int1{91}, ra::scalar(a)+ra::scalar(b));
  52. }
  53. }
  54. tr.section("regression [ra25]");
  55. {
  56. ra::Big<int1, 1> c { {7}, {8} };
  57. tr.test_eq(ra::scalar("2\n8 9"), ra::scalar(format(c+1)));
  58. }
  59. tr.section("regression [ra26]"); // This uses Scalar.at().
  60. {
  61. ra::Big<int1, 1> c { {7}, {8} };
  62. tr.test_eq(ra::scalar("2\n8 9"), ra::scalar(format(c+ra::scalar(int1{1}))));
  63. }
  64. tr.section("regression [ra23]");
  65. {
  66. // This is just to show that it works the same way with 'scalar' = int as with 'scalar' = int2.
  67. int x = 2;
  68. ra::Big<int, 1> c { 3, 4 };
  69. ra::scalar(x) += c + ra::scalar(99);
  70. tr.test_eq(2+3+99+4+99, x); // FIXME
  71. }
  72. {
  73. int2 x {2, 0};
  74. ra::Big<int2, 1> c { {1, 3}, {2, 4} };
  75. ra::scalar(x) += c + ra::scalar(int2 { 1, 99 });
  76. tr.test_eq(int2{2, 0}+int2{1, 3}+int2{1, 99}+int2{2, 4}+int2{1, 99}, x);
  77. }
  78. tr.section("ra::start() on foreign types");
  79. {
  80. auto ref = std::array<int, 4> {12, 77, 44, 1};
  81. tr.test_eq(2, expr([](int i) { return i; }, ra::start(std::vector {1, 2, 3})).at(ra::Small<int, 1>{1}));
  82. tr.test_eq(ra::start(ref), expr([](int i) { return i; }, ra::start(std::array {12, 77, 44, 1})));
  83. // [ra1] no need to forward in Expr()/Match()/Pick(), that is done in expr()/pick(). Used to need it bc CTE in older gcc.
  84. tr.test_eq(ra::start(ref), expr([](int i) { return i; }, ra::start(ra::Big<int, 1> {12, 77, 44, 1})));
  85. tr.test_eq(ra::start(ref), expr([](int i) { return i; }, ra::start(std::vector {12, 77, 44, 1})));
  86. {
  87. int c = 0;
  88. ply_ravel(expr([&c](int i) { c += i; }, ra::start(ra::Unique<int, 1> {12, 77, 44, 1})));
  89. tr.test_eq(12+77+44+1, c);
  90. }
  91. {
  92. int c = 0;
  93. for_each([&c](int i) { c += i; }, ra::Unique<int, 1> {12, 77, 44, 1});
  94. tr.test_eq(12+77+44+1, c);
  95. }
  96. }
  97. // ra::Vector() constructors are needed for V=std::array (cf test/vector-array.cc).
  98. tr.section("[ra35] - reference");
  99. {
  100. std::array a1 = {1, 2};
  101. std::vector a2 = {1, 2};
  102. for_each([](auto && a, auto && b) { a = b; }, ra::ptr(a1), 99);
  103. tr.test_eq(99, ra::start(a1));
  104. }
  105. tr.section("[ra35] - value");
  106. {
  107. auto fun1 = [] { return std::array {7, 2}; };
  108. auto fun2 = [] { return std::vector {5, 2}; };
  109. tr.test_eq(ra::start({7, 2}), ra::ptr(fun1()));
  110. tr.test_eq(ra::start({5, 2}), ra::ptr(fun2()));
  111. }
  112. tr.section("self assigment of ra::ptr");
  113. {
  114. auto a0 = std::array {0, 10};
  115. auto a1 = std::array {1, 11};
  116. ra::ptr(a0) = ra::ptr(a1);
  117. tr.test_eq(1, a0[0]);
  118. tr.test_eq(11, a0[1]);
  119. tr.test_eq(1, a1[0]);
  120. tr.test_eq(11, a1[1]);
  121. }
  122. // This used to be supported, but it really made no sense. Now v0 is a read-only location so this will ct error [ra42]
  123. // {
  124. // auto fun1 = [](int a) { return std::array {a, a+10}; };
  125. // auto v0 = ra::ptr(fun1(0));
  126. // auto v1 = ra::ptr(fun1(1));
  127. // v0 = v1;
  128. // tr.test_eq(1, v0.at(std::array { 0 }));
  129. // tr.test_eq(11, v0.at(std::array { 1 }));
  130. // tr.test_eq(1, v1.at(std::array { 0 }));
  131. // tr.test_eq(11, v1.at(std::array { 1 }));
  132. // v1 += v0;
  133. // tr.test_eq(1, v0.at(std::array { 0 }));
  134. // tr.test_eq(11, v0.at(std::array { 1 }));
  135. // tr.test_eq(2, v1.at(std::array { 0 }));
  136. // tr.test_eq(22, v1.at(std::array { 1 }));
  137. // }
  138. return tr.summary();
  139. }