From 75f98290bd8e7bb8c6d9f594d1232418fd48fdce Mon Sep 17 00:00:00 2001
From: thibault allenet <thibault.allenet@cea.fr>
Date: Mon, 19 Feb 2024 15:20:16 +0000
Subject: [PATCH] Generate random matrix instead of loading a path for CI tests

---
 unit_tests/Test_Stimulus.cpp                  | 28 ++++++++++++--
 .../Test_StimulusImpl_opencv_imread.cpp       | 38 +++++++++++++++----
 unit_tests/Tests_Utils.cpp                    | 10 +----
 unit_tests/Tools.hpp                          | 26 +++++++++++++
 4 files changed, 81 insertions(+), 21 deletions(-)
 create mode 100644 unit_tests/Tools.hpp

diff --git a/unit_tests/Test_Stimulus.cpp b/unit_tests/Test_Stimulus.cpp
index 7ad5613..bdfe453 100644
--- a/unit_tests/Test_Stimulus.cpp
+++ b/unit_tests/Test_Stimulus.cpp
@@ -19,17 +19,37 @@
 #include "aidge/backend/opencv/data/TensorImpl.hpp"
 #include "aidge/data/Tensor.hpp"
 
+#include "Tools.hpp"
 
 using namespace Aidge;
 
 TEST_CASE("Stimulus creation", "[Stimulus][OpenCV]") {
     SECTION("Instanciation & load an image") {
-        //  Load image with imread
-        cv::Mat true_mat = cv::imread("/data1/is156025/tb256203/dev/eclipse_aidge/aidge/user_tests/train-images-idx3-ubyte[00001].pgm", cv::IMREAD_UNCHANGED);
-        REQUIRE(true_mat.empty()==false);
+        // //  Load image with imread
+        // cv::Mat true_mat = cv::imread("/data1/is156025/tb256203/dev/eclipse_aidge/aidge/user_tests/train-images-idx3-ubyte[00001].pgm", cv::IMREAD_UNCHANGED);
+        // REQUIRE(true_mat.empty()==false);
+
+        // // Create Stimulus
+        // Stimulus stimg("/data1/is156025/tb256203/dev/eclipse_aidge/aidge/user_tests/train-images-idx3-ubyte[00001].pgm", true);
+        // stimg.setBackend("opencv");
+        
+        //  Generate random matrix and save it
+        std::vector<cv::Mat> channels;
+        cv::Mat true_mat;
+
+        for (int c = 0; c < 3; ++c){
+            // Create a random matrix
+            cv::Mat randomMat = createRandomMat<unsigned char>(224, 224);
+            // Add each random matrix to the vector
+            channels.push_back(randomMat);
+        }
+        // Merge the vector of cv mat into one cv mat
+        cv::merge(channels, true_mat);
+
+        cv::imwrite("output_image.png", true_mat);
 
         // Create Stimulus
-        Stimulus stimg("/data1/is156025/tb256203/dev/eclipse_aidge/aidge/user_tests/train-images-idx3-ubyte[00001].pgm", true);
+        Stimulus stimg("output_image.png", true);
         stimg.setBackend("opencv");
 
         // Load the image in a tensor & save it in memory
diff --git a/unit_tests/Test_StimulusImpl_opencv_imread.cpp b/unit_tests/Test_StimulusImpl_opencv_imread.cpp
index d84e65e..b3b2b19 100644
--- a/unit_tests/Test_StimulusImpl_opencv_imread.cpp
+++ b/unit_tests/Test_StimulusImpl_opencv_imread.cpp
@@ -19,19 +19,41 @@
 #include "aidge/backend/opencv/data/TensorImpl.hpp"
 #include "aidge/data/Tensor.hpp"
 
+#include "Tools.hpp"
 
 using namespace Aidge;
 
 TEST_CASE("StimulusImpl_opencv_imread creation", "[StimulusImpl_opencv_imread][OpenCV]") {
     SECTION("Instanciation & load an image") {
-        //  Load image with imread
-        // cv::Mat true_mat = cv::imread("/data1/is156025/tb256203/dev/eclipse_aidge/aidge/user_tests/Lenna.png");
-        cv::Mat true_mat = cv::imread("/data1/is156025/tb256203/dev/eclipse_aidge/aidge/user_tests/train-images-idx3-ubyte[00001].pgm", cv::IMREAD_UNCHANGED);
-        REQUIRE(true_mat.empty()==false);
-
-        // Create StimulusImpl_opencv_imread
-        // StimulusImpl_opencv_imread stImpl("/data1/is156025/tb256203/dev/eclipse_aidge/aidge/user_tests/Lenna.png");
-        StimulusImpl_opencv_imread stImpl("/data1/is156025/tb256203/dev/eclipse_aidge/aidge/user_tests/train-images-idx3-ubyte[00001].pgm");
+        // //  Load image with imread
+        // // cv::Mat true_mat = cv::imread("/data1/is156025/tb256203/dev/eclipse_aidge/aidge/user_tests/Lenna.png");
+        // cv::Mat true_mat = cv::imread("/data1/is156025/tb256203/dev/eclipse_aidge/aidge/user_tests/train-images-idx3-ubyte[00001].pgm", cv::IMREAD_UNCHANGED);
+        // REQUIRE(true_mat.empty()==false);
+
+        // // Create StimulusImpl_opencv_imread
+        // // StimulusImpl_opencv_imread stImpl("/data1/is156025/tb256203/dev/eclipse_aidge/aidge/user_tests/Lenna.png");
+        // StimulusImpl_opencv_imread stImpl("/data1/is156025/tb256203/dev/eclipse_aidge/aidge/user_tests/train-images-idx3-ubyte[00001].pgm");
+        
+         //  Generate random matrix and save it
+        std::vector<cv::Mat> channels;
+        cv::Mat true_mat;
+
+        for (int c = 0; c < 3; ++c){
+            // Create a random matrix
+            cv::Mat randomMat = createRandomMat<unsigned char>(224, 224);
+            // Add each random matrix to the vector
+            channels.push_back(randomMat);
+        }
+        // Merge the vector of cv mat into one cv mat
+        cv::merge(channels, true_mat);
+
+        // Save image into a png file
+        cv::imwrite("output_image_stimpl.png", true_mat);
+
+        // Instanciate timulusImpl_opencv_imread
+        StimulusImpl_opencv_imread stImpl("output_image_stimpl.png");
+
+        // Load the image as a tensor
         std::shared_ptr<Tensor> tensor_load;
         tensor_load = stImpl.load();
 
diff --git a/unit_tests/Tests_Utils.cpp b/unit_tests/Tests_Utils.cpp
index bd01df9..91cd54f 100644
--- a/unit_tests/Tests_Utils.cpp
+++ b/unit_tests/Tests_Utils.cpp
@@ -21,18 +21,10 @@
 #include "aidge/backend/opencv/data/TensorImpl.hpp"
 #include "aidge/backend/cpu/data/TensorImpl.hpp"
 
+#include "Tools.hpp"
 
 using namespace Aidge;
 
-template <typename T>
-cv::Mat createRandomMat(int rows, int cols) {
-    cv::Mat randomMat(rows, cols, cv::DataType<T>::type);
-
-    cv::randu(randomMat, cv::Scalar::all(0), cv::Scalar::all(255));
-
-    return randomMat;
-}
-
 // TEMPLATE_TEST_CASE("Opencv Utils", "[Utils][OpenCV]", char, unsigned char, short, unsigned short, int, float, double) {
 // TODO : perform test for char and double
 TEMPLATE_TEST_CASE("Opencv Utils", "[Utils][OpenCV]", signed char, unsigned char, short, unsigned short, int, float, double) {
diff --git a/unit_tests/Tools.hpp b/unit_tests/Tools.hpp
new file mode 100644
index 0000000..0ade0c5
--- /dev/null
+++ b/unit_tests/Tools.hpp
@@ -0,0 +1,26 @@
+/********************************************************************************
+ * Copyright (c) 2023 CEA-List
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0.
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ ********************************************************************************/
+
+#ifndef AIDGE_OPENCV_TOOLS_H_
+#define AIDGE_OPENCV_TOOLS_H_
+
+#include "opencv2/core.hpp"
+
+template <typename T>
+cv::Mat createRandomMat(int rows, int cols) {
+    cv::Mat randomMat(rows, cols, cv::DataType<T>::type);
+
+    cv::randu(randomMat, cv::Scalar::all(0), cv::Scalar::all(255));
+
+    return randomMat;
+}
+
+#endif // AIDGE_OPENCV_TESTS_UTILS_H_
\ No newline at end of file
-- 
GitLab