Skip to content

Commit

Permalink
Switch to our test utilities for float comparisons.
Browse files Browse the repository at this point in the history
  • Loading branch information
LTLA committed Jun 24, 2024
1 parent 8fe8f21 commit 6223f48
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
8 changes: 8 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ FetchContent_Declare(
URL https://github.com/google/googletest/archive/1d17ea141d2c11b8917d2c7d029f1c4e2b9769b2.zip
)

FetchContent_Declare(
scran_test_utils
GIT_REPOSITORY https://github.com/libscran/test_utils
GIT_TAG master
)

# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
FetchContent_MakeAvailable(scran_test_utils)

enable_testing()

Expand All @@ -29,6 +36,7 @@ macro(create_test name)
${name}
gtest_main
scran_core_utils
scran_test_utils
)

if(DO_CODE_COVERAGE)
Expand Down
17 changes: 5 additions & 12 deletions tests/src/average_vectors.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <gtest/gtest.h>
#include "scran/average_vectors.hpp"
#include "compare_almost_equal.h"
#include <vector>

static int full_of_nans(const std::vector<double>& vec) {
Expand All @@ -10,14 +11,6 @@ static int full_of_nans(const std::vector<double>& vec) {
return nan_count;
}

template<typename Left_, typename Right_>
void equal_float_vectors(const std::vector<Left_>& left, const std::vector<Right_>& right) {
ASSERT_EQ(left.size(), right.size());
for (size_t i = 0; i < left.size(); ++i) {
EXPECT_FLOAT_EQ(left[i], right[i]);
}
}

TEST(AverageVectors, Simple) {
std::vector<std::vector<double> > stuff {
std::vector<double>{1, 2, 3, 4, 5},
Expand All @@ -27,7 +20,7 @@ TEST(AverageVectors, Simple) {

auto out = scran::average_vectors::compute(5, std::vector<double*>{stuff[0].data(), stuff[1].data(), stuff[2].data()}, false);
std::vector<double> ref {2, 10.0/3, 8.0/3, 13/3.0, 5.0};
equal_float_vectors(ref, out);
compare_almost_equal(ref, out);

// Optimization when there's just one, or none.
auto out_opt = scran::average_vectors::compute(5, std::vector<double*>{stuff[0].data()}, false);
Expand Down Expand Up @@ -64,7 +57,7 @@ TEST(AverageVectors, Weighted) {
std::vector<double> weights3 { 0.5, 2, 1.5 };
auto out3 = scran::average_vectors::compute_weighted(5, ptrs, weights3.data(), false);
std::vector<double> ref3{ 2.250, 3.375, 2.500, 4.625, 5.375 };
equal_float_vectors(ref3, out3);
compare_almost_equal(ref3, out3);

// Optimizations.
auto out_opt = scran::average_vectors::compute_weighted(5, std::vector<double*>{stuff[0].data()}, weights1.data(), false);
Expand Down Expand Up @@ -92,12 +85,12 @@ TEST(AverageVectors, Missings) {

auto out = scran::average_vectors::compute(5, std::vector<double*>{stuff[0].data(), stuff[1].data(), stuff[2].data()}, true);
std::vector<double> ref {2, 9.0/2, 5.0/2, 4.0};
equal_float_vectors(ref, std::vector<double>(out.begin(), out.begin() + 4));
compare_almost_equal(ref, std::vector<double>(out.begin(), out.begin() + 4));
EXPECT_TRUE(std::isnan(out[4]));

std::vector<double> weights { 2, 3, 5 };
auto wout = scran::average_vectors::compute_weighted(5, std::vector<double*>{stuff[0].data(), stuff[1].data(), stuff[2].data()}, weights.data(), true);
std::vector<double> wref {(2 + 6 + 15)/10.0, (4 + 35)/(2.0 + 5.0), (6 + 15)/(3.0 + 5.0), 4.0};
equal_float_vectors(wref, std::vector<double>(wout.begin(), wout.begin() + 4));
compare_almost_equal(wref, std::vector<double>(wout.begin(), wout.begin() + 4));
EXPECT_TRUE(std::isnan(wout[4]));
}

0 comments on commit 6223f48

Please sign in to comment.