test_devstone.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #define BOOST_TEST_MODULE DEVStoneTests
  2. #include <boost/test/unit_test.hpp>
  3. #include <string>
  4. #include "../src/devstone.hpp"
  5. #define STEP 5
  6. #define MAX_WIDTH 50
  7. #define MAX_DEPTH 50
  8. int expectedAtomics(const std::string& type, int width, int depth) {
  9. auto wFactor = width - 1;
  10. if (type == "HOmod") {
  11. wFactor += (width - 1) * width / 2;
  12. }
  13. return wFactor * (depth - 1) + 1;
  14. }
  15. int expectedEICs(const std::string& type, int width, int depth) {
  16. auto wFactor = width;
  17. if (type == "HO") {
  18. wFactor = width + 1;
  19. } else if (type == "HOmod") {
  20. wFactor = 2 * (width - 1) + 1;
  21. }
  22. return wFactor * (depth - 1) + 1;
  23. }
  24. int expectedICs(const std::string& type, int width, int depth) {
  25. auto wFactor = 0;
  26. if (width > 2 && (type == "HI" || type == "HO")) {
  27. wFactor = (width - 2);
  28. } else if (type == "HOmod") {
  29. wFactor = (width - 1) * (width - 1) + (width - 1) * width / 2;
  30. }
  31. return wFactor * (depth - 1);
  32. }
  33. int expectedEOCs(const std::string& type, int width, int depth) {
  34. return (type == "HO")? width * (depth - 1) + 1 : depth;
  35. }
  36. int expectedInternals(const std::string& type, int width, int depth) {
  37. if (type == "LI") {
  38. return (width - 1) * (depth - 1) + 1;
  39. } else if (type == "HI" || type == "HO") {
  40. return (width - 1) * width / 2 * (depth - 1) + 1;
  41. }
  42. auto n = 1;
  43. for (int d = 1; d < depth; ++d) {
  44. n += (1 + (d - 1) * (width - 1)) * (width - 1) * width / 2 + (width - 1) * (width + (d - 1) * (width - 1));
  45. }
  46. return n;
  47. }
  48. int expectedExternals(const std::string& type, int width, int depth) {
  49. return expectedInternals(type, width, depth);
  50. }
  51. int expectedEvents(const std::string& type, int width, int depth) {
  52. if (type != "HOmod") {
  53. return expectedInternals(type, width, depth);
  54. }
  55. auto n = 1;
  56. if (width > 1 && depth > 1) {
  57. n += 2 * (width - 1);
  58. auto aux = 0;
  59. for (int i = 2; i < depth; ++i) {
  60. aux += 1 + (i - 1) * (width - 1);
  61. }
  62. n += aux * 2 * (width - 1) * (width - 1);
  63. n += (aux + 1) * ((width - 1) * (width - 1) + (width - 2) * (width - 1) / 2);
  64. }
  65. return n;
  66. }
  67. adevs::Simulator<IO_Type> createEngine(DEVStone* devstone) {
  68. return {devstone};
  69. }
  70. [[maybe_unused]] void runSimulation(adevs::Simulator<IO_Type>& sim) {
  71. while (sim.nextEventTime() < DBL_MAX) {
  72. sim.execNextEvent();
  73. }
  74. }
  75. BOOST_AUTO_TEST_CASE(DEVStoneLI)
  76. {
  77. for (int w = 1; w <= MAX_WIDTH; w += STEP) {
  78. for (int d = 1; d <= MAX_DEPTH; d += STEP) {
  79. auto coupled = new DEVStone("LI", w, d, 0, 0);
  80. BOOST_CHECK_EQUAL(coupled->model->nAtomics(), expectedAtomics("LI", w, d));
  81. BOOST_CHECK_EQUAL(coupled->model->nEICs(), expectedEICs("LI", w, d));
  82. BOOST_CHECK_EQUAL(coupled->model->nICs(), expectedICs("LI", w, d));
  83. BOOST_CHECK_EQUAL(coupled->model->nEOCs(), expectedEOCs("LI", w, d));
  84. auto coordinator = createEngine(coupled);
  85. runSimulation(coordinator);
  86. BOOST_CHECK_EQUAL(coupled->model->nInternals(), expectedInternals("LI", w, d));
  87. BOOST_CHECK_EQUAL(coupled->model->nExternals(), expectedExternals("LI", w, d));
  88. BOOST_CHECK_EQUAL(coupled->model->nEvents(), expectedEvents("LI", w, d));
  89. }
  90. }
  91. }
  92. BOOST_AUTO_TEST_CASE(DEVStoneHI)
  93. {
  94. for (int w = 1; w <= MAX_WIDTH; w += STEP) {
  95. for (int d = 1; d <= MAX_DEPTH; d += STEP) {
  96. auto coupled = new DEVStone("HI", w, d, 0, 0);
  97. BOOST_CHECK_EQUAL(coupled->model->nAtomics(), expectedAtomics("HI", w, d));
  98. BOOST_CHECK_EQUAL(coupled->model->nEICs(), expectedEICs("HI", w, d));
  99. BOOST_CHECK_EQUAL(coupled->model->nICs(), expectedICs("HI", w, d));
  100. BOOST_CHECK_EQUAL(coupled->model->nEOCs(), expectedEOCs("HI", w, d));
  101. auto coordinator = createEngine(coupled);
  102. runSimulation(coordinator);
  103. BOOST_CHECK_EQUAL(coupled->model->nInternals(), expectedInternals("HI", w, d));
  104. BOOST_CHECK_EQUAL(coupled->model->nExternals(), expectedExternals("HI", w, d));
  105. BOOST_CHECK_EQUAL(coupled->model->nEvents(), expectedEvents("HI", w, d));
  106. }
  107. }
  108. }
  109. BOOST_AUTO_TEST_CASE(DEVStoneHO)
  110. {
  111. for (int w = 1; w <= MAX_WIDTH; w += STEP) {
  112. for (int d = 1; d <= MAX_DEPTH; d += STEP) {
  113. auto coupled = new DEVStone("HO", w, d, 0, 0);
  114. BOOST_CHECK_EQUAL(coupled->model->nAtomics(), expectedAtomics("HO", w, d));
  115. BOOST_CHECK_EQUAL(coupled->model->nEICs(), expectedEICs("HO", w, d));
  116. BOOST_CHECK_EQUAL(coupled->model->nICs(), expectedICs("HO", w, d));
  117. BOOST_CHECK_EQUAL(coupled->model->nEOCs(), expectedEOCs("HO", w, d));
  118. auto coordinator = createEngine(coupled);
  119. runSimulation(coordinator);
  120. BOOST_CHECK_EQUAL(coupled->model->nInternals(), expectedInternals("HO", w, d));
  121. BOOST_CHECK_EQUAL(coupled->model->nExternals(), expectedExternals("HO", w, d));
  122. BOOST_CHECK_EQUAL(coupled->model->nEvents(), expectedEvents("HO", w, d));
  123. }
  124. }
  125. }
  126. BOOST_AUTO_TEST_CASE(DEVStoneHOmod)
  127. {
  128. for (int w = 1; w <= MAX_WIDTH / 5; w += STEP / 5) {
  129. for (int d = 1; d <= MAX_DEPTH / 5; d += STEP / 5) {
  130. auto coupled = new DEVStone("HOmod", w, d, 0, 0);
  131. BOOST_CHECK_EQUAL(coupled->model->nAtomics(), expectedAtomics("HOmod", w, d));
  132. BOOST_CHECK_EQUAL(coupled->model->nEICs(), expectedEICs("HOmod", w, d));
  133. BOOST_CHECK_EQUAL(coupled->model->nICs(), expectedICs("HOmod", w, d));
  134. BOOST_CHECK_EQUAL(coupled->model->nEOCs(), expectedEOCs("HOmod", w, d));
  135. auto coordinator = createEngine(coupled);
  136. runSimulation(coordinator);
  137. BOOST_CHECK_EQUAL(coupled->model->nInternals(), expectedInternals("HOmod", w, d));
  138. BOOST_CHECK_EQUAL(coupled->model->nExternals(), expectedExternals("HOmod", w, d));
  139. BOOST_CHECK_EQUAL(coupled->model->nEvents(), expectedEvents("HOmod", w, d));
  140. }
  141. }
  142. }