1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- #include <gtest/gtest.h>
- #include <json.hpp>
- //#define private public
- #include <ProcessorManual.h>
- #include <ModuleMath.h>
- #include <ModuleSwitch.h>
- using namespace mdd;
- TEST(ProcessorManual, CalculateSimpleFormula){
- //f4:(f3:(f1:(5*3)-f2:(4+5))/2)==(15-9)/2==6/2==3
- std::vector<std::string> inputs;
- std::shared_ptr<ModuleMath> f1 = std::make_shared<ModuleMath>(MULTIPLY);
- inputs = f1->getInputIDs();
- f1->getInput(inputs[0])->setDefaultValue()["value"] = 5;
- f1->getInput(inputs[1])->setDefaultValue()["value"] = 3;
- std::shared_ptr<ModuleMath> f2 = std::make_shared<ModuleMath>(ADD);
- inputs = f2->getInputIDs();
- f2->getInput(inputs[0])->setDefaultValue()["value"] = 4;
- f2->getInput(inputs[1])->setDefaultValue()["value"] = 5;
- std::shared_ptr<ModuleMath> f3 = std::make_shared<ModuleMath>(SUBTRACT);
- inputs = f3->getInputIDs();
- f3->getInput(inputs[0])->connect(f1->getOutput(f1->getOutputIDs()[0]));
- f3->getInput(inputs[1])->connect(f1->getOutput(f2->getOutputIDs()[0]));
- std::shared_ptr<ModuleMath> f4 = std::make_shared<ModuleMath>(DIVIDE);
- inputs = f4->getInputIDs();
- f4->getInput(inputs[0])->connect(f1->getOutput(f3->getOutputIDs()[0]));
- f4->getInput(inputs[1])->setDefaultValue()["value"] = 2;
- std::shared_ptr<ProcessorManual> test = std::make_shared<ProcessorManual>();
- test->addModule(f1);
- test->addModule(f2);
- test->addModule(f3);
- test->addModule(f4);
- test->addModuleOutput(f3, f3->getOutput(f3->getOutputIDs()[0]));
- test->update();
- EXPECT_EQ(test->getOutput(test->getOutputIDs()[1])->getValue()["value"].get<int>(), 3);
- }
- TEST(ProcessorManual, CalculateAdvancedFormula){
- //f4:(f3:(f1:(5*3)-f2:(4+5))/2)==(15-9)/2==6/2==3
- std::vector<std::string> inputs;
- std::shared_ptr<ModuleMath> f1 = std::make_shared<ModuleMath>(MULTIPLY);
- inputs = f1->getInputIDs();
- f1->getInput(inputs[0])->setDefaultValue()["value"] = 5;
- f1->getInput(inputs[1])->setDefaultValue()["value"] = 3;
- std::shared_ptr<ModuleMath> f2 = std::make_shared<ModuleMath>(ADD);
- inputs = f2->getInputIDs();
- f2->getInput(inputs[0])->setDefaultValue()["value"] = 4;
- f2->getInput(inputs[1])->setDefaultValue()["value"] = 5;
- std::shared_ptr<ModuleMath> f3 = std::make_shared<ModuleMath>(SUBTRACT);
- inputs = f3->getInputIDs();
- f3->getInput(inputs[0])->connect(f1->getOutput(f1->getOutputIDs()[0]));
- f3->getInput(inputs[1])->connect(f1->getOutput(f2->getOutputIDs()[0]));
- std::shared_ptr<ModuleMath> f4 = std::make_shared<ModuleMath>(DIVIDE);
- inputs = f4->getInputIDs();
- f4->getInput(inputs[0])->connect(f1->getOutput(f3->getOutputIDs()[0]));
- f4->getInput(inputs[1])->setDefaultValue()["value"] = 2;
- std::shared_ptr<ProcessorManual> test = std::make_shared<ProcessorManual>();
- test->addModule(f4);
- test->addModule(f3);
- test->addModule(f2);
- test->addModule(f1);
- test->addModuleOutput(f3, f3->getOutput(f3->getOutputIDs()[0]));
- test->update();
- EXPECT_EQ(test->getOutput(test->getOutputIDs()[1])->getValue()["value"].get<int>(), 3);
- }
- TEST(ProcessorManual, CalculateExtremeFormula){
- //x_0=8, x_i=x_{i-1}/2
- std::shared_ptr<ModuleSwitch> switchModule = std::make_shared<ModuleSwitch>();
- std::shared_ptr<ModuleMath> calcModule = std::make_shared<ModuleMath>(DIVIDE);
- std::shared_ptr<ProcessorManual> processor = std::make_shared<ProcessorManual>();
- processor->addModule(switchModule);
- processor->addModule(calcModule);
- bool connect;
- connect =switchModule->getInput(switchModule->getInputIDs()[0])->connect(processor->getIteration());
- switchModule->getInput(switchModule->getInputIDs()[1])->setDefaultValue()["value"] = 8.0;
- connect = switchModule->getInput(switchModule->getInputIDs()[2])->connect(calcModule->getOutput(calcModule->getOutputIDs()[0]));
- calcModule->getInput(calcModule->getInputIDs()[0])->connect(switchModule->getOutput(switchModule->getInputIDs()[0]));
- calcModule->getInput(calcModule->getInputIDs()[1])->setDefaultValue()["value"] = 2.0;
- processor->addModuleOutput(calcModule,calcModule->getOutput(calcModule->getOutputIDs()[0]));
- processor->update();
- EXPECT_FLOAT_EQ(processor->getOutput(processor->getOutputIDs()[1])->getValue()["value"].get<double>(), 0.0);
- }
|