From fa4c75f8cc87f55c2c1ca0e46ebeeb526cbf3bf0 Mon Sep 17 00:00:00 2001
From: Jerome Hue <jerome.hue@cea.fr>
Date: Mon, 16 Sep 2024 16:03:26 +0200
Subject: [PATCH] [UT] Harmonize tests by using approxEq<> everywhere

Previously, tests used both `approxEq<>` and a for-loop combined with
`std::abs()` to test the equality of two tensors.
`approxEq`` results in fewer and clearer lines. It is also more
configurable.
---
 unit_tests/operator/Test_AvgPoolingImpl.cpp |  8 +++-----
 unit_tests/operator/Test_BatchNormImpl.cpp  |  8 ++------
 unit_tests/operator/Test_ConvImpl.cpp       |  7 ++-----
 unit_tests/operator/Test_ErfImpl.cpp        | 15 ++++-----------
 unit_tests/operator/Test_MetaOperator.cpp   |  6 +-----
 unit_tests/operator/Test_SoftmaxImpl.cpp    |  8 +++-----
 unit_tests/operator/Test_SqrtImpl.cpp       | 16 ++++------------
 7 files changed, 19 insertions(+), 49 deletions(-)

diff --git a/unit_tests/operator/Test_AvgPoolingImpl.cpp b/unit_tests/operator/Test_AvgPoolingImpl.cpp
index aaa27578..702c0350 100644
--- a/unit_tests/operator/Test_AvgPoolingImpl.cpp
+++ b/unit_tests/operator/Test_AvgPoolingImpl.cpp
@@ -9,6 +9,7 @@
  *
  ********************************************************************************/
 
+#include <aidge/utils/TensorUtils.hpp>
 #include <catch2/catch_test_macros.hpp>
 #include <memory>
 #include <cstdlib>
@@ -100,11 +101,8 @@ TEST_CASE("[cpu/operator] AvgPooling(forward)", "[AvgPooling][CPU]") {
         op->setBackend("cpu");
         myAvgPool->forward();
         op->getOutput(0)->print();
-        float* outPtr = static_cast<float*>(op->getOutput(0)->getImpl()->rawPtr());
-        float* expectedOutPtr = static_cast<float*>(myOutput.getImpl()->rawPtr());
-        for (std::size_t i = 0; i < 1; ++i) {
-            REQUIRE(std::abs(outPtr[i] - expectedOutPtr[i]) < 0.00001);
-        }
+
+        REQUIRE(approxEq<float>(*(op->getOutput(0)), myOutput, 0, 1E-5));
     }
     // std::cout << static_cast<Tensor>((*op)["weight"])[0][0][0][0] << std::endl;
 }
\ No newline at end of file
diff --git a/unit_tests/operator/Test_BatchNormImpl.cpp b/unit_tests/operator/Test_BatchNormImpl.cpp
index 1b42c90d..cd691f70 100644
--- a/unit_tests/operator/Test_BatchNormImpl.cpp
+++ b/unit_tests/operator/Test_BatchNormImpl.cpp
@@ -9,6 +9,7 @@
  *
  ********************************************************************************/
 
+#include <aidge/utils/TensorUtils.hpp>
 #include <catch2/catch_test_macros.hpp>
 #include <memory>
 
@@ -88,11 +89,6 @@ TEST_CASE("[cpu/operator] BatchNorm(forward)", "[BatchNorm][CPU]") {
     op->setBackend("cpu");
     myBatchNorm->forward();
 
-    float* resPtr = static_cast<float*>(op->getOutput(0)->getImpl()->rawPtr());
-    float* expectedPtr = static_cast<float*>(myOutput->getImpl()->rawPtr());
-    for (std::size_t i = 0; i< 54; ++i) {
-        REQUIRE(std::abs(resPtr[i]-expectedPtr[i]) < 0.00001);
-    }
-
+    REQUIRE(approxEq<float>(*op->getOutput(0), *myOutput, 0, 1E-5));
     // std::cout << static_cast<Tensor>((*op)["weight"])[0][0][0][0] << std::endl;
 }
\ No newline at end of file
diff --git a/unit_tests/operator/Test_ConvImpl.cpp b/unit_tests/operator/Test_ConvImpl.cpp
index b5208513..fd662ee4 100644
--- a/unit_tests/operator/Test_ConvImpl.cpp
+++ b/unit_tests/operator/Test_ConvImpl.cpp
@@ -9,6 +9,7 @@
  *
  ********************************************************************************/
 
+#include <aidge/utils/TensorUtils.hpp>
 #include <catch2/catch_test_macros.hpp>
 #include <cstdlib>
 #include <memory>
@@ -245,10 +246,6 @@ TEST_CASE("[cpu/operator] Conv(forward)", "[Conv][CPU]") {
         op->setBackend("cpu");
         myConv->forward();
 
-        float* resPtr = static_cast<float*>(op->getOutput(0)->getImpl()->rawPtr());
-        float* expectedPtr = static_cast<float*>(expectedOutput.getImpl()->rawPtr());
-        for (std::size_t i = 0; i< expectedOutput.size(); ++i) {
-            REQUIRE(std::abs(resPtr[i]-expectedPtr[i]) < 0.00001);
-        }
+        REQUIRE(approxEq<float>(*op->getOutput(0),expectedOutput, 0, 1E-5));
     }
 }
\ No newline at end of file
diff --git a/unit_tests/operator/Test_ErfImpl.cpp b/unit_tests/operator/Test_ErfImpl.cpp
index 2826b5b5..09980223 100644
--- a/unit_tests/operator/Test_ErfImpl.cpp
+++ b/unit_tests/operator/Test_ErfImpl.cpp
@@ -9,6 +9,7 @@
  *
  ********************************************************************************/
 
+#include <aidge/utils/TensorUtils.hpp>
 #include <catch2/catch_test_macros.hpp>
 
 #include "aidge/data/Tensor.hpp"
@@ -21,7 +22,7 @@
 
 using namespace Aidge;
 
-TEST_CASE("[cpu/operator] Erf(forward)") {
+TEST_CASE("[cpu/operator] Erf(forward)", "[CPU][Erf]") {
     SECTION("1D Tensor") {
         std::shared_ptr<Tensor> input0 = std::make_shared<Tensor>(Array1D<float,10> {
             {0.41384590, 0.43120754, 0.93762982, 0.31049860, 0.77547199, 0.09514862,
@@ -39,11 +40,7 @@ TEST_CASE("[cpu/operator] Erf(forward)") {
         op->setBackend("cpu");
         myErf->forward();
 
-        float* resPtr = static_cast<float*>(op->getOutput(0)->getImpl()->rawPtr());
-        float* expectedPtr = static_cast<float*>(expectedOutput->getImpl()->rawPtr());
-        for (std::size_t i = 0; i< expectedOutput->size(); ++i) {
-            REQUIRE(std::abs(resPtr[i]-expectedPtr[i]) < 0.00001);
-        }
+        REQUIRE(approxEq<float>(*(op->getOutput(0)), *(expectedOutput)));
     }
 
     SECTION("3D Tensor") {
@@ -79,10 +76,6 @@ TEST_CASE("[cpu/operator] Erf(forward)") {
         op->setBackend("cpu");
         myErf->forward();
 
-        float* resPtr = static_cast<float*>(op->getOutput(0)->getImpl()->rawPtr());
-        float* expectedPtr = static_cast<float*>(expectedOutput->getImpl()->rawPtr());
-        for (std::size_t i = 0; i< expectedOutput->size(); ++i) {
-            REQUIRE(std::abs(resPtr[i]-expectedPtr[i]) < 0.00001);
-        }
+        REQUIRE(approxEq<float>(*(op->getOutput(0)), *(expectedOutput)));
     }
 }
\ No newline at end of file
diff --git a/unit_tests/operator/Test_MetaOperator.cpp b/unit_tests/operator/Test_MetaOperator.cpp
index 271a1e2f..f0611df4 100644
--- a/unit_tests/operator/Test_MetaOperator.cpp
+++ b/unit_tests/operator/Test_MetaOperator.cpp
@@ -182,11 +182,7 @@ TEST_CASE("[cpu/operator] MetaOperator", "[MetaOperator][CPU]") {
     myConv->forward();
     convOp -> getOutput(0) -> print();
 
-    double* computedOutput = static_cast<double*>(convOp->getOutput(0)->getImpl()->rawPtr());
-    double* expectedOutput = static_cast<double*>(myOutput->getImpl()->rawPtr());
-    for (std::size_t i = 0; i < myOutput->size(); ++i) {
-        REQUIRE(std::abs(computedOutput[i] - expectedOutput[i]) < 1e-5);
-    }
+    REQUIRE(approxEq<double>(*(convOp->getOutput(0)), *myOutput, 0, 1E-5));
 
     std::shared_ptr<Node> myPaddedConv =
             PaddedConv(3, 4, {3, 3}, "myPaddedConv", {1, 1}, {1, 1, 1, 1});
diff --git a/unit_tests/operator/Test_SoftmaxImpl.cpp b/unit_tests/operator/Test_SoftmaxImpl.cpp
index da6c6f0d..14081c03 100644
--- a/unit_tests/operator/Test_SoftmaxImpl.cpp
+++ b/unit_tests/operator/Test_SoftmaxImpl.cpp
@@ -9,6 +9,7 @@
  *
  ********************************************************************************/
 
+#include <aidge/utils/TensorUtils.hpp>
 #include <catch2/catch_test_macros.hpp>
 
 #include "aidge/data/Tensor.hpp"
@@ -52,6 +53,7 @@ TEST_CASE("[cpu/operator] Softmax(forward)", "[Softmax][CPU]") {
             REQUIRE(std::abs(resPtr[i]-expectedPtr[i]) < 0.00001);
         }
 
+        REQUIRE(approxEq<float>(*op->getOutput(0), *expectedOutput, 0, 1E-5));
     }
     SECTION("4D Tensor") {
         std::shared_ptr<Tensor> input = std::make_shared<Tensor>(Array4D<float,2,3,3,3> {
@@ -114,10 +116,6 @@ TEST_CASE("[cpu/operator] Softmax(forward)", "[Softmax][CPU]") {
         op->setBackend("cpu");
         mySoftmax->forward();
 
-        float* resPtr = static_cast<float*>(op->getOutput(0)->getImpl()->rawPtr());
-        float* expectedPtr = static_cast<float*>(expectedOutput->getImpl()->rawPtr());
-        for (std::size_t i = 0; i< expectedOutput->size(); ++i) {
-            REQUIRE(std::abs(resPtr[i]-expectedPtr[i]) < 0.00001);
-        }
+        REQUIRE(approxEq<float>(*op->getOutput(0), *expectedOutput, 0, 1E-5));
     }
 }
\ No newline at end of file
diff --git a/unit_tests/operator/Test_SqrtImpl.cpp b/unit_tests/operator/Test_SqrtImpl.cpp
index d630c66c..6c65b5e8 100644
--- a/unit_tests/operator/Test_SqrtImpl.cpp
+++ b/unit_tests/operator/Test_SqrtImpl.cpp
@@ -9,6 +9,7 @@
  *
  ********************************************************************************/
 
+#include <aidge/utils/TensorUtils.hpp>
 #include <catch2/catch_test_macros.hpp>
 
 #include "aidge/data/Tensor.hpp"
@@ -42,12 +43,7 @@ TEST_CASE("[cpu/operator] Sqrt(forward)", "[Sqrt][CPU]") {
         mySqrt->getOperator()->setBackend("cpu");
         mySqrt->forward();
 
-        float* resPtr = static_cast<float*>(op->getOutput(0)->getImpl()->rawPtr());
-        float* expectedPtr = static_cast<float*>(expectedOutput->getImpl()->rawPtr());
-        for (std::size_t i = 0; i< 4; ++i) {
-            REQUIRE(std::abs(resPtr[i]-expectedPtr[i]) < 0.00001);
-        }
-
+        REQUIRE(approxEq<float>(*op->getOutput(0), *expectedOutput, 0, 1E-5));
     }
 
     SECTION("4D Tensor") {
@@ -112,10 +108,6 @@ TEST_CASE("[cpu/operator] Sqrt(forward)", "[Sqrt][CPU]") {
         mySqrt->getOperator()->setBackend("cpu");
         mySqrt->forward();
 
-        float* resPtr = static_cast<float*>(op->getOutput(0)->getImpl()->rawPtr());
-        float* expectedPtr = static_cast<float*>(expectedOutput->getImpl()->rawPtr());
-        for (std::size_t i = 0; i< 54; ++i) {
-            REQUIRE(std::abs(resPtr[i]-expectedPtr[i]) < 0.00001);
-        }
+        REQUIRE(approxEq<float>(*op->getOutput(0), *expectedOutput, 0, 1E-5));
     }
-}
\ No newline at end of file
+}
\ No newline at end of file
-- 
GitLab