Skip to content

Commit

Permalink
Merge pull request #148 from NeurodataWithoutBorders/enh_unit_coverage
Browse files Browse the repository at this point in the history
Enhance unit test coverage
  • Loading branch information
oruebel authored Feb 5, 2025
2 parents 00a529c + d5f4e5d commit 9818339
Show file tree
Hide file tree
Showing 5 changed files with 447 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/io/BaseIO.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,24 @@ class BaseRecordingData
const BaseDataType& type,
const std::vector<std::string>& data) = 0;

/**
* @brief Get the number of dimensions in the dataset.
* @return The number of dimensions.
*/
inline SizeType getNumDimensions() const { return nDimensions; }

/**
* @brief Get the size of the dataset.
* @return Vector containing the size in each dimension.
*/
inline const std::vector<SizeType>& getSize() const { return size; }

/**
* @brief Get the current position in the dataset.
* @return Vector containing the position in each dimension.
*/
inline const std::vector<SizeType>& getPosition() const { return position; }

protected:
/**
* @brief The size of the dataset in each dimension.
Expand All @@ -576,7 +594,7 @@ class BaseRecordingData
std::vector<SizeType> position;

/**
* @brief The number of dimensions in the data block.
* @brief Number of dimensions in the dataset.
*/
SizeType nDimensions;
};
Expand Down
2 changes: 2 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ include(Catch)

