Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] config validation #78

Merged
merged 1 commit into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/hibf/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct config
* \{
*/
//!\brief A lambda how to hash your input. TODO: Detailed docu needed!
std::function<void(size_t const, insert_iterator &&)> input_fn;
std::function<void(size_t const, insert_iterator &&)> input_fn{};

//!\brief Number of user bins
size_t number_of_user_bins{};
Expand All @@ -53,7 +53,7 @@ struct config
uint8_t sketch_bits{12};

//!\brief The maximum number of technical bins on each IBF in the HIBF.
uint16_t tmax{64};
uint16_t tmax{};

/*\brief A scaling factor to influence the amount of merged bins produced by the algorithm.
*
Expand Down
6 changes: 5 additions & 1 deletion src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void config::validate_and_set_defaults()
if (tmax == 0) // no tmax was set by the user on the command line
{
// Set default as sqrt(#samples). Experiments showed that this is a reasonable default.
if (number_of_user_bins >= 1ULL << 32) // sqrt is bigger than uint16_t
if (number_of_user_bins > 4'286'582'784ULL) // https://godbolt.org/z/oPh18s7cM
{
throw std::invalid_argument{
"[HIBF CONFIG ERROR] Too many user bins to compute a default tmax. " // GCOVR_EXCL_LINE
Expand All @@ -85,6 +85,10 @@ void config::validate_and_set_defaults()
tmax = seqan::hibf::next_multiple_of_64(static_cast<uint16_t>(std::ceil(std::sqrt(number_of_user_bins))));
}
}
else if (tmax > 65472u) // next_multiple_of_64 would return 65536, does not fit in uint16_t
{
throw std::invalid_argument{"[HIBF CONFIG ERROR] The maximum possible tmax is 65472."};
}
else if (tmax % 64 != 0)
{
tmax = seqan::hibf::next_multiple_of_64(tmax);
Expand Down
114 changes: 114 additions & 0 deletions test/unit/hibf/config_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,117 @@ TEST(config_test, read_from_with_more_meta)
EXPECT_EQ(configuration.disable_estimate_union, true);
EXPECT_EQ(configuration.disable_rearrangement, false);
}

TEST(config_test, validate_and_set_defaults)
{
// Set disable_rearrangement if disable_estimate_union is set
{
seqan::hibf::config configuration{.number_of_user_bins = 1u, .disable_estimate_union = true};
EXPECT_TRUE(configuration.disable_estimate_union);
EXPECT_FALSE(configuration.disable_rearrangement);

testing::internal::CaptureStderr();
configuration.validate_and_set_defaults();
EXPECT_TRUE(configuration.disable_estimate_union);
EXPECT_TRUE(configuration.disable_rearrangement);
EXPECT_EQ((testing::internal::GetCapturedStderr()), "");
}

// number_of_user_bins cannot be 0
{
seqan::hibf::config configuration{};

try
{
configuration.validate_and_set_defaults();
FAIL();
}
catch (std::invalid_argument const & exception)
{
EXPECT_STREQ("[HIBF CONFIG ERROR] You did not set the required config::number_of_user_bins.",
exception.what());
}
catch (...)
{
FAIL();
}
}

// Set default tmax
{
seqan::hibf::config configuration{.number_of_user_bins = 4'286'582'784ULL};
EXPECT_EQ(configuration.tmax, 0u);

testing::internal::CaptureStderr();
configuration.validate_and_set_defaults();
EXPECT_EQ(configuration.tmax, 65472u);
EXPECT_EQ((testing::internal::GetCapturedStderr()), "");
}

// Cannot set default tmax
{
seqan::hibf::config configuration{.number_of_user_bins = 4'286'582'785ULL};
EXPECT_EQ(configuration.tmax, 0u);

try
{
configuration.validate_and_set_defaults();
FAIL();
}
catch (std::invalid_argument const & exception)
{
EXPECT_STREQ(
"[HIBF CONFIG ERROR] Too many user bins to compute a default tmax. Please set a tmax manually.",
exception.what());
}
catch (...)
{
FAIL();
}
}

// Given tmax OK
{
seqan::hibf::config configuration{.number_of_user_bins = 1u, .tmax = 65472u};

testing::internal::CaptureStderr();
configuration.validate_and_set_defaults();

EXPECT_EQ(configuration.number_of_user_bins, 1u);
EXPECT_EQ(configuration.tmax, 65472u);
EXPECT_EQ((testing::internal::GetCapturedStderr()), "");
}

// Given tmax too big
{
seqan::hibf::config configuration{.number_of_user_bins = 1u, .tmax = 65473u};

try
{
configuration.validate_and_set_defaults();
FAIL();
}
catch (std::invalid_argument const & exception)
{
EXPECT_STREQ("[HIBF CONFIG ERROR] The maximum possible tmax is 65472.", exception.what());
}
catch (...)
{
FAIL();
}
}

// Given tmax is not a multiple of 64
{
seqan::hibf::config configuration{.number_of_user_bins = 32u, .tmax = 32u};
EXPECT_EQ(configuration.tmax, 32u);

testing::internal::CaptureStderr();
configuration.validate_and_set_defaults();
EXPECT_EQ(configuration.tmax, 64u);
EXPECT_EQ((testing::internal::GetCapturedStderr()),
"[HIBF CONFIG WARNING]: Your requested number of technical bins was not a multiple of 64. Due to the "
"architecture of the HIBF, it will use up space equal to the next multiple of 64 anyway, so we "
"increased your number of technical bins to 64.\n");
}
}