merged main into feature/polytrope

This commit is contained in:
2025-02-23 11:47:29 -05:00
86 changed files with 3732 additions and 653 deletions

111
tests/config/configTest.cpp Normal file
View File

@@ -0,0 +1,111 @@
#include <gtest/gtest.h>
#include "config.h"
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <sstream>
#include <algorithm>
std::string EXAMPLE_FILENAME = std::string(getenv("MESON_SOURCE_ROOT")) + "/tests/config/example.yaml";
/**
* @file configTest.cpp
* @brief Unit tests for the Config class.
*/
class configTestPrivateAccessor {
public:
static bool callIsKeyInCache(Config& config, const std::string& key) {
return config.isKeyInCache(key);
}
static int callCacheSize(Config& config) {
return config.configMap.size();
}
static void callAddToCache(Config& config, const std::string& key, const YAML::Node& node) {
config.addToCache(key, node);
}
static void callRegisterKeyNotFound(Config& config, const std::string& key) {
config.registerUnknownKey(key);
}
static bool CheckIfKeyUnknown(Config& config, const std::string& key) {
if (std::find(config.unknownKeys.begin(), config.unknownKeys.end(), key) == config.unknownKeys.end()) {
return false;
}
return true;
}
};
/**
* @brief Test suite for the Config class.
*/
class configTest : public ::testing::Test {};
/**
* @brief Test the constructor of the Config class.
*/
TEST_F(configTest, constructor) {
EXPECT_NO_THROW(Config::getInstance());
}
TEST_F(configTest, loadConfig) {
Config& config = Config::getInstance();
EXPECT_TRUE(config.loadConfig(EXAMPLE_FILENAME));
}
TEST_F(configTest, singletonTest) {
Config& config1 = Config::getInstance();
Config& config2 = Config::getInstance();
EXPECT_EQ(&config1, &config2);
}
TEST_F(configTest, getTest) {
Config& config = Config::getInstance();
config.loadConfig(EXAMPLE_FILENAME);
int maxIter = config.get<int>("opac:lowTemp:numeric:maxIter", 10);
EXPECT_EQ(maxIter, 100);
EXPECT_NE(maxIter, 10);
std::string logLevel = config.get<std::string>("logLevel", "DEBUG");
EXPECT_EQ(logLevel, "INFO");
EXPECT_NE(logLevel, "DEBUG");
float polytropicIndex = config.get<float>("poly:physics:index", 2);
EXPECT_EQ(polytropicIndex, 1.5);
EXPECT_NE(polytropicIndex, 2);
float polytropicIndex2 = config.get<float>("poly:physics:index2", 2.0);
EXPECT_EQ(polytropicIndex2, 2.0);
}
TEST_F(configTest, secondSingletonTest) {
Config& config = Config::getInstance();
EXPECT_EQ(config.get<int>("opac:lowTemp:numeric:maxIter", 10), 100);
}
TEST_F(configTest, isKeyInCacheTest) {
Config& config = Config::getInstance();
config.loadConfig(EXAMPLE_FILENAME);
EXPECT_TRUE(configTestPrivateAccessor::callIsKeyInCache(config, "opac:lowTemp:numeric:maxIter"));
EXPECT_FALSE(configTestPrivateAccessor::callIsKeyInCache(config, "opac:lowTemp:numeric:maxIter2"));
}
TEST_F(configTest, cacheSize) {
Config& config = Config::getInstance();
config.loadConfig(EXAMPLE_FILENAME);
EXPECT_EQ(configTestPrivateAccessor::callCacheSize(config), 3);
EXPECT_NE(configTestPrivateAccessor::callCacheSize(config), 4);
config.get<std::string>("outputDir", "DEBUG");
EXPECT_EQ(configTestPrivateAccessor::callCacheSize(config), 4);
}
TEST_F(configTest, unknownKeyTest) {
Config& config = Config::getInstance();
config.loadConfig(EXAMPLE_FILENAME);
config.get<int>("opac:lowTemp:numeric:random", 10);
EXPECT_FALSE(configTestPrivateAccessor::CheckIfKeyUnknown(config, "opac:lowTemp:numeric:maxIter"));
EXPECT_TRUE(configTestPrivateAccessor::CheckIfKeyUnknown(config, "opac:lowTemp:numeric:random"));
}

