Skip to content

Commit

Permalink
Add unit tests for Utils.hpp (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
oruebel authored Jan 15, 2025
1 parent bf91541 commit e7f4b7e
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,16 @@ static inline std::string mergePaths(const std::string& path1,
while (start < path2.size() && path2[start] == '/') {
start++;
}
// Get path2 without trailing slashes
std::string path2Clean = path2.substr(start);
while (!path2Clean.empty() && path2Clean.back() == '/' && path2Clean != "/") {
path2Clean.pop_back();
}
// Append path2 to path1 with a "/" in between
if (!result.empty()) {
if (!result.empty() && !path2Clean.empty()) {
result += '/';
}
result += path2.substr(start);
result += path2Clean;

// Remove any potential occurrences of "//" and replace with "/"
size_t pos = result.find("//");
Expand All @@ -114,6 +119,11 @@ static inline std::string mergePaths(const std::string& path1,
pos = result.find("//", pos);
}

// Remove trailing "/" from final result if not root path
while (!result.empty() && result.back() == '/' && result != "/") {
result.pop_back();
}

return result;
}

Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ add_executable(aqnwb_test
testRecordingWorkflow.cpp
testRegisteredType.cpp
testTimeSeries.cpp
testUtilsFunctions.cpp
testVectorData.cpp
examples/test_HDF5IO_examples.cpp
examples/test_example.cpp
Expand Down
91 changes: 91 additions & 0 deletions tests/testUtilsFunctions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <regex>

#include "testUtils.hpp"

#include "Utils.hpp"

TEST_CASE("Test UUID generation", "[utils]")
{
// Test that generated UUIDs are valid
std::string uuid = AQNWB::generateUuid();

// UUID format regex (8-4-4-4-12 hex digits)
std::regex uuidRegex(
"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$");

SECTION("UUID format is valid")
{
REQUIRE(std::regex_match(uuid, uuidRegex));
}

SECTION("UUIDs are unique")
{
std::string uuid2 = AQNWB::generateUuid();
REQUIRE(uuid != uuid2);
}
}

TEST_CASE("Test current time format", "[utils]")
{
std::string time = AQNWB::getCurrentTime();

// ISO 8601 format regex with timezone offset
std::regex timeRegex(
"^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{6}[+-]\\d{2}:\\d{2}$");

SECTION("Time format is valid ISO 8601")
{
REQUIRE(std::regex_match(time, timeRegex));
}
}

TEST_CASE("Test IO creation", "[utils]")
{
std::string testFile = getTestFilePath("test_createIO.h5");

SECTION("Create HDF5 IO")
{
REQUIRE_NOTHROW(AQNWB::createIO("HDF5", testFile));
auto io = AQNWB::createIO("HDF5", testFile);
REQUIRE(io != nullptr);
}

SECTION("Invalid IO type throws")
{
REQUIRE_THROWS_AS(AQNWB::createIO("INVALID", testFile),
std::invalid_argument);
}
}

TEST_CASE("Test path merging", "[utils]")
{
SECTION("Basic path merging")
{
REQUIRE(AQNWB::mergePaths("path1", "path2") == "path1/path2");
REQUIRE(AQNWB::mergePaths("/path1", "path2") == "/path1/path2");
REQUIRE(AQNWB::mergePaths("path1/", "/path2") == "path1/path2");
REQUIRE(AQNWB::mergePaths("/path1/", "/path2/") == "/path1/path2");
REQUIRE(AQNWB::mergePaths("/path1/", "path2/") == "/path1/path2");
}

SECTION("Handle empty paths")
{
REQUIRE(AQNWB::mergePaths("", "path2") == "path2");
REQUIRE(AQNWB::mergePaths("path1", "") == "path1");
REQUIRE(AQNWB::mergePaths("", "") == "");
REQUIRE(AQNWB::mergePaths("/", "") == "/");
}

SECTION("Handle root paths")
{
REQUIRE(AQNWB::mergePaths("/", "path2") == "/path2");
REQUIRE(AQNWB::mergePaths("/", "/path2") == "/path2");
REQUIRE(AQNWB::mergePaths("/", "/") == "/");
}

SECTION("Remove duplicate slashes")
{
REQUIRE(AQNWB::mergePaths("path1//", "//path2") == "path1/path2");
REQUIRE(AQNWB::mergePaths("path1///", "///path2") == "path1/path2");
}
}

0 comments on commit e7f4b7e

Please sign in to comment.