test_ProcessorManual.cpp 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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(f1->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(f1->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(f3, f3->getOutput(f3->getOutputIDs()[0]));
  33. test->update();
  34. EXPECT_EQ(test->getOutput(test->getOutputIDs()[1])->getValue()["value"].get<int>(), 3);
  35. }
  36. TEST(ProcessorManual, CalculateAdvancedFormula){
  37. //f4:(f3:(f1:(5*3)-f2:(4+5))/2)==(15-9)/2==6/2==3
  38. std::vector<std::string> inputs;
  39. std::shared_ptr<ModuleMath> f1 = std::make_shared<ModuleMath>(MULTIPLY);
  40. inputs = f1->getInputIDs();
  41. f1->getInput(inputs[0])->setDefaultValue()["value"] = 5;
  42. f1->getInput(inputs[1])->setDefaultValue()["value"] = 3;
  43. std::shared_ptr<ModuleMath> f2 = std::make_shared<ModuleMath>(ADD);
  44. inputs = f2->getInputIDs();
  45. f2->getInput(inputs[0])->setDefaultValue()["value"] = 4;
  46. f2->getInput(inputs[1])->setDefaultValue()["value"] = 5;
  47. std::shared_ptr<ModuleMath> f3 = std::make_shared<ModuleMath>(SUBTRACT);
  48. inputs = f3->getInputIDs();
  49. f3->getInput(inputs[0])->connect(f1->getOutput(f1->getOutputIDs()[0]));
  50. f3->getInput(inputs[1])->connect(f1->getOutput(f2->getOutputIDs()[0]));
  51. std::shared_ptr<ModuleMath> f4 = std::make_shared<ModuleMath>(DIVIDE);
  52. inputs = f4->getInputIDs();
  53. f4->getInput(inputs[0])->connect(f1->getOutput(f3->getOutputIDs()[0]));
  54. f4->getInput(inputs[1])->setDefaultValue()["value"] = 2;
  55. std::shared_ptr<ProcessorManual> test = std::make_shared<ProcessorManual>();
  56. test->addModule(f4);
  57. test->addModule(f3);
  58. test->addModule(f2);
  59. test->addModule(f1);
  60. test->addModuleOutput(f3, f3->getOutput(f3->getOutputIDs()[0]));
  61. test->update();
  62. EXPECT_EQ(test->getOutput(test->getOutputIDs()[1])->getValue()["value"].get<int>(), 3);
  63. }
  64. TEST(ProcessorManual, CalculateExtremeFormula){
  65. //x_0=8, x_i=x_{i-1}/2
  66. std::shared_ptr<ModuleSwitch> switchModule = std::make_shared<ModuleSwitch>();
  67. std::shared_ptr<ModuleMath> calcModule = std::make_shared<ModuleMath>(DIVIDE);
  68. std::shared_ptr<ProcessorManual> processor = std::make_shared<ProcessorManual>();
  69. processor->addModule(switchModule);
  70. processor->addModule(calcModule);
  71. bool connect;
  72. connect =switchModule->getInput(switchModule->getInputIDs()[0])->connect(processor->getIteration());
  73. switchModule->getInput(switchModule->getInputIDs()[1])->setDefaultValue()["value"] = 8.0;
  74. connect = switchModule->getInput(switchModule->getInputIDs()[2])->connect(calcModule->getOutput(calcModule->getOutputIDs()[0]));
  75. calcModule->getInput(calcModule->getInputIDs()[0])->connect(switchModule->getOutput(switchModule->getInputIDs()[0]));
  76. calcModule->getInput(calcModule->getInputIDs()[1])->setDefaultValue()["value"] = 2.0;
  77. processor->addModuleOutput(calcModule,calcModule->getOutput(calcModule->getOutputIDs()[0]));
  78. processor->update();
  79. EXPECT_FLOAT_EQ(processor->getOutput(processor->getOutputIDs()[1])->getValue()["value"].get<double>(), 0.0);
  80. }