2016-02-03 13:57:18 +11:00
|
|
|
#include "roots/bisection.hpp"
|
|
|
|
|
|
|
|
#include "tap.hpp"
|
|
|
|
|
|
|
|
#include "maths.hpp"
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
using cruft::pow;
|
2016-02-03 13:57:18 +11:00
|
|
|
|
|
|
|
|
|
|
|
constexpr float order2 (float x) { return x * x + 3 * x - 7.f; }
|
2018-08-05 14:42:02 +10:00
|
|
|
constexpr float order4 (float x) { return 10 * cruft::pow (x, 4u)
|
|
|
|
-270 * cruft::pow (x, 2u)
|
|
|
|
-140 * cruft::pow (x, 1u)
|
2016-02-03 13:57:18 +11:00
|
|
|
+1200; }
|
|
|
|
|
|
|
|
struct {
|
|
|
|
float (*func) (float);
|
|
|
|
float lo;
|
|
|
|
float hi;
|
|
|
|
float root;
|
|
|
|
const char *msg;
|
|
|
|
} TESTS[] = {
|
|
|
|
{ order2, 0.f, 3.f, 0.5f * (std::sqrt (37.f) - 3.f), "order-2 bisection" },
|
|
|
|
{ order4, 0.f, 5.f, 2.f, "order-4 bisection" }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
main (void)
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::TAP::logger tap;
|
2016-02-03 13:57:18 +11:00
|
|
|
|
|
|
|
for (const auto &t: TESTS) {
|
|
|
|
constexpr float TOLERANCE = 0.00001f;
|
2018-08-05 14:42:02 +10:00
|
|
|
auto root = cruft::roots::bisection (t.lo, t.hi, t.func, TOLERANCE);
|
2016-07-28 13:36:23 +10:00
|
|
|
tap.expect_eq (root, t.root, "%s", t.msg);
|
2016-02-03 13:57:18 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
return tap.status ();
|
|
|
|
}
|