refactor: extract finder-test template method

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

View File

@ -12,7 +12,7 @@
using std::string, std::string_view, std::vector, std::cout, std::endl;
vector<string> 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<string> 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 <typename FINDER>
void test_finder(const vector<string> &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<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;
}