summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@gmail.com>2010-10-06 18:50:35 -0400
committerTavian Barnes <tavianator@gmail.com>2010-10-06 18:50:35 -0400
commitdaf5e150da756deb950023d61f0643dac875c941 (patch)
tree5e92e24ea0dd340d03cde82b739e22cd5ef3064e
parent39617c503faf08de1324516adc8ebb7150c218ca (diff)
downloadvz-daf5e150da756deb950023d61f0643dac875c941.tar.xz
Make tests test for accuracy.
-rw-r--r--tests/Euler.cpp18
-rw-r--r--tests/Midpoint.cpp20
-rw-r--r--tests/RK4.cpp20
3 files changed, 47 insertions, 11 deletions
diff --git a/tests/Euler.cpp b/tests/Euler.cpp
index 5666cd8..0a6e28e 100644
--- a/tests/Euler.cpp
+++ b/tests/Euler.cpp
@@ -2,6 +2,7 @@
#include <cmath>
#include <cstdlib>
#include <iostream>
+#include <iomanip>
// y' = y (y == C*exp(t))
double
@@ -18,8 +19,19 @@ main()
integrator.integrate(2.0);
- std::cout << integrator.y() << std::endl
- << std::exp(2.0) << std::endl;
+ double actual = integrator.y();
+ double expected = std::exp(2.0);
- return EXIT_SUCCESS;
+ std::cout << std::setprecision(10)
+ << "Numerical: " << actual << std::endl
+ << "Expected: " << expected << std::endl;
+
+ double error = std::fabs(expected - actual)/expected;
+ if (error > 0.01) {
+ std::cerr << "Error: " << 100.0*error << "%" << std::endl;
+ return EXIT_FAILURE;
+ } else {
+ std::cout << "Error: " << 100.0*error << "%" << std::endl;
+ return EXIT_SUCCESS;
+ }
}
diff --git a/tests/Midpoint.cpp b/tests/Midpoint.cpp
index aea1885..9be7bb7 100644
--- a/tests/Midpoint.cpp
+++ b/tests/Midpoint.cpp
@@ -2,6 +2,7 @@
#include <cmath>
#include <cstdlib>
#include <iostream>
+#include <iomanip>
// y' = y (y == C*exp(t))
double
@@ -14,12 +15,23 @@ int
main()
{
vZ::MidpointIntegrator integrator(f);
- integrator.y(1.0).x(0.0).h(0.02);
+ integrator.y(1.0).x(0.0).h(0.01);
integrator.integrate(2.0);
- std::cout << integrator.y() << std::endl
- << std::exp(2.0) << std::endl;
+ double actual = integrator.y();
+ double expected = std::exp(2.0);
- return EXIT_SUCCESS;
+ std::cout << std::setprecision(10)
+ << "Numerical: " << actual << std::endl
+ << "Expected: " << expected << std::endl;
+
+ double error = std::fabs(expected - actual)/expected;
+ if (error > 3.4e-5) {
+ std::cerr << "Error: " << 100.0*error << "%" << std::endl;
+ return EXIT_FAILURE;
+ } else {
+ std::cout << "Error: " << 100.0*error << "%" << std::endl;
+ return EXIT_SUCCESS;
+ }
}
diff --git a/tests/RK4.cpp b/tests/RK4.cpp
index 8e0ca0a..7165c76 100644
--- a/tests/RK4.cpp
+++ b/tests/RK4.cpp
@@ -2,6 +2,7 @@
#include <cmath>
#include <cstdlib>
#include <iostream>
+#include <iomanip>
// y' = y (y == C*exp(t))
double
@@ -14,12 +15,23 @@ int
main()
{
vZ::RK4Integrator integrator(f);
- integrator.y(1.0).x(0.0).h(0.04);
+ integrator.y(1.0).x(0.0).h(0.01);
integrator.integrate(2.0);
- std::cout << integrator.y() << std::endl
- << std::exp(2.0) << std::endl;
+ double actual = integrator.y();
+ double expected = std::exp(2.0);
- return EXIT_SUCCESS;
+ std::cout << std::setprecision(10)
+ << "Numerical: " << actual << std::endl
+ << "Expected: " << expected << std::endl;
+
+ double error = std::fabs(expected - actual)/expected;
+ if (error > 1.7e-10) {
+ std::cerr << "Error: " << 100.0*error << "%" << std::endl;
+ return EXIT_FAILURE;
+ } else {
+ std::cout << "Error: " << 100.0*error << "%" << std::endl;
+ return EXIT_SUCCESS;
+ }
}