32
tests/config/example.yaml Normal file
View File

@@ -0,0 +1,32 @@
# High level options
logLevel: "INFO"
outputDir: output
# Module options
poly:
numeric:
newtonTol: 1e-6
newtonMaxIter: 100
gmresTol: 1e-6
gmresMaxIter: 100
physics:
index: 1.5
# Module options
opac:
highTemp:
physics:
table: "/path/to/highTempTable.dat"
numeric:
tol: 1e-6
maxIter: 100
lowTemp:
physics:
table: "/path/to/lowTempTable.dat"
numeric:
tol: 1e-6
maxIter: 100
mesh:
structure:
refine: 2

25
tests/config/meson.build Normal file
View File

@@ -0,0 +1,25 @@
# Test files for const
test_sources = [
'configTest.cpp',
]
foreach test_file : test_sources
exe_name = test_file.split('.')[0]
message('Building test: ' + exe_name)
# Create an executable target for each test
test_exe = executable(
exe_name,
test_file,
dependencies: [gtest_dep, config_dep],
include_directories: include_directories('../../src/config/public'),
link_with: libconst, # Link the dobj library
install_rpath: '@loader_path/../../src' # Ensure runtime library path resolves correctly
)
# Add the executable as a test
test(
exe_name,
test_exe,
env: ['MESON_SOURCE_ROOT=' + meson.project_source_root(), 'MESON_BUILD_ROOT=' + meson.project_build_root()])
endforeach

View File

@@ -7,7 +7,9 @@ subdir('dobj')
subdir('const')
subdir('opatIO')
subdir('meshIO')
subdir('probe')
subdir('poly')
subdir('config')
# Subdirectories for sandbox tests
subdir('dobj_sandbox')

View File

@@ -13,7 +13,7 @@ foreach test_file : test_sources
test_exe = executable(
exe_name,
test_file,
dependencies: [gtest_dep],
dependencies: [gtest_dep, picosha2_dep],
include_directories: include_directories('../../src/opatIO/public'),
link_with: libopatIO, # Link the dobj library
install_rpath: '@loader_path/../../src' # Ensure runtime library path resolves correctly

View File

@@ -5,8 +5,10 @@
#include <vector>
#include <set>
#include <sstream>
#include <cstring>
#include "picosha2.h"
std::string EXAMPLE_FILENAME = std::string(getenv("MESON_SOURCE_ROOT")) + "/tests/opatIO/test.opat";
std::string EXAMPLE_FILENAME = std::string(getenv("MESON_SOURCE_ROOT")) + "/tests/opatIO/synthetic_tables.opat";
/**
* @file opatIOTest.cpp
@@ -32,6 +34,9 @@ TEST_F(opatIOTest, Constructor) {
OpatIO opatIO(EXAMPLE_FILENAME);
}
/**
* @test Verify the header is read correctly.
*/
TEST_F(opatIOTest, Header) {
OpatIO opatIO(EXAMPLE_FILENAME);
Header header = opatIO.getHeader();
@@ -39,42 +44,60 @@ TEST_F(opatIOTest, Header) {
EXPECT_EQ(header.numTables, 20);
EXPECT_EQ(header.headerSize, 256);
EXPECT_EQ(header.indexOffset, 416416);
EXPECT_EQ(std::string(header.creationDate), "Feb 16, 2025");
EXPECT_EQ(std::string(header.sourceInfo), "no source provided by user");
EXPECT_EQ(std::string(header.comment), "default header");
EXPECT_EQ(std::string(header.creationDate), "Feb 17, 2025");
EXPECT_EQ(std::string(header.sourceInfo), "utils/opatio/utils/mkTestData.py");
EXPECT_EQ(std::string(header.comment), "Synthetic Opacity Tables");
EXPECT_EQ(header.numIndex, 2);
}
/**
* @test Verify the number of index values is correct. Also check the byte position and index vector
*/
TEST_F(opatIOTest, TableIndex) {
OpatIO opatIO(EXAMPLE_FILENAME);
std::vector<TableIndex> tableIndex = opatIO.getTableIndex();
EXPECT_EQ(tableIndex.size(), 20);
EXPECT_EQ(tableIndex[0].X, 0.1);
EXPECT_EQ(tableIndex[0].Z, 0.001);
EXPECT_EQ(tableIndex[0].index.at(0), 0.1);
EXPECT_EQ(tableIndex[0].index.at(1), 0.001);
EXPECT_EQ(tableIndex[0].byteStart, 256);
EXPECT_EQ(tableIndex[0].byteEnd, 21064);
}
/**
* @test Verify the maxQDepth (for caching)
*/
TEST_F(opatIOTest, MaxQDepth) {
OpatIO opatIO(EXAMPLE_FILENAME);
EXPECT_EQ(opatIO.getMaxQDepth(), 10);
EXPECT_EQ(opatIO.getMaxQDepth(), 20);
opatIO.setMaxQDepth(5);
EXPECT_EQ(opatIO.getMaxQDepth(), 5);
}
/**
* @test Verify the Unload function
*/
TEST_F(opatIOTest, Unload){
OpatIO opatIO(EXAMPLE_FILENAME);
EXPECT_NO_THROW(opatIO.unload());
EXPECT_FALSE(opatIO.isLoaded());
}
/**
* @test Verify the lookupTableID function
*/
TEST_F(opatIOTest, LookupTableID) {
OpatIO opatIO(EXAMPLE_FILENAME);
EXPECT_EQ(opatIO.lookupTableID(0.321053, 0.0116842), 7);
std::vector<double> index = {0.321053, 0.0116842};
EXPECT_EQ(opatIO.lookupTableID(index), 7);
}
/**
* @test Verify the GetTable function by checking the first 2x2 square of the table
*/
TEST_F(opatIOTest, GetTable) {
OpatIO opatIO(EXAMPLE_FILENAME);
OPATTable tab = opatIO.getTable(0.1, 0.001);
std::vector<double> index = {0.1, 0.001};
OPATTable tab = opatIO.getTable(index);
EXPECT_EQ(tab.N_R, 50);
EXPECT_EQ(tab.N_T, 50);
EXPECT_DOUBLE_EQ(tab.logR[0], -8.0);
@@ -83,4 +106,28 @@ TEST_F(opatIOTest, GetTable) {
EXPECT_DOUBLE_EQ(tab.logKappa[0][1], 1.8028572256396647);
EXPECT_DOUBLE_EQ(tab.logKappa[1][0], 1.8783385110582342);
EXPECT_DOUBLE_EQ(tab.logKappa[1][1], 1.1005312934444582);
}
/**
* @test Verify the GetTable function by computing the checksum of the first table and
* comparing it to the stored checksum
*/
TEST_F(opatIOTest, Checksum) {
OpatIO opatIO(EXAMPLE_FILENAME);
std::vector<double> index = {0.1, 0.001};
TableIndex tableIndex = opatIO.getTableIndex(index);
std::vector<unsigned char> hash = opatIO.computeChecksum(index);
std::string hexRepr = picosha2::bytes_to_hex_string(hash);
std::vector<unsigned char> storedHashVec(tableIndex.sha256, tableIndex.sha256 + 32);
std::string storedHexRepr = picosha2::bytes_to_hex_string(storedHashVec);
EXPECT_EQ(hexRepr, storedHexRepr);
}
TEST_F(opatIOTest, ChecksumAll) {
OpatIO opatIO(EXAMPLE_FILENAME);
opatIO.setMaxQDepth(5);
EXPECT_TRUE(opatIO.validateAll());
}

Binary file not shown.

20
tests/probe/meson.build Normal file
View File

@@ -0,0 +1,20 @@
# Test files for dobj
test_sources = [
'probeTest.cpp',
]
foreach test_file : test_sources
exe_name = test_file.split('.')[0]
message('Building test: ' + exe_name)
# Create an executable target for each test
test_exe = executable(
exe_name,
test_file,
dependencies: [gtest_dep, probe_dep, mfem_dep, quill_dep],
install_rpath: '@loader_path/../../src' # Ensure runtime library path resolves correctly
)
# Add the executable as a test
test(exe_name, test_exe)
endforeach

88
tests/probe/probeTest.cpp Normal file
View File

@@ -0,0 +1,88 @@
#include <gtest/gtest.h>
#include "probe.h"
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <sstream>
#include <regex>
#include <source_location>
#include <chrono>
#include "quill/LogMacros.h"
std::string getLastLine(const std::string& filename) {
std::ifstream file(filename);
std::string line, lastLine;
if (!file.is_open()) {
throw std::runtime_error("Could not open file");
}
while (std::getline(file, line)) {
lastLine = line;
}
return lastLine; // Returns the last non-empty line
}
std::string stripTimestamps(const std::string& logLine) {
std::regex logPattern(R"(\d+:\d+:\d+\.\d+\s+\[\d+\]\s+probeTest\.cpp:\d+\s+LOG_INFO\s+[A-Za-z]*\s+(.*))");
std::smatch match;
if (std::regex_match(logLine, match, logPattern) && match.size() > 1) {
return match[1].str(); // Extract log message after timestamp
}
return logLine; // Return as-is if pattern doesn't match
}
class probeTest : public ::testing::Test {};
TEST_F(probeTest, DefaultConstructorTest) {
EXPECT_NO_THROW(Probe::LogManager::getInstance());
}
TEST_F(probeTest, waitTest) {
auto start = std::chrono::high_resolution_clock::now();
Probe::wait(1);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = end - start;
EXPECT_LE(elapsed.count(), 1.1);
}
TEST_F(probeTest, getLoggerTest) {
Probe::LogManager& logManager = Probe::LogManager::getInstance();
std::string loggerName = "log";
quill::Logger* logger = logManager.getLogger(loggerName);
EXPECT_NE(logger, nullptr);
LOG_INFO(logger, "This is a test message");
// Wait for the log to be written by calling getLastLine until it is non empty
std::string lastLine;
while (lastLine.empty()) {
lastLine = getLastLine("4DSSE.log");
}
EXPECT_EQ(stripTimestamps(lastLine), "This is a test message");
}
TEST_F(probeTest, newFileLoggerTest) {
Probe::LogManager& logManager = Probe::LogManager::getInstance();
const std::string loggerName = "newLog";
const std::string filename = "newLog.log";
quill::Logger* logger = logManager.newFileLogger(filename, loggerName);
EXPECT_NE(logger, nullptr);
LOG_INFO(logger, "This is a new test message");
// Wait for the log to be written by calling getLastLine until it is non empty
std::string lastLine;
while (lastLine.empty()) {
lastLine = getLastLine(filename);
}
EXPECT_EQ(stripTimestamps(lastLine), "This is a new test message");
}
TEST_F(probeTest, getLoggerNames) {
Probe::LogManager& logManager = Probe::LogManager::getInstance();
std::vector<std::string> loggerNames = logManager.getLoggerNames();
EXPECT_EQ(loggerNames.size(), 3);
EXPECT_EQ(loggerNames.at(0), "log");
EXPECT_EQ(loggerNames.at(1), "newLog");
EXPECT_EQ(loggerNames.at(2), "stdout");
}