refactor: extract test runs

This commit is contained in:
mandlm 2024-03-20 16:37:00 +01:00
parent 21ca48b9c2
commit 6ba52ab73f
Signed by: mandlm
GPG Key ID: 4AA25D647AA54CC7

View File

@ -1,3 +1,4 @@
#include "grouped_finder.h"
#include "linear_finder.h" #include "linear_finder.h"
#include "parallel_finder.h" #include "parallel_finder.h"
#include "timer.h" #include "timer.h"
@ -6,10 +7,8 @@
#include <cstdlib> #include <cstdlib>
#include <iostream> #include <iostream>
#include <thread>
using std::string, std::string_view, std::vector, std::thread, std::cout, using std::string, std::string_view, std::vector, std::cout, std::endl;
std::endl;
vector<string> generate_word_list() { vector<string> generate_word_list() {
cout << "\ngenerating word list" << endl; cout << "\ngenerating word list" << endl;
@ -24,36 +23,37 @@ vector<string> generate_word_list() {
return word_list; return word_list;
} }
void test_finder_search(Finder &finder, std::string_view name,
std::string_view search_term) {
Timer find_timer;
auto result = finder.find_prefix(search_term);
find_timer.stop();
cout << name << "(" << search_term << ") took " << find_timer << endl;
cout << "result list is " << std::distance(result.cbegin(), result.cend())
<< " element(s) long" << endl;
}
void test_finder(Finder &finder, std::string_view name) {
for (const auto &search_term : {"A", "AB", "ABC", "ABCD"}) {
test_finder_search(finder, name, search_term);
}
}
void test_linear_finder(const vector<string> &word_list) { void test_linear_finder(const vector<string> &word_list) {
cout << "\nrunning linear finder" << endl; cout << "\nrunning linear finder" << endl;
LinearFinder linear_finder(word_list); LinearFinder linear_finder(word_list);
Timer find_timer; test_finder(linear_finder, "linear finder");
auto result = linear_finder.find_prefix("ABCD");
find_timer.stop();
cout << "linear finder took " << find_timer << endl;
cout << "result list is " << std::distance(result.cbegin(), result.cend())
<< " element(s) long" << endl;
} }
void test_parallel_finder(const vector<string> &word_list) { void test_parallel_finder(const vector<string> &word_list) {
cout << "\nrunning parallel finder" << endl; cout << "\nrunning parallel finder" << endl;
const size_t thread_count = thread::hardware_concurrency(); ParallelFinder parallel_finder(word_list);
cout << "using " << thread_count << " threads" << endl; test_finder(parallel_finder, "parallel finder");
ParallelFinder parallel_finder(word_list, thread_count);
Timer find_timer;
auto result = parallel_finder.find_prefix("ABCD");
find_timer.stop();
cout << "parallel finder took " << find_timer << endl;
cout << "result list is " << std::distance(result.cbegin(), result.cend())
<< " element(s) long" << endl;
} }
void test_tree_finder(const vector<string> &word_list) { void test_tree_finder(const vector<string> &word_list) {
@ -62,16 +62,20 @@ void test_tree_finder(const vector<string> &word_list) {
Timer constructor_timer; Timer constructor_timer;
TreeFinder tree_finder(word_list); TreeFinder tree_finder(word_list);
constructor_timer.stop(); constructor_timer.stop();
cout << "tree finder constructor took " << constructor_timer << endl; cout << "tree finder constructor took " << constructor_timer << endl;
Timer find_timer; test_finder(tree_finder, "tree finder");
auto result = tree_finder.find_prefix("ABCD"); }
find_timer.stop();
cout << "tree finder took " << find_timer << endl; void test_grouped_finder(const vector<string> &word_list) {
cout << "result list is " << std::distance(result.cbegin(), result.cend()) cout << "\nrunning grouped finder" << endl;
<< " element(s) long" << 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[]) { int main(int argc, char *argv[]) {
@ -82,6 +86,7 @@ int main(int argc, char *argv[]) {
test_linear_finder(word_list); test_linear_finder(word_list);
test_parallel_finder(word_list); test_parallel_finder(word_list);
test_tree_finder(word_list); test_tree_finder(word_list);
test_grouped_finder(word_list);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }