#include #include #include #include #include #include "JLang/JComparator.hh" #include "JLang/JComparison.hh" #include "Jeep/JParser.hh" #include "Jeep/JMessage.hh" namespace { class __A__ { public: __A__(const double a) { this->a = a; } double get() const { return -a; } friend inline std::ostream& operator<<(std::ostream& out, const __A__& object) { return out << object.a; } double a; }; template void print(std::ostream& out, int debug, const char* title, T __begin, T __end) { using namespace std; using namespace JPP; if (debug >= debug_t) { out << title << endl; copy(__begin, __end, ostream_iterator(out, " ")); out << endl; } } } /** * \file * * Auxiliary program to test JLANG::JComparator class and helper methods. * \author mdejong */ int main(int argc, char **argv) { using namespace std; using namespace JPP; int debug; try { JParser<> zap("Auxiliary program to test object comparison methods."); zap['d'] = make_field(debug) = 3; zap(argc, argv); } catch(const exception &error) { FATAL(error.what() << endl); } typedef vector<__A__> buffer_type; buffer_type buffer; buffer.push_back(__A__(3.0)); buffer.push_back(__A__(4.0)); buffer.push_back(__A__(2.0)); buffer.push_back(__A__(1.0)); buffer.push_back(__A__(5.0)); ASSERT(buffer.size() >= 2); print(cout, debug, "data", buffer.begin(), buffer.end()); sort(buffer.begin(), buffer.end(), make_comparator(&__A__::get)); print(cout, debug, "sort(.., make_comparator(&__A__::get))", buffer.begin(), buffer.end()); for (buffer_type::const_iterator q = buffer.begin(), p = q++; q != buffer.end(); ++p, ++q) { ASSERT(p->get() < q->get()); } sort(buffer.begin(), buffer.end(), make_comparator(&__A__::get, JComparison::gt())); print(cout, debug, "sort(.., make_comparator(&__A__::get, JComparison::gt()))", buffer.begin(), buffer.end()); for (buffer_type::const_iterator q = buffer.begin(), p = q++; q != buffer.end(); ++p, ++q) { ASSERT(p->get() > q->get()); } sort(buffer.begin(), buffer.end(), make_comparator(&__A__::a)); print(cout, debug, "sort(.., make_comparator(&__A__::a))", buffer.begin(), buffer.end()); for (buffer_type::const_iterator q = buffer.begin(), p = q++; q != buffer.end(); ++p, ++q) { ASSERT(p->a < q->a); } sort(buffer.begin(), buffer.end(), make_comparator(&__A__::a, JComparison::gt())); print(cout, debug, "sort(.., make_comparator(&__A__::a), JComparison::gt())", buffer.begin(), buffer.end()); for (buffer_type::const_iterator q = buffer.begin(), p = q++; q != buffer.end(); ++p, ++q) { ASSERT(p->a > q->a); } return 0; }