concrete.C 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file concrete.C
  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.H"
  10. #include "ra/test.H"
  11. #include "ra/mpdebug.H"
  12. #include <memory>
  13. using std::cout, std::endl;
  14. int main()
  15. {
  16. 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");
  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("var size I");
  45. {
  46. ra::Big<int> a = {1, 2, 3};
  47. tr.info(a.size(0)).test_eq(ra::Small<int, 1> {3}, ra::shape(a));
  48. tr.info(a.size(0)).test_eq(ra::Small<int, 1> {3}, ra::shape(ra::Big<int> {1, 2, 3}));
  49. }
  50. tr.section("var size II");
  51. {
  52. ra::Big<int, 1> a = {1, 2, 3};
  53. ra::Big<int, 1> b = {4, 5, 6};
  54. using K = ra::concrete_type<decltype(a+b)>;
  55. tr.test(std::is_same_v<K, ra::Big<int, 1>>);
  56. auto c = concrete(a+b);
  57. tr.test(std::is_same_v<decltype(c), K>);
  58. tr.test_eq(a+b, c);
  59. tr.test_eq(ra::Small<int, 1> {3}, ra::shape(a+b));
  60. cout << ra::start(c) << endl;
  61. tr.info(c.size(0)).test_eq(ra::Small<int, 1> {3}, ra::shape(c));
  62. }
  63. tr.section("var size + fixed size");
  64. {
  65. ra::Small<int, 3, 2> a = {1, 2, 3, 4, 5, 6};
  66. ra::Big<int, 1> b = {4, 5, 6};
  67. using K = ra::concrete_type<decltype(a+b)>;
  68. tr.test(std::is_same_v<K, ra::Small<int, 3, 2>>);
  69. auto c = concrete(a+b);
  70. tr.test(std::is_same_v<decltype(c), K>);
  71. tr.test_eq(a+b, c);
  72. tr.test_eq(ra::Small<int, 2> {3, 2}, ra::shape(a+b));
  73. tr.test_eq(ra::Small<int, 2> {3, 2}, ra::shape(c));
  74. }
  75. tr.section("var size + var rank");
  76. {
  77. ra::Big<int, 1> a = {1, 2, 3};
  78. ra::Big<int> b = {4, 5, 6};
  79. using K = ra::concrete_type<decltype(a+b)>;
  80. // ra:: b could be higher rank and that decides the type.
  81. tr.test(std::is_same_v<K, ra::Big<int>>);
  82. auto c = concrete(a+b);
  83. tr.test(std::is_same_v<decltype(c), K>);
  84. tr.test_eq(a+b, c);
  85. tr.test_eq(ra::Small<int, 1> {3}, ra::shape(a+b));
  86. tr.test_eq(ra::Small<int, 1> {3}, ra::shape(c));
  87. }
  88. tr.section("concrete on is_slice fixed size");
  89. {
  90. ra::Small<int, 3> a = {1, 2, 3};
  91. auto c = concrete(a);
  92. using K = decltype(c);
  93. tr.test(std::is_same_v<K, ra::Small<int, 3>>);
  94. tr.test(std::is_same_v<decltype(c), K>);
  95. tr.test_eq(a, c);
  96. a = 99;
  97. tr.test_eq(99, a);
  98. tr.info("concrete() makes copies").test_eq(K {1, 2, 3}, c);
  99. tr.test_eq(ra::Small<int, 1> {3}, ra::shape(a));
  100. tr.test_eq(ra::Small<int, 1> {3}, ra::shape(c));
  101. }
  102. tr.section("concrete on is_slice var size");
  103. {
  104. ra::Big<int, 1> a = {1, 2, 3};
  105. auto c = concrete(a);
  106. using K = decltype(c);
  107. tr.test(std::is_same_v<K, ra::Big<int, 1>>);
  108. tr.test(std::is_same_v<decltype(c), K>);
  109. tr.test_eq(a, c);
  110. a = 99;
  111. tr.test_eq(99, a);
  112. tr.info("concrete() makes copies").test_eq(K {1, 2, 3}, c);
  113. tr.test_eq(ra::Small<int, 1> {3}, ra::shape(a));
  114. tr.test_eq(ra::Small<int, 1> {3}, ra::shape(c));
  115. }
  116. tr.section("concrete on foreign vector");
  117. {
  118. std::vector<int> a = {1, 2, 3};
  119. auto c = ra::concrete(a);
  120. using K = decltype(c);
  121. tr.test(std::is_same_v<K, ra::Big<int, 1>>);
  122. tr.test(std::is_same_v<decltype(c), K>);
  123. tr.test_eq(a, c);
  124. ra::start(a) = 99;
  125. tr.test_eq(99, ra::start(a));
  126. tr.info("concrete() makes copies").test_eq(K {1, 2, 3}, c);
  127. tr.test_eq(ra::Small<int, 1> {3}, ra::shape(a));
  128. tr.test_eq(ra::Small<int, 1> {3}, ra::shape(c));
  129. }
  130. tr.section("concrete on scalar");
  131. {
  132. int a = 9;
  133. auto b = ra::with_same_shape(a, 8);
  134. tr.test_eq(8, b);
  135. tr.test_eq(9, a);
  136. b = 7;
  137. tr.test_eq(7, b);
  138. tr.test_eq(9, a);
  139. }
  140. tr.section("concrete on nested array");
  141. {
  142. ra::Big<ra::Small<int, 3>, 1> x({2}, 1);
  143. cout << x << endl;
  144. cout << concrete(x) << endl;
  145. cout << x*double(2.) << endl;
  146. // cout << concrete(x*double(2.)) << endl; // FIXME fails [ra41]
  147. tr.test_eq(ra::Small<int, 1> {2}, ra::shape(x));
  148. }
  149. return tr.summary();
  150. }