refactor: extract finder-test template method

mandlm 2024-03-20 21:00:47 +01:00
parent ad8a9ada83
commit 076969f393
Signed by: mandlm
GPG Key ID: 4AA25D647AA54CC7
1 changed files with 16 additions and 62 deletions

View File

@ -35,77 +35,31 @@ void test_finder_search(Finder &finder, std::string_view name,
<< " element(s) long" << endl;
}
void test_finder(Finder &finder, std::string_view name) {
template <typename FINDER>
void test_finder(const vector<string> &word_list,
std::string_view finder_name) {
cout << "\nrunning " << 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<string> &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<string> &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<string> &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<string> &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<string> &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<LinearFinder>(word_list, "linear finder");
test_finder<SortedLinearFinder>(word_list, "sorted linear finder");
test_finder<ParallelFinder>(word_list, "parallel finder");
test_finder<TreeFinder>(word_list, "tree finder");
test_finder<GroupedFinder>(word_list, "grouped finder");
return EXIT_SUCCESS;
}