concrete.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file concrete.cc
  3. /// @brief Tests for concrete_type.
  4. // (c) Daniel Llorens - 2017
  5. // This library is free software; you can redistribute it and/or modify it under
  6. // the terms of the GNU Lesser General Public License as published by the Free
  7. // Software Foundation; either version 3 of the License, or (at your option) any
  8. // later version.
  9. #include "ra/ra.hh"
  10. #include "ra/test.hh"
  11. #include "ra/mpdebug.hh"
  12. #include <memory>
  13. using std::cout, std::endl;
  14. int main()
  15. {
  16. ra::TestRecorder tr(std::cout);
  17. tr.section("scalars");
  18. {
  19. int a = 3;
  20. int b = 4;
  21. using K = ra::concrete_type<decltype(a+b)>;
  22. cout << mp::type_name<K>() << endl;
  23. tr.info("scalars are their own concrete_types").test(std::is_same_v<K, int>);
  24. auto c = ra::concrete(a+b);
  25. tr.test(std::is_same_v<decltype(c), K>);
  26. tr.test_eq(a+b, c);
  27. auto d = ra::concrete(a);
  28. d = 99;
  29. tr.info("concrete() makes copies (", d, ")").test_eq(a, 3);
  30. tr.test_eq(ra::Small<int, 0> {}, ra::shape(d));
  31. }
  32. tr.section("fixed size, rank==1");
  33. {
  34. ra::Small<int, 3> a = {1, 2, 3};
  35. ra::Small<int, 3> b = {4, 5, 6};
  36. using K = ra::concrete_type<decltype(a+b)>;
  37. tr.test(std::is_same_v<K, ra::Small<int, 3>>);
  38. auto c = concrete(a+b);
  39. tr.test(std::is_same_v<decltype(c), K>);
  40. tr.test_eq(a+b, c);
  41. tr.test_eq(ra::Small<int, 1> {3}, ra::shape(a+b));
  42. tr.test_eq(ra::Small<int, 1> {3}, ra::shape(c));
  43. }
  44. tr.section("fixed size, rank>1");
  45. {
  46. ra::Small<int, 2, 3> a = {{1, 2, 3}, {4, 5, 6}};
  47. ra::Small<int, 2, 3> b = {{10, 20, 30}, {40, 50, 60}};
  48. using K = ra::concrete_type<decltype(a+b)>;
  49. tr.test(std::is_same_v<K, ra::Small<int, 2, 3>>);
  50. auto c = concrete(a+b);
  51. tr.test(std::is_same_v<decltype(c), K>);
  52. tr.test_eq(a+b, c);
  53. tr.test_eq(ra::Small<int, 2> {2, 3}, ra::shape(a+b));
  54. tr.test_eq(ra::Small<int, 2> {2, 3}, ra::shape(c));
  55. }
  56. tr.section("var size I");
  57. {
  58. ra::Big<int> a = {1, 2, 3};
  59. tr.info(a.size(0)).test_eq(ra::Small<int, 1> {3}, ra::shape(a));
  60. tr.info(a.size(0)).test_eq(ra::Small<int, 1> {3}, ra::shape(ra::Big<int> {1, 2, 3}));
  61. }
  62. tr.section("var size II");
  63. {
  64. ra::Big<int, 1> a = {1, 2, 3};
  65. ra::Big<int, 1> b = {4, 5, 6};
  66. using K = ra::concrete_type<decltype(a+b)>;
  67. tr.test(std::is_same_v<K, ra::Big<int, 1>>);
  68. auto c = concrete(a+b);
  69. tr.test(std::is_same_v<decltype(c), K>);
  70. tr.test_eq(a+b, c);
  71. tr.test_eq(ra::Small<int, 1> {3}, ra::shape(a+b));
  72. cout << ra::start(c) << endl;
  73. tr.info(c.size(0)).test_eq(ra::Small<int, 1> {3}, ra::shape(c));
  74. }
  75. tr.section("var size + fixed size");
  76. {
  77. ra::Small<int, 3, 2> a = {1, 2, 3, 4, 5, 6};
  78. ra::Big<int, 1> b = {4, 5, 6};
  79. using K = ra::concrete_type<decltype(a+b)>;
  80. tr.test(std::is_same_v<K, ra::Small<int, 3, 2>>);
  81. auto c = concrete(a+b);
  82. tr.test(std::is_same_v<decltype(c), K>);
  83. tr.test_eq(a+b, c);
  84. tr.test_eq(ra::Small<int, 2> {3, 2}, ra::shape(a+b));
  85. tr.test_eq(ra::Small<int, 2> {3, 2}, ra::shape(c));
  86. }
  87. tr.section("var size + var rank");
  88. {
  89. ra::Big<int, 1> a = {1, 2, 3};
  90. ra::Big<int> b = {4, 5, 6};
  91. using K = ra::concrete_type<decltype(a+b)>;
  92. // ra:: b could be higher rank and that decides the type.
  93. tr.test(std::is_same_v<K, ra::Big<int>>);
  94. auto c = concrete(a+b);
  95. tr.test(std::is_same_v<decltype(c), K>);
  96. tr.test_eq(a+b, c);
  97. tr.test_eq(ra::Small<int, 1> {3}, ra::shape(a+b));
  98. tr.test_eq(ra::Small<int, 1> {3}, ra::shape(c));
  99. }
  100. tr.section("concrete on is_slice fixed size");
  101. {
  102. ra::Small<int, 3> a = {1, 2, 3};
  103. auto c = concrete(a);
  104. using K = decltype(c);
  105. tr.test(std::is_same_v<K, ra::Small<int, 3>>);
  106. tr.test(std::is_same_v<decltype(c), K>);
  107. tr.test_eq(a, c);
  108. a = 99;
  109. tr.test_eq(99, a);
  110. tr.info("concrete() makes copies").test_eq(K {1, 2, 3}, c);
  111. tr.test_eq(ra::Small<int, 1> {3}, ra::shape(a));
  112. tr.test_eq(ra::Small<int, 1> {3}, ra::shape(c));
  113. }
  114. tr.section("concrete on is_slice var size");
  115. {
  116. ra::Big<int, 1> a = {1, 2, 3};
  117. auto c = concrete(a);
  118. using K = decltype(c);
  119. tr.test(std::is_same_v<K, ra::Big<int, 1>>);
  120. tr.test(std::is_same_v<decltype(c), K>);
  121. tr.test_eq(a, c);
  122. a = 99;
  123. tr.test_eq(99, a);
  124. tr.info("concrete() makes copies").test_eq(K {1, 2, 3}, c);
  125. tr.test_eq(ra::Small<int, 1> {3}, ra::shape(a));
  126. tr.test_eq(ra::Small<int, 1> {3}, ra::shape(c));
  127. }
  128. tr.section("concrete on foreign vector");
  129. {
  130. std::vector<int> a = {1, 2, 3};
  131. auto c = ra::concrete(a);
  132. using K = decltype(c);
  133. tr.test(std::is_same_v<K, ra::Big<int, 1>>);
  134. tr.test(std::is_same_v<decltype(c), K>);
  135. tr.test_eq(a, c);
  136. ra::start(a) = 99;
  137. tr.test_eq(99, ra::start(a));
  138. tr.info("concrete() makes copies").test_eq(K {1, 2, 3}, c);
  139. tr.test_eq(ra::Small<int, 1> {3}, ra::shape(a));
  140. tr.test_eq(ra::Small<int, 1> {3}, ra::shape(c));
  141. }
  142. tr.section("concrete on scalar");
  143. {
  144. int a = 9;
  145. auto b = ra::with_same_shape(a, 8);
  146. tr.test_eq(8, b);
  147. tr.test_eq(9, a);
  148. b = 7;
  149. tr.test_eq(7, b);
  150. tr.test_eq(9, a);
  151. }
  152. tr.section("concrete on nested array");
  153. {
  154. ra::Big<ra::Small<int, 3>, 1> x({2}, 1);
  155. cout << x << endl;
  156. cout << concrete(x) << endl;
  157. cout << x*double(2.) << endl;
  158. // cout << concrete(x*double(2.)) << endl; // FIXME fails [ra41]
  159. tr.test_eq(ra::Small<int, 1> {2}, ra::shape(x));
  160. }
  161. return tr.summary();
  162. }