ra-9.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 [ra22]");
  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; },
  82. ra::start(std::vector {1, 2, 3})).at(ra::Small<int, 1>{1}));
  83. tr.test_eq(ra::start(ref), expr([](int i) { return i; }, ra::start(std::array {12, 77, 44, 1})));
  84. // [ra1] these require ra::start and ra::Expr to forward in the constructor. Clue for why is in the ra::Unique case below.
  85. tr.test_eq(ra::start(ref), expr([](int i) { return i; }, ra::start(ra::Big<int, 1> {12, 77, 44, 1})));
  86. tr.test_eq(ra::start(ref), expr([](int i) { return i; }, ra::start(std::vector {12, 77, 44, 1})));
  87. // these require ra::start and ra::Expr constructors to forward (otherwise CTE), but this makes sense, as argname is otherwise always an lref.
  88. ply_ravel(expr([](int i) { std::cout << "Bi: " << i << std::endl; return i; },
  89. ra::start(ra::Unique<int, 1> {12, 77, 44, 1})));
  90. }
  91. // ra::Vector() constructors are needed for V=std::array (cf test/vector-array.cc).
  92. tr.section("[ra35] - reference");
  93. {
  94. std::array a1 = {1, 2};
  95. std::vector a2 = {1, 2};
  96. for_each([](auto && a, auto && b) { a = b; }, ra::ptr(a1), 99);
  97. tr.test_eq(99, ra::start(a1));
  98. }
  99. tr.section("[ra35] - value");
  100. {
  101. auto fun1 = [] { return std::array {7, 2}; };
  102. auto fun2 = [] { return std::vector {5, 2}; };
  103. tr.test_eq(ra::start({7, 2}), ra::ptr(fun1()));
  104. tr.test_eq(ra::start({5, 2}), ra::ptr(fun2()));
  105. }
  106. tr.section("self assigment of ra::ptr");
  107. {
  108. auto a0 = std::array {0, 10};
  109. auto a1 = std::array {1, 11};
  110. ra::ptr(a0) = ra::ptr(a1);
  111. tr.test_eq(1, a0[0]);
  112. tr.test_eq(11, a0[1]);
  113. tr.test_eq(1, a1[0]);
  114. tr.test_eq(11, a1[1]);
  115. }
  116. // This used to be supported, but it really made no sense. Now v0 is a read-only location so this will ct error [ra42]
  117. // {
  118. // auto fun1 = [](int a) { return std::array {a, a+10}; };
  119. // auto v0 = ra::ptr(fun1(0));
  120. // auto v1 = ra::ptr(fun1(1));
  121. // v0 = v1;
  122. // tr.test_eq(1, v0.at(std::array { 0 }));
  123. // tr.test_eq(11, v0.at(std::array { 1 }));
  124. // tr.test_eq(1, v1.at(std::array { 0 }));
  125. // tr.test_eq(11, v1.at(std::array { 1 }));
  126. // v1 += v0;
  127. // tr.test_eq(1, v0.at(std::array { 0 }));
  128. // tr.test_eq(11, v0.at(std::array { 1 }));
  129. // tr.test_eq(2, v1.at(std::array { 0 }));
  130. // tr.test_eq(22, v1.at(std::array { 1 }));
  131. // }
  132. return tr.summary();
  133. }