diff --git a/unit_tests/operator/Test_ReshapeImpl.cpp b/unit_tests/operator/Test_ReshapeImpl.cpp index 2f374732d5757d1fdd036c26d039bea71499659f..5b6f5f3fbff86cb2dacb5b136b6834b6e59835b5 100644 --- a/unit_tests/operator/Test_ReshapeImpl.cpp +++ b/unit_tests/operator/Test_ReshapeImpl.cpp @@ -20,56 +20,109 @@ using namespace Aidge; TEST_CASE("[cpu/operator] Reshape(forward)") { SECTION("1D Tensor") { - std::shared_ptr<Tensor> input = std::make_shared<Tensor>(Array1D<float,6> { - {1.0, 2.0, 3.0, 4.0, 5.0, 6.0} - }); - std::shared_ptr<Tensor> shape = std::make_shared<Tensor>(Array1D<float,2> { - {2, 3} - }); - std::shared_ptr<Tensor> expectedOutput = std::make_shared<Tensor>(Array2D<float,2,3> { - { - {1.0, 2.0, 3.0}, - {4.0, 5.0, 6.0} - } - }); + SECTION("Shape As Input") { + std::shared_ptr<Tensor> input = std::make_shared<Tensor>(Array1D<float,6> { + {1.0, 2.0, 3.0, 4.0, 5.0, 6.0} + }); + std::shared_ptr<Tensor> shape = std::make_shared<Tensor>(Array1D<float,2> { + {2, 3} + }); + std::shared_ptr<Tensor> expectedOutput = std::make_shared<Tensor>(Array2D<float,2,3> { + { + {1.0, 2.0, 3.0}, + {4.0, 5.0, 6.0} + } + }); - std::shared_ptr<Node> myReshape = Reshape(); - auto op = std::static_pointer_cast<OperatorTensor>(myReshape -> getOperator()); - op->associateInput(0, input); - op->associateInput(1, shape); - op->setDataType(DataType::Float32); - op->setBackend("cpu"); - myReshape->forward(); + std::shared_ptr<Node> myReshape = Reshape(); + auto op = std::static_pointer_cast<OperatorTensor>(myReshape -> getOperator()); + op->associateInput(0, input); + op->associateInput(1, shape); + op->setDataType(DataType::Float32); + op->setBackend("cpu"); + myReshape->forward(); - REQUIRE(*(op->getOutput(0)) == *expectedOutput); + REQUIRE(*(op->getOutput(0)) == *expectedOutput); + } + SECTION("Shape As Input") { + std::shared_ptr<Tensor> input = std::make_shared<Tensor>(Array1D<float,6> { + {1.0, 2.0, 3.0, 4.0, 5.0, 6.0} + }); + std::shared_ptr<Tensor> expectedOutput = std::make_shared<Tensor>(Array2D<float,2,3> { + { + {1.0, 2.0, 3.0}, + {4.0, 5.0, 6.0} + } + }); + + std::shared_ptr<Node> myReshape = Reshape({2, 3}); + auto op = std::static_pointer_cast<OperatorTensor>(myReshape -> getOperator()); + op->associateInput(0, input); + // TODO find a way to avoid connecting an empty tensor + op->associateInput(1, std::make_shared<Tensor>(Tensor({}))); + op->setDataType(DataType::Float32); + op->setBackend("cpu"); + myReshape->forward(); + + REQUIRE(*(op->getOutput(0)) == *expectedOutput); + } } - SECTION("2D Tensor") { - std::shared_ptr<Tensor> input = std::make_shared<Tensor>(Array2D<float,2,3> { - { - {1.0, 2.0, 3.0}, - {4.0, 5.0, 6.0} - } + SECTION("2D Tensor - Shape Input") { + SECTION("Shape As Input") { + std::shared_ptr<Tensor> input = std::make_shared<Tensor>(Array2D<float,2,3> { + { + {1.0, 2.0, 3.0}, + {4.0, 5.0, 6.0} + } + + }); + std::shared_ptr<Tensor> shape = std::make_shared<Tensor>(Array1D<float,2> { + {3, 2} + }); + std::shared_ptr<Tensor> expectedOutput = std::make_shared<Tensor>(Array2D<float,3,2> { + { + {1.0, 2.0}, + {3.0, 4.0}, + {5.0, 6.0} + } + }); + + std::shared_ptr<Node> myReshape = Reshape(); + auto op = std::static_pointer_cast<OperatorTensor>(myReshape -> getOperator()); + op->associateInput(0, input); + op->associateInput(1, shape); + op->setDataType(DataType::Float32); + op->setBackend("cpu"); + myReshape->forward(); + + REQUIRE(*(op->getOutput(0)) == *expectedOutput); + } + SECTION("Shape As Attribute") { + std::shared_ptr<Tensor> input = std::make_shared<Tensor>(Array2D<float,2,3> { + { + {1.0, 2.0, 3.0}, + {4.0, 5.0, 6.0} + } - }); - std::shared_ptr<Tensor> shape = std::make_shared<Tensor>(Array1D<float,2> { - {3, 2} - }); - std::shared_ptr<Tensor> expectedOutput = std::make_shared<Tensor>(Array2D<float,3,2> { - { - {1.0, 2.0}, - {3.0, 4.0}, - {5.0, 6.0} - } - }); + }); + std::shared_ptr<Tensor> expectedOutput = std::make_shared<Tensor>(Array2D<float,3,2> { + { + {1.0, 2.0}, + {3.0, 4.0}, + {5.0, 6.0} + } + }); - std::shared_ptr<Node> myReshape = Reshape(); - auto op = std::static_pointer_cast<OperatorTensor>(myReshape -> getOperator()); - op->associateInput(0, input); - op->associateInput(1, shape); - op->setDataType(DataType::Float32); - op->setBackend("cpu"); - myReshape->forward(); + std::shared_ptr<Node> myReshape = Reshape({3, 2}); + auto op = std::static_pointer_cast<OperatorTensor>(myReshape -> getOperator()); + op->associateInput(0, input); + // TODO find a way to avoid connecting an empty tensor + op->associateInput(1, std::make_shared<Tensor>(Tensor({}))); + op->setDataType(DataType::Float32); + op->setBackend("cpu"); + myReshape->forward(); - REQUIRE(*(op->getOutput(0)) == *expectedOutput); + REQUIRE(*(op->getOutput(0)) == *expectedOutput); + } } } \ No newline at end of file