test-concrete.C 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // (c) Daniel Llorens - 2017
  2. // This library is free software; you can redistribute it and/or modify it under
  3. // the terms of the GNU Lesser General Public License as published by the Free
  4. // Software Foundation; either version 3 of the License, or (at your option) any
  5. // later version.
  6. /// @file test-concrete.C
  7. /// @brief Tests for concrete_type.
  8. #include "ra/operators.H"
  9. #include "ra/io.H"
  10. #include "ra/concrete.H"
  11. #include "ra/test.H"
  12. #include "ra/mpdebug.H"
  13. #include <memory>
  14. using std::cout; using std::endl;
  15. int main()
  16. {
  17. TestRecorder tr(std::cout);
  18. tr.section("scalars");
  19. {
  20. int a = 3;
  21. int b = 4;
  22. using K = ra::concrete_type<decltype(a+b)>;
  23. cout << mp::type_name<K>() << endl;
  24. tr.info("scalars are their own concrete_types").test(std::is_same<K, int>::value);
  25. auto c = ra::concrete(a+b);
  26. tr.test(std::is_same<decltype(c), K>::value);
  27. tr.test_eq(a+b, c);
  28. auto d = ra::concrete(a);
  29. d = 99;
  30. tr.info("concrete() makes copies (", d, ")").test_eq(a, 3);
  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<K, ra::Small<int, 3>>::value);
  38. auto c = concrete(a+b);
  39. tr.test(std::is_same<decltype(c), K>::value);
  40. tr.test_eq(a+b, c);
  41. }
  42. tr.section("var size");
  43. {
  44. ra::Big<int, 1> a = {1, 2, 3};
  45. ra::Big<int, 1> b = {4, 5, 6};
  46. using K = ra::concrete_type<decltype(a+b)>;
  47. tr.test(std::is_same<K, ra::Big<int, 1>>::value);
  48. auto c = concrete(a+b);
  49. tr.test(std::is_same<decltype(c), K>::value);
  50. tr.test_eq(a+b, c);
  51. }
  52. tr.section("var size + fixed size");
  53. {
  54. ra::Small<int, 3, 2> a = {1, 2, 3, 4, 5, 6};
  55. ra::Big<int, 1> b = {4, 5, 6};
  56. using K = ra::concrete_type<decltype(a+b)>;
  57. tr.test(std::is_same<K, ra::Small<int, 3, 2>>::value);
  58. auto c = concrete(a+b);
  59. tr.test(std::is_same<decltype(c), K>::value);
  60. tr.test_eq(a+b, c);
  61. }
  62. tr.section("var size + var rank");
  63. {
  64. ra::Big<int, 1> a = {1, 2, 3};
  65. ra::Big<int> b = {4, 5, 6};
  66. using K = ra::concrete_type<decltype(a+b)>;
  67. // ra:: b could be higher rank and that decides the type.
  68. tr.test(std::is_same<K, ra::Big<int>>::value);
  69. auto c = concrete(a+b);
  70. tr.test(std::is_same<decltype(c), K>::value);
  71. tr.test_eq(a+b, c);
  72. }
  73. tr.section("concrete on is_slice fixed size");
  74. {
  75. ra::Small<int, 3> a = {1, 2, 3};
  76. auto c = concrete(a);
  77. using K = decltype(c);
  78. tr.test(std::is_same<K, ra::Small<int, 3>>::value);
  79. tr.test(std::is_same<decltype(c), K>::value);
  80. tr.test_eq(a, c);
  81. a = 99;
  82. tr.test_eq(99, a);
  83. tr.info("concrete() makes copies").test_eq(K {1, 2, 3}, c);
  84. }
  85. tr.section("concrete on is_slice var size");
  86. {
  87. ra::Big<int, 1> a = {1, 2, 3};
  88. auto c = concrete(a);
  89. using K = decltype(c);
  90. tr.test(std::is_same<K, ra::Big<int, 1>>::value);
  91. tr.test(std::is_same<decltype(c), K>::value);
  92. tr.test_eq(a, c);
  93. a = 99;
  94. tr.test_eq(99, a);
  95. tr.info("concrete() makes copies").test_eq(K {1, 2, 3}, c);
  96. }
  97. tr.section("concrete on foreign vector");
  98. {
  99. std::vector<int> a = {1, 2, 3};
  100. auto c = ra::concrete(a);
  101. using K = decltype(c);
  102. tr.test(std::is_same<K, ra::Big<int, 1>>::value);
  103. tr.test(std::is_same<decltype(c), K>::value);
  104. tr.test_eq(a, c);
  105. ra::start(a) = 99;
  106. tr.test_eq(99, ra::start(a));
  107. tr.info("concrete() makes copies").test_eq(K {1, 2, 3}, c);
  108. }
  109. tr.section("concrete on scalar");
  110. {
  111. int a = 9;
  112. auto b = ra::with_same_shape(a, 8);
  113. tr.test_eq(8, b);
  114. tr.test_eq(9, a);
  115. b = 7;
  116. tr.test_eq(7, b);
  117. tr.test_eq(9, a);
  118. }
  119. return tr.summary();
  120. }