#include <table.h>
#include <Block.h>
#include <math.h>
#include <iostream.h>
#include <assert.h>

LOCAL BOOL closeto(double a, double b)
{ return fabs(a-b)<.0001;}

int errtst(const char *s)
{
 return *s ? 1 : 0;
}

LOCAL double f(double x, const void *p)
{
 return x+*(const double *)p;
}

main()
{
 Block<float> x(3);
 x[0] = 0.0;
 x[1] = 1.0;
 x[2] = 2.0;

 table_c tx(Range<double>(0.0, 2.0), x, TRUE, FALSE, "TX");
 cerr << tx(1.0) << endl;
 assert(closeto(tx(1.0), 1.0));
 cerr << tx(0.5) << endl;
 assert(closeto(tx(0.5), 0.5));
 cerr << tx(2.0) << endl;
 assert(closeto(tx(3.0), 2.0));
 assert(closeto(tx(2.0), 2.0));
 assert(closeto(tx(0.0), 0.0));
 table_c::default_error.appoint(errtst);
 table_c::default_error_return = -1.0;
 tx.error_return = -1.11;
 assert(closeto(tx(-2.0), -1.0));
 tx.error.appoint(errtst);
 assert(closeto(tx(-2.0), -1.11));

 C double extra = 105.4;
 table_c ty(Range<double>(0.5, 0.6), 100, f, &extra, FALSE, FALSE, "TY");
 assert(closeto(ty(0.51), 0.51+extra));
 assert(closeto(ty(0.52), 0.52+extra));

 Block<table_ddoublet> btd(6);
 btd[0] = table_ddoublet(1.0, 2.0);
 btd[1] = table_ddoublet(2.0, 4.0);
 btd[2] = table_ddoublet(3.0, 6.0);
 btd[3] = table_ddoublet(0.0, 0.0);
 btd[4] = table_ddoublet(0.5, 1.0);
 btd[5] = table_ddoublet(1.0, 2.0);
 ntable_c nt(btd, FALSE, FALSE, "NT");
 assert(closeto(nt(0.5), 1.0));
 assert(closeto(nt(0.25), 0.5));
 assert(closeto(nt(2.5), 5.0));
 assert(closeto(nt(2.0), 4.0));
 assert(closeto(nt.valid_range().low, 0.0));
 assert(closeto(nt.valid_range().high, 3.0));

 ntable_c nt5 = nt*5.0;
 assert(closeto(nt5(2.0), 20.0));

 ntable_c ntnt = nt*nt;
 assert(closeto(ntnt(2.0), 16.0));

 exit(0);
}
