Skip to content

Commit

Permalink
Handling local test files is added. Matrices comparison is now separa…
Browse files Browse the repository at this point in the history
…te rotine that can be reused.
  • Loading branch information
vidanovic committed Nov 1, 2024
1 parent ac8d6b7 commit ea8e593
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 74 deletions.
21 changes: 15 additions & 6 deletions src/MultiLayerOptics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,22 @@ install(TARGETS ${target_name}
if( BUILD_WCE_TESTING )
# common helper files for the testing
include_directories( ../helper )
file( GLOB all_test_src RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "../helper/*.hpp" )
file( GLOB all_test_src RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "../helper/*.cpp" )
LIST( APPEND test_src ${all_test_src} )

# Collect helper files
file( GLOB helper_test_src RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "../helper/*.hpp" "../helper/*.cpp")
list( APPEND test_src ${helper_test_src} )

# Include main test files
include_directories( include )
file( GLOB all_test_src RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" tst/units/*.cpp )
set( test_src ${all_test_src} )
file( GLOB unit_test_src RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "tst/units/*.cpp" )
list( APPEND test_src ${unit_test_src} )

# Create test targets
CREATE_TEST_TARGETS_WCE( ${target_name} "${test_src}" "" )
endif ()

# Set test data directory
set(TEST_DATA_DIR "${CMAKE_CURRENT_SOURCE_DIR}/tst")
target_compile_definitions(${target_name}_tests PRIVATE TEST_DATA_DIR="${TEST_DATA_DIR}")
endif()

warning_level_update_wce()
22 changes: 16 additions & 6 deletions src/SingleLayerOptics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,23 @@ install(TARGETS ${target_name}
if( BUILD_WCE_TESTING )
# common helper files for the testing
include_directories( ../helper )
file( GLOB all_test_src RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "../helper/*.hpp" )
file( GLOB all_test_src RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "../helper/*.cpp" )
LIST( APPEND test_src ${all_test_src} )

# Collect helper files
file( GLOB helper_test_src RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "../helper/*.hpp" "../helper/*.cpp")
list( APPEND test_src ${helper_test_src} )

# Include main test files
include_directories( include )
file( GLOB all_test_src RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" tst/units/*.cpp )
set( test_src ${all_test_src} )
file( GLOB unit_test_src RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "tst/units/*.cpp" )
list( APPEND test_src ${unit_test_src} )

# Create test targets
CREATE_TEST_TARGETS_WCE( ${target_name} "${test_src}" "" )
endif ()

# Set test data directory
set(TEST_DATA_DIR "${CMAKE_CURRENT_SOURCE_DIR}/tst")
target_compile_definitions(${target_name}_tests PRIVATE TEST_DATA_DIR="${TEST_DATA_DIR}")
endif()


warning_level_update_wce()
76 changes: 15 additions & 61 deletions src/SingleLayerOptics/tst/units/VenetianUniformShadeMatrix.unit.cpp

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Tarcog/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ if( BUILD_WCE_TESTING )
file( GLOB all_test_src RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "../helper/*.cpp" )
LIST( APPEND test_src ${all_test_src} )

include_directories( include )
include_directories( include )
file( GLOB all_test_src RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" tst/units/*.cpp )
set( test_src ${all_test_src} )
CREATE_TEST_TARGETS_WCE( ${target_name} "${test_src}" "" )
Expand Down
59 changes: 59 additions & 0 deletions src/helper/csvHandlers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <iomanip>

#include "csvHandlers.hpp"

namespace Helper
{
void writeVectorToCSV(const std::vector<std::vector<double>> & data,
const std::string & filename)
{
std::ofstream file(filename);
if(!file.is_open())
{
throw std::runtime_error("Could not open file to write");
}

file << std::fixed << std::setprecision(9); // Set precision to 9 decimal places

for(const auto & row : data)
{
for(size_t i = 0; i < row.size(); ++i)
{
file << row[i];
if(i < row.size() - 1)
{
file << ","; // Add comma if not the last element
}
}
file << "\n";
}
file.close();
}

std::vector<std::vector<double>> readVectorFromCSV(const std::string & filename)
{
std::ifstream file(filename);
if(!file.is_open())
{
throw std::runtime_error("Could not open file to read");
}

std::vector<std::vector<double>> data;
std::string line;

while(std::getline(file, line))
{
std::vector<double> row;
std::stringstream ss(line);
std::string value;

while(std::getline(ss, value, ','))
{
row.push_back(std::stod(value)); // Convert string to double and add to row
}
data.push_back(row);
}
file.close();
return data;
}
} // namespace Helper
18 changes: 18 additions & 0 deletions src/helper/csvHandlers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include <iostream>

namespace Helper
{
// Function to write a 2D vector to a CSV file
void writeVectorToCSV(const std::vector<std::vector<double>> & data,
const std::string & filename);

// Function to read a 2D vector from a CSV file
std::vector<std::vector<double>> readVectorFromCSV(const std::string & filename);
} // namespace Helper
19 changes: 19 additions & 0 deletions src/helper/matrixTesting.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "matrixTesting.hpp"

namespace Helper
{
void compareMatrices(const std::vector<std::vector<double>> & expected,
const std::vector<std::vector<double>> & actual,
double tolerance)
{
ASSERT_EQ(expected.size(), actual.size());
for(size_t i = 0; i < expected.size(); ++i)
{
ASSERT_EQ(expected[i].size(), actual[i].size());
for(size_t j = 0; j < expected[i].size(); ++j)
{
EXPECT_NEAR(expected[i][j], actual[i][j], tolerance);
}
}
}
}
11 changes: 11 additions & 0 deletions src/helper/matrixTesting.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <vector>
#include "gtest/gtest.h"

namespace Helper
{
void compareMatrices(const std::vector<std::vector<double>> & expected,
const std::vector<std::vector<double>> & actual,
double tolerance = 1e-6);
}

0 comments on commit ea8e593

Please sign in to comment.