test_ProcessorManual.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. ModuleMath f1 = ModuleMath(MULTIPLY);
  11. f1.setInputDefaultValue(0)["value"] = 5;
  12. f1.setInputDefaultValue(1)["value"] = 3;
  13. ModuleMath f2 = ModuleMath(ADD);
  14. f2.setInputDefaultValue(0)["value"] = 4;
  15. f2.setInputDefaultValue(1)["value"] = 5;
  16. ModuleMath f3 = ModuleMath(SUBTRACT);
  17. f3.connectInput(0,f1.getOutput(0));
  18. f3.connectInput(1,f2.getOutput(0));
  19. ModuleMath f4 = ModuleMath(DIVIDE);
  20. f4.connectInput(0,f3.getOutput(0));
  21. f4.setInputDefaultValue(1)["value"] = 2;
  22. ProcessorManual test = ProcessorManual();
  23. test.addModule(std::make_shared<ModuleMath>(f1));
  24. test.addModule(std::make_shared<ModuleMath>(f2));
  25. test.addModule(std::make_shared<ModuleMath>(f3));
  26. test.addModule(std::make_shared<ModuleMath>(f4));
  27. test.addOutput(3,0);
  28. test.update();
  29. EXPECT_EQ(test.getOutput(0)->getValue()["value"].get<int>(), 3);
  30. }
  31. TEST(ProcessorManual, CalculateAdvancedFormula){
  32. //f4:(f3:(f1:(5*3)-f2:(4+5))/2)==(15-9)/2==6/2==3
  33. ModuleMath f1 = ModuleMath(MULTIPLY);
  34. f1.setInputDefaultValue(0)["value"] = 5;
  35. f1.setInputDefaultValue(1)["value"] = 3;
  36. ModuleMath f2 = ModuleMath(ADD);
  37. f2.setInputDefaultValue(0)["value"] = 4;
  38. f2.setInputDefaultValue(1)["value"] = 5;
  39. ModuleMath f3 = ModuleMath(SUBTRACT);
  40. f3.connectInput(0,f1.getOutput(0));
  41. f3.connectInput(1,f2.getOutput(0));
  42. ModuleMath f4 = ModuleMath(DIVIDE);
  43. f4.connectInput(0,f3.getOutput(0));
  44. f4.setInputDefaultValue(1)["value"] = 2;
  45. ProcessorManual test = ProcessorManual();
  46. test.addModule(std::make_shared<ModuleMath>(f4));
  47. test.addModule(std::make_shared<ModuleMath>(f3));
  48. test.addModule(std::make_shared<ModuleMath>(f2));
  49. test.addModule(std::make_shared<ModuleMath>(f1));
  50. test.addOutput(0,0);
  51. test.update();
  52. EXPECT_EQ(test.getOutput(0)->getValue()["value"].get<int>(), 3);
  53. }
  54. TEST(ProcessorManual, CalculateExtremeFormula){
  55. //x_0=8, x_i=x_{i-1}/2
  56. ModuleSwitch switchModule = ModuleSwitch();
  57. ModuleMath calcModule = ModuleMath(DIVIDE);
  58. ProcessorManual processor = ProcessorManual();
  59. processor.addModule(std::make_shared<ModuleSwitch>(switchModule));
  60. processor.addModule(std::make_shared<ModuleMath>(calcModule));
  61. bool connect;
  62. connect =switchModule.connectInput(0,processor.getIteration());
  63. switchModule.setInputDefaultValue(1)["value"] = 8.0;
  64. connect = switchModule.connectInput(2,calcModule.getOutput(0));
  65. calcModule.connectInput(0,switchModule.getOutput(0));
  66. calcModule.setInputDefaultValue(1)["value"] = 2.0;
  67. processor.addOutput(1,0);
  68. processor.update();
  69. EXPECT_FLOAT_EQ(processor.getOutput(0)->getValue()["value"].get<float>(), 0.0);
  70. }