From 9fbec222a3bce8ae9f779d8f855197818d8c7ec4 Mon Sep 17 00:00:00 2001 From: karsten Date: Thu, 16 Dec 2021 15:29:45 +0100 Subject: [PATCH] brush up :-) --- main.cpp | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/main.cpp b/main.cpp index dfa95d6..a349741 100644 --- a/main.cpp +++ b/main.cpp @@ -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(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;