test_ProcessorManual.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <gtest/gtest.h>
  2. #include <json.hpp>
  3. //#define private public
  4. #include <ProcessorManual.h>
  5. #include <ModuleMath.h>
  6. #include <ModuleSwitch.h>
  7. using namespace mdd;
  8. TEST(ProcessorManual, 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>(MULTIPLY);
  12. inputs = f1->getInputIDs();
  13. f1->getInput(inputs[0])->setDefaultValue()["value"] = 5;
  14. f1->getInput(inputs[1])->setDefaultValue()["value"] = 3;
  15. std::shared_ptr<ModuleMath> f2 = std::make_shared<ModuleMath>(ADD);
  16. inputs = f2->getInputIDs();
  17. f2->getInput(inputs[0])->setDefaultValue()["value"] = 4;
  18. f2->getInput(inputs[1])->setDefaultValue()["value"] = 5;
  19. std::shared_ptr<ModuleMath> f3 = std::make_shared<ModuleMath>(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>(DIVIDE);
  24. inputs = f4->getInputIDs();
  25. f4->getInput(inputs[0])->connect(f3->getOutput(f3->getOutputIDs()[0]));
  26. f4->getInput(inputs[1])->setDefaultValue()["value"] = 2;
  27. std::shared_ptr<ProcessorManual> test = std::make_shared<ProcessorManual>();
  28. test->addModule(f1);
  29. test->addModule(f2);
  30. test->addModule(f3);
  31. test->addModule(f4);
  32. test->addModuleOutput(f4, f4->getOutput(f4->getOutputIDs()[0]));
  33. test->update();
  34. EXPECT_EQ(f1->getOutput(f1->getOutputIDs()[0])->getValue()["value"].get<int>(), 15);
  35. EXPECT_EQ(f2->getOutput(f2->getOutputIDs()[0])->getValue()["value"].get<int>(), 9);
  36. EXPECT_EQ(f3->getOutput(f3->getOutputIDs()[0])->getValue()["value"].get<int>(), 6);
  37. EXPECT_EQ(f4->getOutput(f4->getOutputIDs()[0])->getValue()["value"].get<int>(), 3);
  38. EXPECT_EQ(test->getOutput(test->getOutputIDs()[1])->getValue()["value"].get<int>(), 3);
  39. }
  40. TEST(ProcessorManual, 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>(MULTIPLY);
  44. inputs = f1->getInputIDs();
  45. f1->getInput(inputs[0])->setDefaultValue()["value"] = 5;
  46. f1->getInput(inputs[1])->setDefaultValue()["value"] = 3;
  47. std::shared_ptr<ModuleMath> f2 = std::make_shared<ModuleMath>(ADD);
  48. inputs = f2->getInputIDs();
  49. f2->getInput(inputs[0])->setDefaultValue()["value"] = 4;
  50. f2->getInput(inputs[1])->setDefaultValue()["value"] = 5;
  51. std::shared_ptr<ModuleMath> f3 = std::make_shared<ModuleMath>(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>(DIVIDE);
  56. inputs = f4->getInputIDs();
  57. f4->getInput(inputs[0])->connect(f3->getOutput(f3->getOutputIDs()[0]));
  58. f4->getInput(inputs[1])->setDefaultValue()["value"] = 2;
  59. std::shared_ptr<ProcessorManual> test = std::make_shared<ProcessorManual>();
  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. EXPECT_EQ(test->getOutput(test->getOutputIDs()[1])->getValue()["value"].get<int>(), 3);
  67. }
  68. TEST(ProcessorManual, 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>(DIVIDE);
  72. std::shared_ptr<ProcessorManual> processor = std::make_shared<ProcessorManual>();
  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])->setDefaultValue()["value"] = 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])->setDefaultValue()["value"] = 2.0;
  81. processor->addModuleOutput(calcModule,calcModule->getOutput(calcModule->getOutputIDs()[0]));
  82. processor->update();
  83. EXPECT_FLOAT_EQ(processor->getOutput(processor->getOutputIDs()[1])->getValue()["value"].get<double>(), 0.0);
  84. }