Skip to content

Commit 41b915d

Browse files
committed
Make minimal setup for tests
1 parent b3c5834 commit 41b915d

File tree

5 files changed

+70
-26
lines changed

5 files changed

+70
-26
lines changed

packages/react-native-executorch/common/rnexecutorch/data_processing/ImageProcessing.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,18 @@ static cv::Mat convertBufferToColorMat(std::span<const float> buffer,
7676
}
7777

7878
std::string saveToTempFile(const cv::Mat &image) {
79-
std::string filename = "rn_executorch_" + file_utils::getTimeID() + ".png";
79+
const auto filename = std::string("rn_executorch_")
80+
.append(file_utils::getTimeID())
81+
.append(".png");
8082

81-
fs::path tempDir = fs::temp_directory_path();
82-
fs::path filePath = tempDir / filename;
83+
const fs::path tempDir = fs::temp_directory_path();
84+
const fs::path filePath = tempDir / filename;
8385

8486
if (!cv::imwrite(filePath.string(), image)) {
8587
throw std::runtime_error("Failed to save the image: " + filePath.string());
8688
}
8789

88-
return "file://" + filePath.string();
90+
return std::string("file://").append(filePath.string());
8991
}
9092

9193
static cv::Mat handleBase64Data(const std::string &imageURI) {

packages/react-native-executorch/common/rnexecutorch/tests/CMakeLists.txt

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,37 @@ project(RNExecutorchTests)
55
set(CMAKE_CXX_STANDARD 20)
66
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
77

8-
# googletest subdirectory
9-
# Using an absolute path from the top-level source directory
10-
add_subdirectory(${CMAKE_SOURCE_DIR}/../../../../../third-party/googletest ${PROJECT_BINARY_DIR}/googletest)
8+
# Include directories for your project
9+
include_directories("${PROJECT_SOURCE_DIR}/../")
10+
include_directories("${PROJECT_SOURCE_DIR}/../data_processing")
11+
12+
# Enable testing with GoogleTest
13+
enable_testing()
14+
15+
# Include Google Test
16+
add_subdirectory("${PROJECT_SOURCE_DIR}/../../../../../third-party/googletest" "${CMAKE_BINARY_DIR}/googletest")
1117

12-
# Directories to include
13-
include_directories(${CMAKE_SOURCE_DIR}/../data_processing)
14-
include_directories(${CMAKE_SOURCE_DIR}/..)
1518

16-
# Source files
17-
set(SOURCE_FILES ${CMAKE_SOURCE_DIR}/../data_processing/Numerical.cpp)
19+
# Include Google Test's headers
20+
include_directories("${gtest_SOURCE_DIR}/include" "${gmock_SOURCE_DIR}/include")
1821

19-
# Executables for the tests
20-
add_executable(NumericalTests NumericalTest.cpp ${SOURCE_FILES})
21-
add_executable(LogTests LogTest.cpp)
22+
# Gather all test source files
23+
set(TEST_SOURCES
24+
# "${PROJECT_SOURCE_DIR}/ImageProcessingTest.cpp"
25+
"${PROJECT_SOURCE_DIR}/LogTest.cpp"
26+
"${PROJECT_SOURCE_DIR}/NumericalTest.cpp"
27+
)
28+
29+
set(FILE_SOURCES
30+
# "${PROJECT_SOURCE_DIR}/../data_processing/ImageProcessing.cpp"
31+
"${PROJECT_SOURCE_DIR}/../data_processing/Numerical.cpp"
32+
)
33+
34+
# Add test executable
35+
add_executable(RNExecutorchTests "${PROJECT_SOURCE_DIR}/TestRunner.cpp" ${TEST_SOURCES} ${FILE_SOURCES})
2236

2337
# Libraries linking
24-
target_link_libraries(NumericalTests gtest gtest_main)
25-
target_link_libraries(LogTests gtest gtest_main)
38+
target_link_libraries(RNExecutorchTests gtest gtest_main)
2639

2740
# Testing functionalities
28-
enable_testing()
29-
add_test(NAME NumericalTests COMMAND NumericalTests)
30-
add_test(NAME LogTests COMMAND LogTests)
41+
add_test(NAME RNExecutorchTests COMMAND RNExecutorchTests)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "data_processing/ImageProcessing.h"
2+
#include <filesystem>
3+
#include <gtest/gtest.h>
4+
#include <opencv2/opencv.hpp>
5+
6+
namespace rnexecutorch::image_processing {
7+
8+
namespace fs = std::filesystem;
9+
10+
class ImageProcessingTests : public ::testing::Test {
11+
protected:
12+
cv::Mat testImage;
13+
void SetUp() override {
14+
testImage = cv::Mat::ones(100, 100, CV_8UC3) * 255; // white test image
15+
}
16+
};
17+
18+
// Test for saveToTempFile
19+
TEST_F(ImageProcessingTests, SaveToTempFile) {
20+
std::string filePath = saveToTempFile(testImage);
21+
ASSERT_TRUE(filePath.starts_with("file://"));
22+
ASSERT_TRUE(filePath.ends_with(".png"));
23+
ASSERT_TRUE(fs::exists(filePath.substr(7)));
24+
ASSERT_NO_THROW(auto mat = cv::imread(filePath, cv::IMREAD_COLOR));
25+
ASSERT(!mat.empty());
26+
27+
fs::remove(filePath.substr(7));
28+
}
29+
30+
} // namespace rnexecutorch::image_processing

packages/react-native-executorch/common/rnexecutorch/tests/LogTest.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -522,9 +522,4 @@ TEST(MovingSequencable, MovingSequencableTest) {
522522
ASSERT_EQ(q.size(), 0);
523523
}
524524

525-
} // namespace rnexecutorch
526-
527-
int main(int argc, char **argv) {
528-
::testing::InitGoogleTest(&argc, argv);
529-
return RUN_ALL_TESTS();
530-
}
525+
} // namespace rnexecutorch
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <gtest/gtest.h>
2+
3+
int main(int argc, char **argv) {
4+
::testing::InitGoogleTest(&argc, argv);
5+
return RUN_ALL_TESTS();
6+
}

0 commit comments

Comments
 (0)