#include #include #include #include "JTools/JRange.hh" #include "Jeep/JParser.hh" #include "Jeep/JMessage.hh" namespace { /** * Write range to output. * * \param out output stream * \param range range * \return output stream */ inline std::ostream& operator<<(std::ostream& out, const JTOOLS::JRange& range) { using namespace std; out << fixed << right << showpos << setprecision(1) << "[" << setw(5) << range.first << "," << setw(5) << range.second << "]"; return out; } } /** * \file * * Example program to test JTOOLS::JRange operations. * \author mdejong */ int main(int argc, char **argv) { using namespace std; using namespace JPP; int debug; try { JParser<> zap("Example program to test range operations."); zap['d'] = make_field(debug) = 3; zap(argc, argv); } catch(const exception &error) { FATAL(error.what() << endl); } const double xmin = -1.0; const double xmax = +1.0; const double dx = (xmax - xmin) / 10; const int WIDTH = 12; { typedef JRange JRange_t; JRange_t A(JRange_t::DEFAULT_RANGE()); JRange_t B(xmin, xmax); for (double x = xmin; x < xmax + 0.5 * dx; x += dx) { DEBUG(setw(WIDTH) << left << "inside" << ' ' << B << ' ' << setw(5) << x << ' ' << (B(x) ? "Y" : "N") << endl); ASSERT(B(x) == (x >= xmin && x <= xmax)); A.include(x); } DEBUG(setw(WIDTH) << left << "range" << ' ' << A << endl); ASSERT(A == B); for (double x = xmin - (xmax - xmin); x < xmax + (xmax - xmin) + 0.5 * dx; x += dx) { double x1 = x; while (x1 <= xmin) { x1 += (xmax - xmin); } while (x1 > xmax) { x1 -= (xmax - xmin); } ASSERT(x1 == B.mod(x)); } } { typedef JRange JRange_t; const double x1 = -1.0; const double x2 = +0.5; const double x3 = -0.5; const double x4 = +1.0; JRange_t A(x1, x2); JRange_t B(x3, x4); DEBUG(setw(WIDTH) << left << "join" << ' ' << A << ' ' << B << " = " << join(A,B) << endl); ASSERT(overlap(A,B)); ASSERT(join(A,B) == JRange_t(x3,x2)); } { typedef JRange JRange_t; const double x1 = -1.0; const double x2 = -0.5; const double x3 = +0.5; const double x4 = +1.0; JRange_t A(x1, x2); JRange_t B(x3, x4); DEBUG(setw(WIDTH) << left << "combine" << ' ' << A << ' ' << B << " = " << combine(A,B) << endl); ASSERT(combine(A,B) == JRange_t(x1,x4)); } { typedef JRange JRange_t; const double x1 = -1.0; const double x2 = -0.5; const double x3 = +0.5; const double x4 = +1.0; JRange_t A(x1, x2); JRange_t B(x3, x4); DEBUG(setw(WIDTH) << left << "add" << ' ' << A << ' ' << B << " = " << A + B << endl); ASSERT((A + B) == JRange_t(x1+x3,x2+x4)); } { typedef JRange JRange_t; struct __A__ { __A__() : value(0.0) {} __A__(const double value) : value(value) {} double get() const { return value; } double value; }; vector<__A__> buffer; for (double x = xmin; x < xmax + 0.5 * dx; x += dx) { buffer.push_back(x); } JRange_t A; JRange_t B; A.setRange(make_array(buffer.begin(), buffer.end(), &__A__::value)); B.setRange(make_array(buffer.begin(), buffer.end(), &__A__::get)); DEBUG(setw(WIDTH) << left << "setRange" << ' ' << A << endl); DEBUG(setw(WIDTH) << left << "setRange" << ' ' << B << endl); ASSERT(A == JRange_t(xmin,xmax)); ASSERT(B == JRange_t(xmin,xmax)); } { typedef JRange JRange_t; JRange_t A(1,100); JRange_t B(A.getLowerLimit(), A.getLowerLimit()); JRange_t C(A.getUpperLimit(), A.getUpperLimit()); ASSERT(overlap(A,B)); ASSERT(overlap(A,C)); ASSERT(!overlap(B,C)); } return 0; }