2016-02-03 13:57:18 +11:00
|
|
|
#include "roots/bisection.hpp"
|
|
|
|
|
|
|
|
#include "tap.hpp"
|
|
|
|
|
|
|
|
#include "maths.hpp"
|
|
|
|
|
|
|
|
using util::pow;
|
|
|
|
|
|
|
|
|
|
|
|
constexpr float order2 (float x) { return x * x + 3 * x - 7.f; }
|
2018-01-01 15:45:56 +11:00
|
|
|
constexpr float order4 (float x) { return 10 * util::pow (x, 4u)
|
|
|
|
-270 * util::pow (x, 2u)
|
|
|
|
-140 * util::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)
|
|
|
|
{
|
|
|
|
util::TAP::logger tap;
|
|
|
|
|
|
|
|
for (const auto &t: TESTS) {
|
|
|
|
constexpr float TOLERANCE = 0.00001f;
|
|
|
|
auto root = util::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 ();
|
|
|
|
}
|