add_executable(aqnwb_test
testBaseIO.cpp
testChannel.cpp
testData.cpp
testDevice.cpp
testHDF5RecordingData.cpp
testEcephys.cpp
testElementIdentifiers.cpp
testFile.cpp
Expand Down
186 changes: 186 additions & 0 deletions tests/testChannel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
#include <catch2/catch_approx.hpp>
#include <catch2/matchers/catch_matchers_range_equals.hpp>

#include "Channel.hpp"
#include "testUtils.hpp"

TEST_CASE("Test Channel Construction and Basic Operations", "[channel]")
{
SECTION("Default constructor parameters")
{
Channel ch("test_channel", "test_group", 1, 2, 3);
REQUIRE(ch.getName() == "test_channel");
REQUIRE(ch.getGroupName() == "test_group");
REQUIRE(ch.getGroupIndex() == 1);
REQUIRE(ch.getLocalIndex() == 2);
REQUIRE(ch.getGlobalIndex() == 3);
REQUIRE(ch.getConversion()
== Catch::Approx(0.05e-6f).epsilon(
0.001)); // bitVolts/conversion = 0.05/1e6
REQUIRE(ch.getSamplingRate() == Catch::Approx(30000.f).epsilon(0.001));
REQUIRE(ch.getBitVolts() == Catch::Approx(0.05f).epsilon(0.001));
std::array<float, 3> pos = {0.f, 0.f, 0.f};
const auto& actualPos = ch.getPosition();
REQUIRE_THAT(actualPos,
Catch::Matchers::RangeEquals(pos, approxComparator));
REQUIRE(ch.getComments() == "no comments");
}

SECTION("Custom constructor parameters")
{
std::array<float, 3> pos = {1.f, 2.f, 3.f};
Channel ch("test_channel",
"test_group",
1,
2,
3,
2e6f, // custom conversion
44100.f, // custom sampling rate
0.1f, // custom bitVolts
pos, // custom position
"test comment" // custom comment
);

REQUIRE(ch.getName() == "test_channel");
REQUIRE(ch.getGroupName() == "test_group");
REQUIRE(ch.getGroupIndex() == 1);
REQUIRE(ch.getLocalIndex() == 2);
REQUIRE(ch.getGlobalIndex() == 3);
REQUIRE(ch.getConversion() == Catch::Approx(0.1f / 2e6f).epsilon(0.001));
REQUIRE(ch.getSamplingRate() == Catch::Approx(44100.f).epsilon(0.001));
REQUIRE(ch.getBitVolts() == Catch::Approx(0.1f).epsilon(0.001));
const auto& actualPos = ch.getPosition();
REQUIRE_THAT(actualPos,
Catch::Matchers::RangeEquals(pos, approxComparator));
REQUIRE(ch.getComments() == "test comment");
}
}

TEST_CASE("Test Channel Setters", "[channel]")
{
Channel ch("test_channel", "test_group", 1, 2, 3);

SECTION("Set comments")
{
ch.setComments("new comment");
REQUIRE(ch.getComments() == "new comment");
}

SECTION("Set position")
{
std::array<float, 3> newPos = {4.f, 5.f, 6.f};
ch.setPosition(newPos);
const auto& actualPos = ch.getPosition();
REQUIRE_THAT(actualPos,
Catch::Matchers::RangeEquals(newPos, approxComparator));
}

SECTION("Set name")
{
ch.setName("new_channel");
REQUIRE(ch.getName() == "new_channel");
}
}

TEST_CASE("Test Channel Copy and Move Operations", "[channel]")
{
std::array<float, 3> pos = {1.f, 2.f, 3.f};
Channel original("test_channel",
"test_group",
1,
2,
3,
2e6f,
44100.f,
0.1f,
pos,
"test comment");

SECTION("Copy constructor")
{
Channel copy(original);
REQUIRE(copy.getName() == original.getName());
REQUIRE(copy.getGroupName() == original.getGroupName());
REQUIRE(copy.getGroupIndex() == original.getGroupIndex());
REQUIRE(copy.getLocalIndex() == original.getLocalIndex());
REQUIRE(copy.getGlobalIndex() == original.getGlobalIndex());
REQUIRE(copy.getConversion()
== Catch::Approx(original.getConversion()).epsilon(0.001));
REQUIRE(copy.getSamplingRate()
== Catch::Approx(original.getSamplingRate()).epsilon(0.001));
REQUIRE(copy.getBitVolts()
== Catch::Approx(original.getBitVolts()).epsilon(0.001));
const auto& origPos = original.getPosition();
const auto& copyPos = copy.getPosition();
REQUIRE_THAT(copyPos,
Catch::Matchers::RangeEquals(origPos, approxComparator));
REQUIRE(copy.getComments() == original.getComments());
}

SECTION("Copy assignment")
{
Channel copy("other", "other_group", 0, 0, 0);
copy = original;
REQUIRE(copy.getName() == original.getName());
REQUIRE(copy.getGroupName() == original.getGroupName());
REQUIRE(copy.getGroupIndex() == original.getGroupIndex());
REQUIRE(copy.getLocalIndex() == original.getLocalIndex());
REQUIRE(copy.getGlobalIndex() == original.getGlobalIndex());
REQUIRE(copy.getConversion()
== Catch::Approx(original.getConversion()).epsilon(0.001));
REQUIRE(copy.getSamplingRate()
== Catch::Approx(original.getSamplingRate()).epsilon(0.001));
REQUIRE(copy.getBitVolts()
== Catch::Approx(original.getBitVolts()).epsilon(0.001));
const auto& origPos = original.getPosition();
const auto& copyPos = copy.getPosition();
REQUIRE_THAT(copyPos,
Catch::Matchers::RangeEquals(origPos, approxComparator));
REQUIRE(copy.getComments() == original.getComments());
}

SECTION("Move constructor")
{
Channel temp(original);
Channel moved(std::move(temp));
REQUIRE(moved.getName() == original.getName());
REQUIRE(moved.getGroupName() == original.getGroupName());
REQUIRE(moved.getGroupIndex() == original.getGroupIndex());
REQUIRE(moved.getLocalIndex() == original.getLocalIndex());
REQUIRE(moved.getGlobalIndex() == original.getGlobalIndex());
REQUIRE(moved.getConversion()
== Catch::Approx(original.getConversion()).epsilon(0.001));
REQUIRE(moved.getSamplingRate()
== Catch::Approx(original.getSamplingRate()).epsilon(0.001));
REQUIRE(moved.getBitVolts()
== Catch::Approx(original.getBitVolts()).epsilon(0.001));
const auto& origPos = original.getPosition();
const auto& movedPos = moved.getPosition();
REQUIRE_THAT(movedPos,
Catch::Matchers::RangeEquals(origPos, approxComparator));
REQUIRE(moved.getComments() == original.getComments());
}

SECTION("Move assignment")
{
Channel moved("other", "other_group", 0, 0, 0);
Channel temp(original);
moved = std::move(temp);
REQUIRE(moved.getName() == original.getName());
REQUIRE(moved.getGroupName() == original.getGroupName());
REQUIRE(moved.getGroupIndex() == original.getGroupIndex());
REQUIRE(moved.getLocalIndex() == original.getLocalIndex());
REQUIRE(moved.getGlobalIndex() == original.getGlobalIndex());
REQUIRE(moved.getConversion()
== Catch::Approx(original.getConversion()).epsilon(0.001));
REQUIRE(moved.getSamplingRate()
== Catch::Approx(original.getSamplingRate()).epsilon(0.001));
REQUIRE(moved.getBitVolts()
== Catch::Approx(original.getBitVolts()).epsilon(0.001));
const auto& origPos = original.getPosition();
const auto& movedPos = moved.getPosition();
REQUIRE_THAT(movedPos,
Catch::Matchers::RangeEquals(origPos, approxComparator));
REQUIRE(moved.getComments() == original.getComments());
}
}
Loading

0 comments on commit 9818339

Please sign in to comment.