Skip to content
Snippets Groups Projects
Commit 5621bd88 authored by Dr. Felix Tobias Schindler's avatar Dr. Felix Tobias Schindler
Browse files

[test.common] rewrote check_for_success()

Success was previously defined as the results being less or equal to the
expected results, which is not optimal. From now on it is checked whether
the expected result and the result (in scientific notation) are equal in
their first three meaningful digits.
parent 37d29b0d
No related branches found
No related tags found
No related merge requests found
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "config.h" #include "config.h"
#include <cmath>
#include <sys/time.h> #include <sys/time.h>
#include <dune/stuff/test/gtest/gtest.h> #include <dune/stuff/test/gtest/gtest.h>
...@@ -26,19 +27,56 @@ void busywait(const int ms) ...@@ -26,19 +27,56 @@ void busywait(const int ms)
namespace Dune { namespace Dune {
namespace Stuff { namespace Stuff {
namespace Test { namespace Test {
namespace internal {
std::pair<size_t, ssize_t> convert_to_scientific(const double number, const size_t precision)
{
// see http://www.mathworks.com/matlabcentral/newsreader/view_thread/151859
const double sign = (number > 0.0) ? 1.0 : -1.0;
const double tmp = std::log10(std::abs(number));
ssize_t exponent = ((tmp > 0) ? 1 : -1) * std::floor(std::abs(tmp));
double coefficient = sign * std::pow(10.0, tmp - exponent);
if (std::abs(coefficient) < 1.0) {
coefficient *= 10.0;
exponent -= 1;
}
const double factor = std::pow(10, precision);
// std::cout << "number = " << number << std::endl;
// std::cout << "exponent = " << exponent << std::endl;
// std::cout << "coefficient = " << coefficient << std::endl;
// std::cout << "scaled = " << size_t(std::floor(factor * coefficient)) << std::endl << std::endl;
return std::make_pair(size_t(std::floor(factor * coefficient)), exponent);
} // ... convert_to_scientific(...)
} // namespace internal
void check_for_success(const Dune::Stuff::Common::ConvergenceStudy& study, void check_for_success(const Dune::Stuff::Common::ConvergenceStudy& study,
const std::map<std::string, std::vector<double>>& errors_map) const std::map<std::string, std::vector<double>>& results_map)
{ {
for (const auto& norm : study.used_norms()) { for (const auto& norm : study.used_norms()) {
const auto expected_results = study.expected_results(norm); const auto expected_results = study.expected_results(norm);
const auto errors_search = errors_map.find(norm); const auto results_search = results_map.find(norm);
EXPECT_NE(errors_search, errors_map.end()) << " norm = " << norm; EXPECT_NE(results_search, results_map.end()) << " norm = " << norm;
const auto& errors = errors_search->second; const auto& actual_results = results_search->second;
EXPECT_LE(errors.size(), expected_results.size()) << " norm = " << norm; EXPECT_LE(actual_results.size(), expected_results.size()) << " norm = " << norm;
for (size_t ii = 0; ii < errors.size(); ++ii) for (size_t ii = 0; ii < actual_results.size(); ++ii) {
EXPECT_LE(errors[ii], expected_results[ii]) << " norm = " << norm << ", level = " << ii; const auto actual_result = internal::convert_to_scientific(actual_results[ii], 2);
const auto expected_result = internal::convert_to_scientific(expected_results[ii], 2);
EXPECT_EQ(actual_result.second, expected_result.second)
<< " Exponent comparison (in scientific notation, precision 2) failed for\n"
<< " norm: " << norm << "\n"
<< " actual_results[" << ii << "] = " << actual_results[ii] << "\n"
<< " expected_results[" << ii << "] = " << expected_results[ii];
EXPECT_EQ(actual_result.first, expected_result.first)
<< " Coefficient comparison (in scientific notation, precision 2) failed for\n"
<< " norm: " << norm << "\n"
<< " actual_results[" << ii << "] = " << actual_results[ii] << "\n"
<< " expected_results[" << ii << "] = " << expected_results[ii];
}
} }
} // ... check_for_success(...) } // ... check_for_success(...)
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include <string> #include <string>
#include <map> #include <map>
#include <vector> #include <vector>
#include <utility>
#include <dune/stuff/common/disable_warnings.hh> #include <dune/stuff/common/disable_warnings.hh>
#include <dune/common/tuples.hh> #include <dune/common/tuples.hh>
...@@ -60,6 +61,13 @@ void busywait(const int ms); ...@@ -60,6 +61,13 @@ void busywait(const int ms);
namespace Dune { namespace Dune {
namespace Stuff { namespace Stuff {
namespace Test { namespace Test {
namespace internal {
std::pair<size_t, ssize_t> convert_to_scientific(const double number, const size_t precision = 2);
} // namespace internal
void check_for_success(const Dune::Stuff::Common::ConvergenceStudy& study, void check_for_success(const Dune::Stuff::Common::ConvergenceStudy& study,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment