brush up :-)

This commit is contained in:
Karsten Laux 2021-12-16 15:29:45 +01:00
parent b8ad76b908
commit 9fbec222a3

View File

@ -40,12 +40,23 @@ void run_wordsearch(ParallelWordSearch& search)
search.run();
}
void check_result(const WordList& haystack, const std::string& pattern, const ParallelWordSearch::ResultList& result)
bool check_result(const WordList& haystack, const std::string& pattern, const ParallelWordSearch::ResultList& result)
{
bool result_is_valid = true;
// check number of matches
const unsigned TESTPATTERN_LENGTH = 4;
unsigned expected_count = static_cast<unsigned>(pow(26.0, double(TESTPATTERN_LENGTH - pattern.size())));
std::cout << "Number of matches expected: " << expected_count << " got: " << result.size() << " --> " << ((result.size() == expected_count) ? "OK" : "ERROR") << std::endl;
std::cout << "number of matches expected: " << expected_count << " got: " << result.size() << " --> ";
if (result.size() == expected_count)
{
std::cout << "OK" << std::endl;
}
else
{
std::cout << "ERROR" << std::endl;
result_is_valid = false;
}
// verify matches contain the search-pattern
unsigned valid_count = 0;
@ -57,11 +68,12 @@ void check_result(const WordList& haystack, const std::string& pattern, const Pa
}
else
{
std::cout << "Found mismatch: " << *itr << std::endl;
std::cout << "found mismatch: " << *itr << std::endl;
result_is_valid = false;
}
}
std::cout << "Verified " << valid_count << " matches --> " << ((result.size() == valid_count) ? "OK" : "ERROR") << std::endl;
std::cout << "verified " << valid_count << " matches --> " << ((result.size() == valid_count) ? "OK" : "ERROR") << std::endl;
}
int main(int argc, char** argv)
@ -83,28 +95,25 @@ int main(int argc, char** argv)
std::cout << std::endl;
time_span = TestRunner(create_testdata, test_data);
std::cout << "created test_data (" << test_data.size() << " words) " << time_span << " seconds" << std::endl;
std::cout << "created test_data (" << test_data.size() << " words) in " << time_span << " seconds" << std::endl;
time_span = TestRunner(shuffle_testdata, test_data);
std::cout << "shuffled test_data " << time_span << " seconds" << std::endl;
std::cout << "shuffled test_data in " << time_span << " seconds" << std::endl;
ParallelWordSearch wordSearch(1, test_data.begin(), test_data.end(), parameters.SearchPattern());
time_span = TestRunner(run_wordsearch, wordSearch);
std::cout << "linear search found " << wordSearch.get_result().size() << " matches: " << time_span << " seconds" << std::endl;
std::cout << "linear search found " << wordSearch.get_result().size() << " matches in " << time_span << " seconds" << std::endl;
ParallelWordSearch parallelWordSearch(parameters.WorkerCount(), test_data.begin(), test_data.end(), parameters.SearchPattern());
time_span = TestRunner(run_wordsearch, parallelWordSearch);
std::cout << "parallel search found " << parallelWordSearch.get_result().size() << " matches: " << time_span << " seconds" << std::endl;
std::cout << "parallel search found " << parallelWordSearch.get_result().size() << " matches in " << time_span << " seconds" << std::endl;
std::cout << std::endl;
std::cout << "checking result of parallel search ..." << std::endl;
bool result_valid = check_result(test_data, parameters.SearchPattern(), parallelWordSearch.get_result());
check_result(test_data, parameters.SearchPattern(), parallelWordSearch.get_result());
std::cout << std::endl;
std::cout << "Done." << std::endl;
std::cout << std::endl << "Result is " << (result_valid ? "VALID" : "INVALID") << std::endl;
std::cout << std::endl;
return 0;