concrete.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/test - Tests for concrete(), concrete_type.
  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 <memory>
  9. #include "ra/test.hh"
  10. #include "mpdebug.hh"
  11. using std::cout, std::endl;
  12. struct ZZ { int x = 0; };
  13. int main()
  14. {
  15. ra::TestRecorder tr(std::cout);
  16. tr.section("builtin array");
  17. {
  18. int const a[3] = { 1, 2, 3 };
  19. auto b = ra::concrete(a);
  20. b = -b;
  21. tr.test_eq(ra::start({1, 2, 3}), a);
  22. tr.test_eq(ra::start({-1, -2, -3}), b);
  23. }
  24. tr.section("scalars");
  25. {
  26. int a = 3;
  27. int b = 4;
  28. using K = ra::concrete_type<decltype(a+b)>;
  29. cout << ra::mp::type_name<K>() << endl;
  30. tr.info("scalars are their own concrete_types").test(std::is_same_v<K, int>);
  31. auto c = ra::concrete(a+b);
  32. tr.test(std::is_same_v<decltype(c), K>);
  33. tr.test_eq(a+b, c);
  34. auto d = ra::concrete(a);
  35. d = 99;
  36. tr.info("concrete() makes copies (", d, ")").test_eq(a, 3);
  37. tr.test_eq(ra::Small<int, 0> {}, ra::shape(d));
  38. }
  39. tr.section("unregistered scalar types");
  40. {
  41. tr.test(std::is_same_v<ra::concrete_type<ZZ>, ZZ>);
  42. }
  43. tr.section("fixed size, rank==1");
  44. {
  45. ra::Small<int, 3> a = {1, 2, 3};
  46. ra::Small<int, 3> b = {4, 5, 6};
  47. using K = ra::concrete_type<decltype(a+b)>;
  48. tr.test(std::is_same_v<K, ra::Small<int, 3>>);
  49. auto c = concrete(a+b);
  50. tr.test(std::is_same_v<decltype(c), K>);
  51. tr.test_eq(a+b, c);
  52. tr.test_eq(ra::Small<int, 1> {3}, ra::shape(a+b));
  53. tr.test_eq(ra::Small<int, 1> {3}, ra::shape(c));
  54. }
  55. tr.section("fixed size, rank>1");
  56. {
  57. ra::Small<int, 2, 3> a = {{1, 2, 3}, {4, 5, 6}};
  58. ra::Small<int, 2, 3> b = {{10, 20, 30}, {40, 50, 60}};
  59. using K = ra::concrete_type<decltype(a+b)>;
  60. tr.test(std::is_same_v<K, ra::Small<int, 2, 3>>);
  61. auto c = concrete(a+b);
  62. tr.test(std::is_same_v<decltype(c), K>);
  63. tr.test_eq(a+b, c);
  64. tr.test_eq(ra::Small<int, 2> {2, 3}, ra::shape(a+b));
  65. tr.test_eq(ra::Small<int, 2> {2, 3}, ra::shape(c));
  66. }
  67. tr.section("var size I");
  68. {
  69. ra::Big<int> a = {1, 2, 3};
  70. tr.info(a.len(0)).test_eq(ra::Small<int, 1> {3}, ra::shape(a));
  71. tr.info(a.len(0)).test_eq(ra::Small<int, 1> {3}, ra::shape(ra::Big<int> {1, 2, 3}));
  72. }
  73. tr.section("var size II");
  74. {
  75. ra::Big<int, 1> a = {1, 2, 3};
  76. ra::Big<int, 1> b = {4, 5, 6};
  77. using K = ra::concrete_type<decltype(a+b)>;
  78. tr.test(std::is_same_v<K, ra::Big<int, 1>>);
  79. auto c = concrete(a+b);
  80. tr.test(std::is_same_v<decltype(c), K>);
  81. tr.test_eq(a+b, c);
  82. tr.test_eq(ra::Small<int, 1> {3}, ra::shape(a+b));
  83. cout << ra::start(c) << endl;
  84. tr.info(c.len(0)).test_eq(ra::Small<int, 1> {3}, ra::shape(c));
  85. }
  86. tr.section("var size + fixed size");
  87. {
  88. ra::Small<int, 3, 2> a = {1, 2, 3, 4, 5, 6};
  89. ra::Big<int, 1> b = {4, 5, 6};
  90. using K = ra::concrete_type<decltype(a+b)>;
  91. tr.test(std::is_same_v<K, ra::Small<int, 3, 2>>);
  92. auto c = concrete(a+b);
  93. tr.test(std::is_same_v<decltype(c), K>);
  94. tr.test_eq(a+b, c);
  95. tr.test_eq(ra::Small<int, 2> {3, 2}, ra::shape(a+b));
  96. tr.test_eq(ra::Small<int, 2> {3, 2}, ra::shape(c));
  97. }
  98. tr.section("var size + var rank");
  99. {
  100. ra::Big<int, 1> a = {1, 2, 3};
  101. ra::Big<int> b = {4, 5, 6};
  102. using K = ra::concrete_type<decltype(a+b)>;
  103. // ra:: b could be higher rank and that decides the type.
  104. tr.test(std::is_same_v<K, ra::Big<int>>);
  105. auto c = concrete(a+b);
  106. tr.test(std::is_same_v<decltype(c), K>);
  107. tr.test_eq(a+b, c);
  108. tr.test_eq(ra::Small<int, 1> {3}, ra::shape(a+b));
  109. tr.test_eq(ra::Small<int, 1> {3}, ra::shape(c));
  110. }
  111. tr.section("concrete on SliceConcept fixed size");
  112. {
  113. ra::Small<int, 3> a = {1, 2, 3};
  114. auto c = concrete(a);
  115. using K = decltype(c);
  116. tr.test(std::is_same_v<K, ra::Small<int, 3>>);
  117. tr.test(std::is_same_v<decltype(c), K>);
  118. tr.test_eq(a, c);
  119. a = 99;
  120. tr.test_eq(99, a);
  121. tr.info("concrete() makes copies").test_eq(K {1, 2, 3}, c);
  122. tr.test_eq(ra::Small<int, 1> {3}, ra::shape(a));
  123. tr.test_eq(ra::Small<int, 1> {3}, ra::shape(c));
  124. }
  125. tr.section("concrete on SliceConcept var size");
  126. {
  127. ra::Big<int, 1> a = {1, 2, 3};
  128. auto c = concrete(a);
  129. using K = decltype(c);
  130. tr.test(std::is_same_v<K, ra::Big<int, 1>>);
  131. tr.test(std::is_same_v<decltype(c), K>);
  132. tr.test_eq(a, c);
  133. a = 99;
  134. tr.test_eq(99, a);
  135. tr.info("concrete() makes copies").test_eq(K {1, 2, 3}, c);
  136. tr.test_eq(ra::Small<int, 1> {3}, ra::shape(a));
  137. tr.test_eq(ra::Small<int, 1> {3}, ra::shape(c));
  138. }
  139. tr.section("concrete on foreign vector");
  140. {
  141. std::vector<int> a = {1, 2, 3};
  142. auto c = ra::concrete(a);
  143. using K = decltype(c);
  144. tr.test(std::is_same_v<K, ra::Big<int, 1>>);
  145. tr.test(std::is_same_v<decltype(c), K>);
  146. tr.test_eq(a, c);
  147. ra::start(a) = 99;
  148. tr.test_eq(99, ra::start(a));
  149. tr.info("concrete() makes copies").test_eq(K {1, 2, 3}, c);
  150. tr.test_eq(ra::Small<int, 1> {3}, ra::shape(a));
  151. tr.test_eq(ra::Small<int, 1> {3}, ra::shape(c));
  152. }
  153. tr.section("concrete on scalar");
  154. {
  155. int a = 9;
  156. auto b = ra::with_same_shape(a, 8);
  157. tr.test_eq(8, b);
  158. tr.test_eq(9, a);
  159. b = 7;
  160. tr.test_eq(7, b);
  161. tr.test_eq(9, a);
  162. }
  163. tr.section("concrete on nested array");
  164. {
  165. ra::Big<ra::Small<int, 3>, 1> x({2}, 1);
  166. cout << x << endl;
  167. cout << concrete(x) << endl;
  168. cout << x*double(2.) << endl;
  169. // cout << concrete(x*double(2.)) << endl; // FIXME fails [ra41]
  170. tr.test_eq(ra::Small<int, 1> {2}, ra::shape(x));
  171. }
  172. return tr.summary();
  173. }