ra-3.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/test - Assignment.
  3. // (c) Daniel Llorens - 2013, 2014
  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, ra::TestRecorder;
  13. using std::tuple;
  14. using real = double;
  15. int main()
  16. {
  17. TestRecorder tr;
  18. tr.section("[ra6] assignment cases with scalar or ANY arguments");
  19. {
  20. tr.section("assignment of 0 rank <- scalar expr");
  21. {
  22. ra::Unique<real, 0> a ({}, ra::scalar(99));
  23. tr.test_eq(99, a());
  24. a = ra::scalar(77);
  25. tr.test_eq(77, a());
  26. }
  27. tr.section("assignment of var rank <- scalar expr");
  28. {
  29. ra::Unique<real> a ({3, 2}, ra::scalar(99));
  30. tr.test_eq(99, a(0, 0));
  31. tr.test_eq(99, a(0, 1));
  32. tr.test_eq(99, a(1, 0));
  33. tr.test_eq(99, a(1, 1));
  34. tr.test_eq(99, a(2, 0));
  35. tr.test_eq(99, a(2, 1));
  36. a = ra::scalar(77);
  37. tr.test_eq(77, a(0, 0));
  38. tr.test_eq(77, a(0, 1));
  39. tr.test_eq(77, a(1, 0));
  40. tr.test_eq(77, a(1, 1));
  41. tr.test_eq(77, a(2, 0));
  42. tr.test_eq(77, a(2, 1));
  43. }
  44. tr.section("assignment of var rank <- lower rank expr I");
  45. {
  46. ra::Unique<real, 1> b ({3}, {1, 2, 3});
  47. ra::Unique<real> a ({3, 2}, ra::scalar(99));
  48. a = b.iter();
  49. tr.test_eq(1, a(0, 0));
  50. tr.test_eq(1, a(0, 1));
  51. tr.test_eq(2, a(1, 0));
  52. tr.test_eq(2, a(1, 1));
  53. tr.test_eq(3, a(2, 0));
  54. tr.test_eq(3, a(2, 1));
  55. }
  56. tr.section("construction of var rank <- lower rank expr II");
  57. {
  58. ra::Unique<real, 2> b ({3, 2}, {1, 2, 3, 4, 5, 6});
  59. ra::Unique<real> a ({3, 2, 4}, ra::scalar(99));
  60. a = b.iter();
  61. for (int i=0; i<3; ++i) {
  62. for (int j=0; j<2; ++j) {
  63. tr.test_eq(i*2+j+1, b(i, j));
  64. for (int k=0; k<4; ++k) {
  65. tr.test_eq(b(i, j), a(i, j, k));
  66. }
  67. }
  68. }
  69. }
  70. // this succeeds because of the two var ranks, the top rank comes first (and so it's selected as driver). TODO Have run time driver selection so this is safe.
  71. tr.section("construction of var rank <- lower rank expr III (var rank)");
  72. {
  73. ra::Unique<real> b ({3}, {1, 2, 3});
  74. ra::Unique<real> a ({3, 2}, ra::scalar(99));
  75. a = b.iter();
  76. tr.test_eq(1, a(0, 0));
  77. tr.test_eq(1, a(0, 1));
  78. tr.test_eq(2, a(1, 0));
  79. tr.test_eq(2, a(1, 1));
  80. tr.test_eq(3, a(2, 0));
  81. tr.test_eq(3, a(2, 1));
  82. }
  83. // driver selection is done at compile time (see Expr::DRIVER). Here it'll be the var rank expr, which results in an error at run time. TODO Do run time driver selection to avoid this error.
  84. // tr.section("construction of var rank <- higher rank expr");
  85. // {
  86. // ra::Unique<real> b ({3, 2}, {1, 2, 3, 4, 5, 6});
  87. // cout << "b: " << b << endl;
  88. // ra::Unique<real> a ({4}, ra::scalar(99));
  89. // a = b.iter();
  90. // cout << "a: " << a << endl;
  91. // }
  92. }
  93. return tr.summary();
  94. }