test_ProcessorStandard.cpp 8.2 KB

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