Skip to content
Snippets Groups Projects
Commit 06ab6d31 authored by Maxence Naud's avatar Maxence Naud
Browse files

Merge branch 'fix_207-producer-clone' into 'dev'

add some tests for Tensor addition operator and subtraction operator

See merge request !116
parents 8b99d4f1 2f0e6ffe
No related branches found
No related tags found
2 merge requests!118v0.4.0,!116add some tests for Tensor addition operator and subtraction operator
Pipeline #61039 passed
......@@ -193,4 +193,100 @@ TEST_CASE("Test division of Tensors","[TensorImpl][Div]") {
Tensor T3(T1.dims());
REQUIRE_THROWS(T0 / T3);
}
TEST_CASE("Tensor arithmetic operators", "[Tensor][Operator][CPU]") {
SECTION("Addition") {
const Tensor t = Array1D<std::int32_t, 5>{1,2,3,4,5};
const Tensor t2 = Array1D<std::int32_t, 5>{10,20,30,40,50};
const Tensor t3 = Tensor(std::int32_t(3));
SECTION("operator+") {
auto a = t.clone();
auto b = t2.clone();
auto c = t3.clone();
// simple addition
auto r1 = a + b;
const Tensor expected_res_simple = Array1D<std::int32_t, 5>{11,22,33,44,55};
// input tensors are not modified
REQUIRE(a == t);
REQUIRE(b == t2);
// result is right
REQUIRE(r1 == expected_res_simple);
// simple addition of arithmetic value
auto r2 = a + 10;
const Tensor expected_res_simple_arithmetic = Array1D<std::int32_t, 5>{11,12,13,14,15};
// input tensors are not modified
REQUIRE(a == t);
// result is right
REQUIRE(r2 == expected_res_simple_arithmetic);
// chained addition a+b+c
auto r3 = a + b + c;
const Tensor expected_res_chained = Array1D<std::int32_t, 5>{14,25,36,47,58};
// input Tensors are not modified
REQUIRE(a == t);
REQUIRE(b == t2);
REQUIRE(c == t3);
// result is right
REQUIRE(r3 == expected_res_chained);
}
SECTION("operator+=") {
auto a = t.clone();
auto b = t2.clone();
a += b;
const Tensor expected_res = Array1D<std::int32_t, 5>{11,22,33,44,55};
// input tensors are not modified
REQUIRE(b == t2);
// result is right
REQUIRE(a == expected_res);
// simple addition of arithmetic value
a = t.clone();
a += 10;
const Tensor expected_res_arithmetic = Array1D<std::int32_t, 5>{11,12,13,14,15};
// result is right
REQUIRE(a == expected_res_arithmetic);
}
}
SECTION("Substraction") {
const Tensor t = Array1D<std::int32_t, 5>{1,2,3,4,5};
const Tensor t2 = Tensor(std::int32_t(3));
SECTION("operator-") {
auto a = t.clone();
auto b = t2.clone();
// simple substraction
auto r1 = a - b;
const Tensor expected_res_simple = Array1D<std::int32_t, 5>{-2,-1,0,1,2};
// input tensors are not modified
REQUIRE(a == t);
REQUIRE(b == t2);
// result is right
REQUIRE(r1 == expected_res_simple);
}
SECTION("operator-=") {
auto a = t.clone();
auto b = t2.clone();
a -= b;
const Tensor expected_res = Array1D<std::int32_t, 5>{-2,-1,0,1,2};
// input tensors are not modified
REQUIRE(b == t2);
// result is right
REQUIRE(a == expected_res);
}
}
}
} // namespace Aidge
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment