diff --git a/CMakeLists.txt b/CMakeLists.txt index 8787fe3..fa00d24 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -103,6 +103,17 @@ if (SSHASH_BUILD_EXECUTABLES) target_link_libraries(test_minimizer PRIVATE sshash_static) add_test(NAME minimizer COMMAND test_minimizer) + # Keep independently contributed regression targets in separate files. + set(SSHASH_TEST_GLOB_OPTIONS) + if(NOT CMAKE_VERSION VERSION_LESS "3.12") + list(APPEND SSHASH_TEST_GLOB_OPTIONS CONFIGURE_DEPENDS) + endif() + file(GLOB SSHASH_TEST_REGISTRATIONS ${SSHASH_TEST_GLOB_OPTIONS} + "${CMAKE_CURRENT_SOURCE_DIR}/test/cmake/*.cmake") + foreach(registration IN LISTS SSHASH_TEST_REGISTRATIONS) + include("${registration}") + endforeach() + add_executable(check test/check.cpp) target_link_libraries(check PRIVATE sshash_static) diff --git a/include/spectrum_preserving_string_set.hpp b/include/spectrum_preserving_string_set.hpp index b5fd5eb..0d885a2 100644 --- a/include/spectrum_preserving_string_set.hpp +++ b/include/spectrum_preserving_string_set.hpp @@ -261,7 +261,7 @@ struct spectrum_preserving_string_set // strings_offsets.offset_to_id(res, p, k); } - if (res.kmer_offset < res.string_end - k + 1) return true; + if (res.kmer_offset >= res.string_begin and res.kmer_offset < res.string_end - k + 1) return true; return false; } }; diff --git a/test/cmake/encoded_boundaries.cmake b/test/cmake/encoded_boundaries.cmake new file mode 100644 index 0000000..dacdf76 --- /dev/null +++ b/test/cmake/encoded_boundaries.cmake @@ -0,0 +1,3 @@ +add_executable(test_encoded_boundaries test/test_encoded_boundaries.cpp) +target_link_libraries(test_encoded_boundaries PRIVATE sshash_static) +add_test(NAME encoded_boundaries COMMAND test_encoded_boundaries) diff --git a/test/test_encoded_boundaries.cpp b/test/test_encoded_boundaries.cpp new file mode 100644 index 0000000..0c98c50 --- /dev/null +++ b/test/test_encoded_boundaries.cpp @@ -0,0 +1,62 @@ +#include +#include + +#include "src/builder/build.cpp" +#include "src/dictionary.cpp" +#include "src/info.cpp" + +using namespace sshash; + +struct test_directory { + std::filesystem::path path = + std::filesystem::temp_directory_path() / + ("sshash-boundaries-" + + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count())); + + test_directory() { std::filesystem::create_directory(path); } + ~test_directory() { + std::error_code error; + std::filesystem::remove_all(path, error); + } +}; + +using dict_type = dictionary, encoded_offsets>; + +void verify(dict_type const& dict, std::vector const& strings) { + // The reverse complement TCGTC spans the second/third string boundary. + if (dict.lookup("GACGA").kmer_id != constants::invalid_uint64) { + throw std::runtime_error("absent k-mer GACGA reported present"); + } + uint64_t kmer_id = 0; + for (auto const& string : strings) { + for (uint64_t i = 0; i + dict.k() <= string.size(); ++i, ++kmer_id) { + if (dict.lookup(string.data() + i).kmer_id != kmer_id) { + throw std::runtime_error("stored k-mer lost at string boundary"); + } + } + } +} + +int main() { + try { + test_directory directory; + std::vector strings = {"CGGGGGC", "GTGTATT", "CGTCAGA", "CCACGAA"}; + auto input = directory.path / "input.fa"; + { + std::ofstream out(input); + out.exceptions(std::ios::failbit | std::ios::badbit); + for (auto const& string : strings) out << ">test\n" << string << '\n'; + } + build_configuration config; + config.k = 5; + config.m = 1; + config.verbose = false; + config.tmp_dirname = directory.path.string(); + dict_type dict; + dict.build(input.string(), config); + verify(dict, strings); + } catch (std::exception const& error) { + std::cerr << error.what() << '\n'; + return 1; + } +}