test_ProcessorStandard.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #include <gtest/gtest.h>
  2. #include <json.hpp>
  3. #include <Registration.h>
  4. #include "ProcessorStandard.h"
  5. //#define private public
  6. using namespace mdd;
  7. namespace TEST_PROCESSOR_STANDARD {
  8. auto regi = Registration();
  9. TEST(ProcessorStandard, CalculateSimpleFormula){
  10. //f4:(f3:(f1:(5*3)-f2:(4+5))/2)==(15-9)/2==6/2==3
  11. auto f1 = regi.generateModule("ModuleMath");
  12. json config = f1->getConfiguration();
  13. config["configure"]["operation"]["value"] = "multiply";
  14. f1->configure(config);
  15. f1->getInput(0)->setValue() = { 5 };
  16. f1->getInput(1)->setValue() = { 3 };
  17. auto f2 = regi.generateModule("ModuleMath");
  18. config = f2->getConfiguration();
  19. config["configure"]["operation"]["value"] = "add";
  20. f2->configure(config);
  21. f2->getInput(0)->setValue() = { 4 };
  22. f2->getInput(1)->setValue() = { 5 };
  23. auto f3 = regi.generateModule("ModuleMath");
  24. config = f3->getConfiguration();
  25. config["configure"]["operation"]["value"] = "subtract";
  26. f3->configure(config);
  27. f3->getInput(0)->connect(f1->getOutput(0));
  28. f3->getInput(1)->connect(f2->getOutput(0));
  29. auto f4 = regi.generateModule("ModuleMath");
  30. config = f4->getConfiguration();
  31. config["configure"]["operation"]["value"] = "divide";
  32. f4->configure(config.dump());
  33. f4->getInput(0)->connect(f3->getOutput(0));
  34. f4->getInput(1)->setValue() = { 2 };
  35. auto processor = std::make_shared<ProcessorStandard>();
  36. //regi.generateModule("ProcessorStandard");
  37. processor->addModule(f1);
  38. processor->addModule(f2);
  39. processor->addModule(f3);
  40. processor->addModule(f4);
  41. processor->getOutputParams().push_back(std::make_shared<Parameter>(&(*processor)));
  42. processor->getOutputParams().at(0)->getInput(0)->connect(f4->getOutput(0));
  43. processor->update();
  44. EXPECT_EQ(f1->getOutput(0)->getValue()[0], 15);
  45. EXPECT_EQ(f2->getOutput(0)->getValue()[0], 9);
  46. EXPECT_EQ(f3->getOutput(0)->getValue()[0], 6);
  47. EXPECT_EQ(f4->getOutput(0)->getValue()[0], 3);
  48. EXPECT_EQ(processor->getOutput(1)->getValue()[0], 3);
  49. //processor->disconnect();
  50. }
  51. TEST(ProcessorStandard, CalculateAdvancedFormula){
  52. //f4:(f3:(f1:(5*3)-f2:(4+5))/2)==(15-9)/2==6/2==3
  53. auto f1 = regi.generateModule("ModuleMath");
  54. json config = f1->getConfiguration();
  55. config["configure"]["operation"]["value"] = "multiply";
  56. f1->configure(config);
  57. f1->getInput(0)->setValue() = { 5 };
  58. f1->getInput(1)->setValue() = { 3 };
  59. auto f2 = regi.generateModule("ModuleMath");
  60. config = f2->getConfiguration();
  61. config["configure"]["operation"]["value"] = "add";
  62. f2->configure(config);
  63. f2->getInput(0)->setValue() = { 4 };
  64. f2->getInput(1)->setValue() = { 5 };
  65. auto f3 = regi.generateModule("ModuleMath");
  66. config = f3->getConfiguration();
  67. config["configure"]["operation"]["value"] = "subtract";
  68. f3->configure(config);
  69. f3->getInput(0)->connect(f1->getOutput(0));
  70. f3->getInput(1)->connect(f2->getOutput(0));
  71. auto f4 = regi.generateModule("ModuleMath");
  72. config = f4->getConfiguration();
  73. config["configure"]["operation"]["value"] = "divide";
  74. f4->configure(config);
  75. f4->getInput(0)->connect(f3->getOutput(0));
  76. f4->getInput(1)->setValue() = { 2 };
  77. auto processor = std::make_shared<ProcessorStandard>();
  78. processor->addModule(f4);
  79. processor->addModule(f3);
  80. processor->addModule(f2);
  81. processor->addModule(f1);
  82. processor->getOutputParams().push_back(std::make_shared<Parameter>(&(*processor)));
  83. processor->getOutputParams().at(0)->getInput(0)->connect(f4->getOutput(0));
  84. processor->update();
  85. //std::cout << test->getOutput(test->getOutputIDs()[1])->getValue()["value"].dump() << std::endl;
  86. EXPECT_EQ(processor->getOutput(1)->getValue()[0], 3);
  87. }
  88. TEST(ProcessorStandard, CalculateExtremeFormula){
  89. //x_0=8, x_i=x_{i-1}/2
  90. auto switchModule = regi.generateModule("ModuleSwitch");
  91. json config = switchModule->getConfiguration();
  92. config["configure"]["size"]["value"] = 2;
  93. switchModule->configure(config.dump());
  94. auto calcModule = regi.generateModule("ModuleMath");
  95. config = calcModule->getConfiguration();
  96. config["configure"]["operation"]["value"] = "divide";
  97. calcModule->configure(config.dump());
  98. std::shared_ptr<ProcessorStandard> processor = std::make_shared<ProcessorStandard>();
  99. processor->addModule(switchModule);
  100. processor->addModule(calcModule);
  101. bool connect;
  102. connect =switchModule->getInput(0)->connect(processor->getIteration());
  103. switchModule->getInput(1)->setValue() = { 8.0 };
  104. connect = switchModule->getInput(2)->connect(calcModule->getOutput(0));
  105. calcModule->getInput(0)->connect(switchModule->getOutput(0));
  106. calcModule->getInput(1)->setValue() = { 2.0 };
  107. processor->getOutputParams().push_back(std::make_shared<Parameter>(&(*processor)));
  108. processor->getOutputParams().at(0)->getInput(0)->connect(calcModule->getOutput(0));
  109. processor->update();
  110. EXPECT_FLOAT_EQ(processor->getOutput(1)->getValue()[0], 0.0);
  111. }
  112. TEST(ProcessorStandard, CalculateAdvancedFormulaWithSTATIC) {
  113. //f4:(f3:(f1:(5*3)-f2:(4+5))/2)==(15-9)/2==6/2==3
  114. auto f1 = regi.generateModule("ModuleMath");
  115. json config = f1->getConfiguration();
  116. config["configure"]["operation"]["value"] = "multiply";
  117. f1->configure(config);
  118. f1->getInput(0)->setValue() = { 5 };
  119. f1->getInput(1)->setValue() = { 3 };
  120. auto f2 = regi.generateModule("ModuleMath");
  121. config = f2->getConfiguration();
  122. config["configure"]["operation"]["value"] = "add";
  123. f2->configure(config);
  124. f2->getInput(0)->setValue() = { 4 };
  125. f2->getInput(1)->setValue() = { 5 };
  126. auto f3 = regi.generateModule("ModuleMath");
  127. config = f3->getConfiguration();
  128. config["configure"]["operation"]["value"] = "subtract";
  129. f3->configure(config);
  130. f3->getInput(0)->connect(f1->getOutput(0));
  131. f3->getInput(1)->connect(f2->getOutput(0));
  132. auto f4 = regi.generateModule("ModuleMath");
  133. config = f4->getConfiguration();
  134. config["configure"]["operation"]["value"] = "divide";
  135. f4->configure(config);
  136. f4->getInput(0)->connect(f3->getOutput(0));
  137. f4->getInput(1)->setValue() = { 2 };
  138. auto processor = std::make_shared<ProcessorStandard>();
  139. config = processor->getConfiguration();
  140. config["configure"]["priority"]["value"] = "static";
  141. processor->configure(config);
  142. processor->addModule(f4);
  143. processor->addModule(f3);
  144. processor->addModule(f2);
  145. processor->addModule(f1);
  146. processor->getOutputParams().push_back(std::make_shared<Parameter>(&(*processor)));
  147. processor->getOutputParams().at(0)->getInput(0)->connect(f4->getOutput(0));
  148. processor->update();
  149. //std::cout << test->getOutput(test->getOutputIDs()[1])->getValue()["value"].dump() << std::endl;
  150. EXPECT_EQ(processor->getOutput(1)->getValue()[0], 3);
  151. }
  152. TEST(ProcessorStandard, PrioritySTATIC) {
  153. //f4:(f3:(f1:(5*3)-f2:(4+5))/2)==(15-9)/2==6/2==3
  154. auto f1 = regi.generateModule("ModuleMath");
  155. json config = f1->getConfiguration();
  156. config["configure"]["operation"]["value"] = "multiply";
  157. f1->configure(config);
  158. f1->getInput(0)->setValue() = { 5 };
  159. f1->getInput(1)->setValue() = { 3 };
  160. auto f2 = regi.generateModule("ModuleMath");
  161. config = f2->getConfiguration();
  162. config["configure"]["operation"]["value"] = "add";
  163. f2->configure(config);
  164. f2->getInput(0)->setValue() = { 4 };
  165. f2->getInput(1)->setValue() = { 5 };
  166. auto f3 = regi.generateModule("ModuleMath");
  167. config = f3->getConfiguration();
  168. config["configure"]["operation"]["value"] = "subtract";
  169. f3->configure(config);
  170. f3->getInput(0)->connect(f1->getOutput(0));
  171. f3->getInput(1)->connect(f2->getOutput(0));
  172. auto f4 = regi.generateModule("ModuleMath");
  173. config = f4->getConfiguration();
  174. config["configure"]["operation"]["value"] = "divide";
  175. f4->configure(config);
  176. f4->getInput(0)->connect(f3->getOutput(0));
  177. f4->getInput(1)->setValue() = { 2 };
  178. auto process_manual = std::make_shared<ProcessorStandard>();
  179. auto process_static = std::make_shared<ProcessorStandard>();
  180. config = process_static->getConfiguration();
  181. config["configure"]["priority"]["value"] = "static";
  182. process_static->configure(config);
  183. process_manual->addModule(f1);
  184. process_manual->addModule(f2);
  185. process_manual->addModule(f3);
  186. process_manual->addModule(f4);
  187. process_manual->getOutputParams().push_back(std::make_shared<Parameter>(&(*process_manual)));
  188. process_manual->getOutputParams().at(0)->getInput(0)->connect(f4->getOutput(0));
  189. process_manual->update();
  190. process_static->addModule(f4);
  191. process_static->addModule(f3);
  192. process_static->addModule(f2);
  193. process_static->addModule(f1);
  194. process_static->getOutputParams().push_back(std::make_shared<Parameter>(&(*process_static)));
  195. process_static->getOutputParams().at(0)->getInput(0)->connect(f4->getOutput(0));
  196. process_static->update();
  197. //std::cout << process_static->getOutput(process_static->getOutputIDs()[1])->getValue()["value"].dump() << std::endl;
  198. EXPECT_TRUE(process_static->getIteration()->getValue()[0] <= process_manual->getIteration()->getValue()[0]);
  199. }
  200. //*/
  201. }