test_ProcessorStandard.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include <gtest/gtest.h>
  2. #include <json.hpp>
  3. //#define private public
  4. #include "ProcessorStandard.h"
  5. #include <ModuleSwitch.h>
  6. /*
  7. using namespace mdd;
  8. TEST(ProcessorStandard, CalculateSimpleFormula){
  9. //f4:(f3:(f1:(5*3)-f2:(4+5))/2)==(15-9)/2==6/2==3
  10. std::vector<std::string> inputs;
  11. std::shared_ptr<ModuleMath> f1 = std::make_shared<ModuleMath>(MathOperation::MULTIPLY);
  12. inputs = f1->getInputIDs();
  13. f1->getInput(inputs[0])->setValue() = { 5 };
  14. f1->getInput(inputs[1])->setValue() = { 3 };
  15. std::shared_ptr<ModuleMath> f2 = std::make_shared<ModuleMath>(MathOperation::ADD);
  16. inputs = f2->getInputIDs();
  17. f2->getInput(inputs[0])->setValue() = { 4 };
  18. f2->getInput(inputs[1])->setValue() = { 5 };
  19. std::shared_ptr<ModuleMath> f3 = std::make_shared<ModuleMath>(MathOperation::SUBTRACT);
  20. inputs = f3->getInputIDs();
  21. f3->getInput(inputs[0])->connect(f1->getOutput(f1->getOutputIDs()[0]));
  22. f3->getInput(inputs[1])->connect(f2->getOutput(f2->getOutputIDs()[0]));
  23. std::shared_ptr<ModuleMath> f4 = std::make_shared<ModuleMath>(MathOperation::DIVIDE);
  24. inputs = f4->getInputIDs();
  25. f4->getInput(inputs[0])->connect(f3->getOutput(f3->getOutputIDs()[0]));
  26. f4->getInput(inputs[1])->setValue() = { 2 };
  27. std::shared_ptr<ProcessorStandard> processor = std::make_shared<ProcessorStandard>();
  28. processor->addModule(f1);
  29. processor->addModule(f2);
  30. processor->addModule(f3);
  31. processor->addModule(f4);
  32. processor->addModuleOutput(f4, f4->getOutput(f4->getOutputIDs()[0]));
  33. processor->update();
  34. EXPECT_EQ(f1->getOutput(f1->getOutputIDs()[0])->getValue()[0], 15);
  35. EXPECT_EQ(f2->getOutput(f2->getOutputIDs()[0])->getValue()[0], 9);
  36. EXPECT_EQ(f3->getOutput(f3->getOutputIDs()[0])->getValue()[0], 6);
  37. EXPECT_EQ(f4->getOutput(f4->getOutputIDs()[0])->getValue()[0], 3);
  38. EXPECT_EQ(processor->getOutput(processor->getOutputIDs()[1])->getValue()[0], 3);
  39. }
  40. TEST(ProcessorStandard, CalculateAdvancedFormula){
  41. //f4:(f3:(f1:(5*3)-f2:(4+5))/2)==(15-9)/2==6/2==3
  42. std::vector<std::string> inputs;
  43. std::shared_ptr<ModuleMath> f1 = std::make_shared<ModuleMath>(MathOperation::MULTIPLY);
  44. inputs = f1->getInputIDs();
  45. f1->getInput(inputs[0])->setValue() = { 5 };
  46. f1->getInput(inputs[1])->setValue() = { 3 };
  47. std::shared_ptr<ModuleMath> f2 = std::make_shared<ModuleMath>(MathOperation::ADD);
  48. inputs = f2->getInputIDs();
  49. f2->getInput(inputs[0])->setValue() = { 4 };
  50. f2->getInput(inputs[1])->setValue() = { 5 };
  51. std::shared_ptr<ModuleMath> f3 = std::make_shared<ModuleMath>(MathOperation::SUBTRACT);
  52. inputs = f3->getInputIDs();
  53. f3->getInput(inputs[0])->connect(f1->getOutput(f1->getOutputIDs()[0]));
  54. f3->getInput(inputs[1])->connect(f2->getOutput(f2->getOutputIDs()[0]));
  55. std::shared_ptr<ModuleMath> f4 = std::make_shared<ModuleMath>(MathOperation::DIVIDE);
  56. inputs = f4->getInputIDs();
  57. f4->getInput(inputs[0])->connect(f3->getOutput(f3->getOutputIDs()[0]));
  58. f4->getInput(inputs[1])->setValue() = { 2 };
  59. std::shared_ptr<ProcessorStandard> test = std::make_shared<ProcessorStandard>();
  60. test->addModule(f4);
  61. test->addModule(f3);
  62. test->addModule(f2);
  63. test->addModule(f1);
  64. test->addModuleOutput(f4, f4->getOutput(f4->getOutputIDs()[0]));
  65. test->update();
  66. //std::cout << test->getOutput(test->getOutputIDs()[1])->getValue()["value"].dump() << std::endl;
  67. EXPECT_EQ(test->getOutput(test->getOutputIDs()[1])->getValue()[0], 3);
  68. }
  69. TEST(ProcessorStandard, CalculateExtremeFormula){
  70. //x_0=8, x_i=x_{i-1}/2
  71. std::shared_ptr<ModuleSwitch> switchModule = std::make_shared<ModuleSwitch>();
  72. std::shared_ptr<ModuleMath> calcModule = std::make_shared<ModuleMath>(MathOperation::DIVIDE);
  73. std::shared_ptr<ProcessorStandard> processor = std::make_shared<ProcessorStandard>();
  74. processor->addModule(switchModule);
  75. processor->addModule(calcModule);
  76. bool connect;
  77. connect =switchModule->getInput(switchModule->getInputIDs()[0])->connect(processor->getIteration());
  78. switchModule->getInput(switchModule->getInputIDs()[1])->setValue() = { 8.0 };
  79. connect = switchModule->getInput(switchModule->getInputIDs()[2])->connect(calcModule->getOutput(calcModule->getOutputIDs()[0]));
  80. calcModule->getInput(calcModule->getInputIDs()[0])->connect(switchModule->getOutput(switchModule->getOutputIDs()[0]));
  81. calcModule->getInput(calcModule->getInputIDs()[1])->setValue() = { 2.0 };
  82. processor->addModuleOutput(calcModule,calcModule->getOutput(calcModule->getOutputIDs()[0]));
  83. processor->update();
  84. EXPECT_FLOAT_EQ(processor->getOutput(processor->getOutputIDs()[1])->getValue()[0], 0.0);
  85. }
  86. TEST(ProcessorStandard, CalculateAdvancedFormulaWithSTATIC) {
  87. //f4:(f3:(f1:(5*3)-f2:(4+5))/2)==(15-9)/2==6/2==3
  88. std::vector<std::string> inputs;
  89. std::shared_ptr<ModuleMath> f1 = std::make_shared<ModuleMath>(MathOperation::MULTIPLY);
  90. inputs = f1->getInputIDs();
  91. f1->getInput(inputs[0])->setValue() = { 5 };
  92. f1->getInput(inputs[1])->setValue() = { 3 };
  93. std::shared_ptr<ModuleMath> f2 = std::make_shared<ModuleMath>(MathOperation::ADD);
  94. inputs = f2->getInputIDs();
  95. f2->getInput(inputs[0])->setValue() = { 4 };
  96. f2->getInput(inputs[1])->setValue() = { 5 };
  97. std::shared_ptr<ModuleMath> f3 = std::make_shared<ModuleMath>(MathOperation::SUBTRACT);
  98. inputs = f3->getInputIDs();
  99. f3->getInput(inputs[0])->connect(f1->getOutput(f1->getOutputIDs()[0]));
  100. f3->getInput(inputs[1])->connect(f2->getOutput(f2->getOutputIDs()[0]));
  101. std::shared_ptr<ModuleMath> f4 = std::make_shared<ModuleMath>(MathOperation::DIVIDE);
  102. inputs = f4->getInputIDs();
  103. f4->getInput(inputs[0])->connect(f3->getOutput(f3->getOutputIDs()[0]));
  104. f4->getInput(inputs[1])->setValue() = { 2 };
  105. std::shared_ptr<ProcessorStandard> process_static = std::make_shared<ProcessorStandard>(STATIC);
  106. process_static->addModule(f4);
  107. process_static->addModule(f3);
  108. process_static->addModule(f2);
  109. process_static->addModule(f1);
  110. process_static->addModuleOutput(f4, f4->getOutput(f4->getOutputIDs()[0]));
  111. process_static->update();
  112. //std::cout << test->getOutput(test->getOutputIDs()[1])->getValue()["value"].dump() << std::endl;
  113. EXPECT_EQ(process_static->getOutput(process_static->getOutputIDs()[1])->getValue()[0], 3);
  114. }
  115. TEST(ProcessorStandard, PrioritySTATIC) {
  116. //f4:(f3:(f1:(5*3)-f2:(4+5))/2)==(15-9)/2==6/2==3
  117. std::vector<std::string> inputs;
  118. std::shared_ptr<ModuleMath> f1 = std::make_shared<ModuleMath>(MathOperation::MULTIPLY);
  119. inputs = f1->getInputIDs();
  120. f1->getInput(inputs[0])->setValue() = { 5 };
  121. f1->getInput(inputs[1])->setValue() = { 3 };
  122. std::shared_ptr<ModuleMath> f2 = std::make_shared<ModuleMath>(MathOperation::ADD);
  123. inputs = f2->getInputIDs();
  124. f2->getInput(inputs[0])->setValue() = { 4 };
  125. f2->getInput(inputs[1])->setValue() = { 5 };
  126. std::shared_ptr<ModuleMath> f3 = std::make_shared<ModuleMath>(MathOperation::SUBTRACT);
  127. inputs = f3->getInputIDs();
  128. f3->getInput(inputs[0])->connect(f1->getOutput(f1->getOutputIDs()[0]));
  129. f3->getInput(inputs[1])->connect(f2->getOutput(f2->getOutputIDs()[0]));
  130. std::shared_ptr<ModuleMath> f4 = std::make_shared<ModuleMath>(MathOperation::DIVIDE);
  131. inputs = f4->getInputIDs();
  132. f4->getInput(inputs[0])->connect(f3->getOutput(f3->getOutputIDs()[0]));
  133. f4->getInput(inputs[1])->setValue() = { 2 };
  134. std::shared_ptr<ProcessorStandard> process_manual = std::make_shared<ProcessorStandard>();
  135. process_manual->addModule(f1);
  136. process_manual->addModule(f2);
  137. process_manual->addModule(f3);
  138. process_manual->addModule(f4);
  139. process_manual->addModuleOutput(f4, f4->getOutput(f4->getOutputIDs()[0]));
  140. process_manual->update();
  141. std::shared_ptr<ProcessorStandard> process_static = std::make_shared<ProcessorStandard>(STATIC);
  142. process_static->addModule(f4);
  143. process_static->addModule(f3);
  144. process_static->addModule(f2);
  145. process_static->addModule(f1);
  146. process_static->addModuleOutput(f4, f4->getOutput(f4->getOutputIDs()[0]));
  147. process_static->update();
  148. //std::cout << process_static->getOutput(process_static->getOutputIDs()[1])->getValue()["value"].dump() << std::endl;
  149. EXPECT_TRUE(process_static->getIteration()->getValue()[0] <= process_manual->getIteration()->getValue()[0]);
  150. }
  151. //*/