From aac589d7e1893413be0c5d86993f88a1ebac4679 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Wed, 20 Mar 2024 21:00:47 +0100 Subject: [PATCH] refactor: extract finder-test template method --- vector_search_cli/main.cpp | 83 +++++++++----------------------------- 1 file changed, 18 insertions(+), 65 deletions(-) diff --git a/vector_search_cli/main.cpp b/vector_search_cli/main.cpp index f2ad6cf..7b10701 100644 --- a/vector_search_cli/main.cpp +++ b/vector_search_cli/main.cpp @@ -12,7 +12,7 @@ using std::string, std::string_view, std::vector, std::cout, std::endl; vector generate_word_list(size_t size_factor = 1) { - cout << "\ngenerating word list (" << size_factor << "x)" << endl; + cout << endl << "generating word list (" << size_factor << "x)" << endl; Timer generator_timer; auto word_list = WordListGenerator::generate(size_factor); @@ -24,8 +24,8 @@ vector generate_word_list(size_t size_factor = 1) { return word_list; } -void test_finder_search(Finder &finder, std::string_view name, - std::string_view search_term) { +void test_finder_search(Finder &finder, string_view name, + string_view search_term) { Timer find_timer; auto result = finder.find_prefix(search_term); find_timer.stop(); @@ -35,77 +35,30 @@ void test_finder_search(Finder &finder, std::string_view name, << " element(s) long" << endl; } -void test_finder(Finder &finder, std::string_view name) { +template +void test_finder(const vector &word_list, string_view finder_name) { + cout << endl << "running " << finder_name << endl; + + Timer constructor_timer; + FINDER finder(word_list); + constructor_timer.stop(); + cout << finder_name << " constructor took " << constructor_timer << endl; + for (const auto &search_term : {"A", "AB", "ABC", "ABCD"}) { - test_finder_search(finder, name, search_term); + test_finder_search(finder, finder_name, search_term); } } -void test_linear_finder(const vector &word_list) { - cout << "\nrunning linear finder" << endl; - - Timer constructor_timer; - LinearFinder linear_finder(word_list); - constructor_timer.stop(); - cout << "linear finder constructor took " << constructor_timer << endl; - - test_finder(linear_finder, "linear finder"); -} - -void test_sorted_linear_finder(const vector &word_list) { - cout << "\nrunning sorted linear finder" << endl; - - Timer constructor_timer; - SortedLinearFinder sorted_linear_finder(word_list); - constructor_timer.stop(); - cout << "sorted linear finder constructor took " << constructor_timer << endl; - - test_finder(sorted_linear_finder, "sorted linear finder"); -} - -void test_parallel_finder(const vector &word_list) { - cout << "\nrunning parallel finder" << endl; - - Timer constructor_timer; - ParallelFinder parallel_finder(word_list); - constructor_timer.stop(); - cout << "linear finder constructor took " << constructor_timer << endl; - - test_finder(parallel_finder, "parallel finder"); -} - -void test_tree_finder(const vector &word_list) { - cout << "\nrunning tree finder" << endl; - - Timer constructor_timer; - TreeFinder tree_finder(word_list); - constructor_timer.stop(); - cout << "tree finder constructor took " << constructor_timer << endl; - - test_finder(tree_finder, "tree finder"); -} - -void test_grouped_finder(const vector &word_list) { - cout << "\nrunning grouped finder" << endl; - - Timer constructor_timer; - GroupedFinder grouped_finder(word_list); - constructor_timer.stop(); - cout << "grouped finder constructor took " << constructor_timer << endl; - - test_finder(grouped_finder, "grouped_finder"); -} - int main(int argc, char *argv[]) { cout << "\n== VectorSearch ==" << endl; auto word_list = generate_word_list(5); - test_linear_finder(word_list); - test_sorted_linear_finder(word_list); - test_parallel_finder(word_list); - test_tree_finder(word_list); - test_grouped_finder(word_list); + test_finder(word_list, "linear finder"); + test_finder(word_list, "sorted linear finder"); + test_finder(word_list, "parallel finder"); + test_finder(word_list, "tree finder"); + test_finder(word_list, "grouped finder"); return EXIT_SUCCESS; }