diff --git a/test/engine/TextIndexScanForWordTest.cpp b/test/engine/TextIndexScanForWordTest.cpp index 7e9b0c0fd9..eac3cb0d2f 100644 --- a/test/engine/TextIndexScanForWordTest.cpp +++ b/test/engine/TextIndexScanForWordTest.cpp @@ -80,9 +80,12 @@ std::string secondDocText = std::string docsFileContent = h::createDocsFileLine(4, firstDocText) + h::createDocsFileLine(7, secondDocText); +std::pair contentsOfWordsFileAndDocsFile = { + wordsFileContent, docsFileContent}; + TEST(TextIndexScanForWord, WordScanPrefix) { - auto qec = getQec(kg, true, true, true, 16_B, true, true, wordsFileContent, - docsFileContent); + auto qec = getQec(kg, true, true, true, 16_B, true, true, + contentsOfWordsFileAndDocsFile); TextIndexScanForWord s1{qec, Variable{"?text1"}, "test*"}; TextIndexScanForWord s2{qec, Variable{"?text2"}, "test*"}; @@ -171,8 +174,8 @@ TEST(TextIndexScanForWord, WordScanPrefix) { } TEST(TextIndexScanForWord, WordScanBasic) { - auto qec = getQec(kg, true, true, true, 16_B, true, true, wordsFileContent, - docsFileContent); + auto qec = getQec(kg, true, true, true, 16_B, true, true, + contentsOfWordsFileAndDocsFile); TextIndexScanForWord s1{qec, Variable{"?text1"}, "test"}; @@ -210,8 +213,8 @@ TEST(TextIndexScanForWord, WordScanBasic) { } TEST(TextIndexScanForWord, CacheKey) { - auto qec = getQec(kg, true, true, true, 16_B, true, true, wordsFileContent, - docsFileContent); + auto qec = getQec(kg, true, true, true, 16_B, true, true, + contentsOfWordsFileAndDocsFile); TextIndexScanForWord s1{qec, Variable{"?text1"}, "test*"}; TextIndexScanForWord s2{qec, Variable{"?text2"}, "test*"}; @@ -234,8 +237,8 @@ TEST(TextIndexScanForWord, CacheKey) { } TEST(TextIndexScanForWord, KnownEmpty) { - auto qec = getQec(kg, true, true, true, 16_B, true, true, wordsFileContent, - docsFileContent); + auto qec = getQec(kg, true, true, true, 16_B, true, true, + contentsOfWordsFileAndDocsFile); TextIndexScanForWord s1{qec, Variable{"?text1"}, "nonExistentWord*"}; ASSERT_TRUE(s1.knownEmptyResult()); diff --git a/test/util/IndexTestHelpers.cpp b/test/util/IndexTestHelpers.cpp index 72d1016b50..0dcfd334a6 100644 --- a/test/util/IndexTestHelpers.cpp +++ b/test/util/IndexTestHelpers.cpp @@ -140,8 +140,8 @@ Index makeTestIndex(const std::string& indexBasename, [[maybe_unused]] bool usePrefixCompression, ad_utility::MemorySize blocksizePermutations, bool createTextIndex, bool addWordsFromLiterals, - std::optional wordsFileContent, - std::optional docsFileContent) { + std::optional> + contentsOfWordsFileAndDocsFile) { // Ignore the (irrelevant) log output of the index building and loading during // these tests. static std::ostringstream ignoreLogStream; @@ -188,15 +188,15 @@ Index makeTestIndex(const std::string& indexBasename, std::nullopt}; index.createFromFiles({spec}); if (createTextIndex) { - if (wordsFileContent.has_value() && docsFileContent.has_value()) { + if (contentsOfWordsFileAndDocsFile.has_value()) { // Create and write to words- and docsfile to later build a full text // index from them ad_utility::File wordsFile(indexBasename + ".wordsfile", "w"); ad_utility::File docsFile(indexBasename + ".docsfile", "w"); - wordsFile.write(wordsFileContent.value().c_str(), - wordsFileContent.value().size()); - docsFile.write(docsFileContent.value().c_str(), - docsFileContent.value().size()); + wordsFile.write(contentsOfWordsFileAndDocsFile.value().first.c_str(), + contentsOfWordsFileAndDocsFile.value().first.size()); + docsFile.write(contentsOfWordsFileAndDocsFile.value().second.c_str(), + contentsOfWordsFileAndDocsFile.value().second.size()); wordsFile.close(); docsFile.close(); index.setKbName(indexBasename); @@ -246,8 +246,8 @@ QueryExecutionContext* getQec(std::optional turtleInput, bool usePrefixCompression, ad_utility::MemorySize blocksizePermutations, bool createTextIndex, bool addWordsFromLiterals, - std::optional wordsFileContent, - std::optional docsFileContent) { + std::optional> + contentsOfWordsFileAndDocsFile) { // Similar to `absl::Cleanup`. Calls the `callback_` in the destructor, but // the callback is stored as a `std::function`, which allows to store // different types of callbacks in the same wrapper type. @@ -294,22 +294,21 @@ QueryExecutionContext* getQec(std::optional turtleInput, std::string testIndexBasename = "_staticGlobalTestIndex" + std::to_string(contextMap.size()); contextMap.emplace( - key, - Context{TypeErasedCleanup{[testIndexBasename]() { - for (const std::string& indexFilename : - getAllIndexFilenames(testIndexBasename)) { - // Don't log when a file can't be deleted, - // because the logging might already be - // destroyed. - ad_utility::deleteFile(indexFilename, false); - } - }}, - std::make_unique(makeTestIndex( - testIndexBasename, turtleInput, loadAllPermutations, - usePatterns, usePrefixCompression, blocksizePermutations, - createTextIndex, addWordsFromLiterals, wordsFileContent, - docsFileContent)), - std::make_unique()}); + key, Context{TypeErasedCleanup{[testIndexBasename]() { + for (const std::string& indexFilename : + getAllIndexFilenames(testIndexBasename)) { + // Don't log when a file can't be deleted, + // because the logging might already be + // destroyed. + ad_utility::deleteFile(indexFilename, false); + } + }}, + std::make_unique(makeTestIndex( + testIndexBasename, turtleInput, loadAllPermutations, + usePatterns, usePrefixCompression, + blocksizePermutations, createTextIndex, + addWordsFromLiterals, contentsOfWordsFileAndDocsFile)), + std::make_unique()}); } auto* qec = contextMap.at(key).qec_.get(); qec->getIndex().getImpl().setGlobalIndexAndComparatorOnlyForTesting(); diff --git a/test/util/IndexTestHelpers.h b/test/util/IndexTestHelpers.h index 6bbe0c9195..cbbd5ea486 100644 --- a/test/util/IndexTestHelpers.h +++ b/test/util/IndexTestHelpers.h @@ -46,8 +46,8 @@ Index makeTestIndex(const std::string& indexBasename, ad_utility::MemorySize blocksizePermutations = 16_B, bool createTextIndex = false, bool addWordsFromLiterals = true, - std::optional wordsFileContent = std::nullopt, - std::optional docsFileContent = std::nullopt); + std::optional> + contentsOfWordsFileAndDocsfile = std::nullopt); // Return a static `QueryExecutionContext` that refers to an index that was // build using `makeTestIndex` (see above). The index (most notably its @@ -59,8 +59,8 @@ QueryExecutionContext* getQec( bool usePrefixCompression = true, ad_utility::MemorySize blocksizePermutations = 16_B, bool createTextIndex = false, bool addWordsFromLiterals = true, - std::optional wordsFileContent = std::nullopt, - std::optional docsFileContent = std::nullopt); + std::optional> + contentsOfWordsFileAndDocsfile = std::nullopt); // Return a lambda that takes a string and converts it into an ID by looking // it up in the vocabulary of `index`. An `AD_CONTRACT_CHECK` will fail if the