Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
JPredicate.cc
Go to the documentation of this file.
1
2#include <iostream>
3#include <iomanip>
4#include <vector>
5#include <algorithm>
6#include <iterator>
7
8#include "JLang/JPredicate.hh"
10
11#include "Jeep/JParser.hh"
12#include "Jeep/JMessage.hh"
13
14
15namespace {
16
17 class __A__ {
18 public:
19 __A__(const int a)
20 {
21 this->a = a;
22 }
23
24 int get() const
25 {
26 return a;
27 }
28
29 friend inline std::ostream& operator<<(std::ostream& out, const __A__& object) { return out << object.a; }
30
31 int a;
32 };
33
34
35 template<class T>
36 void print(std::ostream& out, int debug, const char* title, T __begin, T __end)
37 {
38 using namespace std;
39 using namespace JPP;
40
41 if (debug >= debug_t) {
42 out << title << endl;
43 copy(__begin, __end, ostream_iterator<typename T::value_type>(out, " "));
44 out << endl;
45 }
46 }
47}
48
49
50/**
51 * \file
52 *
53 * Auxiliary program to test JLANG::JPredicate class and helper methods.
54 * \author mdejong
55 */
56int main(int argc, char **argv)
57{
58 using namespace std;
59 using namespace JPP;
60
61 int debug;
62
63 try {
64
65 JParser<> zap("Auxiliary program to test object selection methods.");
66
67 zap['d'] = make_field(debug) = 3;
68
69 zap(argc, argv);
70 }
71 catch(const exception &error) {
72 FATAL(error.what() << endl);
73 }
74
75 vector<__A__> buffer;
76
77 buffer.push_back(__A__(3));
78 buffer.push_back(__A__(4));
79 buffer.push_back(__A__(2));
80 buffer.push_back(__A__(1));
81 buffer.push_back(__A__(5));
82
83
84 print(cout, debug, "data:", buffer.begin(), buffer.end());
85
86 int x = 3;
87
88 {
89 vector<__A__>::const_iterator p = find_if(buffer.begin(), buffer.end(), make_predicate(&__A__::a, x));
90
91 DEBUG("has " << x << "? " << (p != buffer.end() ? "yes" : "no") << endl);
92
93 ASSERT(p != buffer.end() && p->a == x);
94 }
95
96 {
97 vector<__A__>::const_iterator p = find_if(buffer.begin(), buffer.end(), make_predicate(&__A__::get, x));
98
99 DEBUG("has " << x << "? " << (p != buffer.end() ? "yes" : "no") << endl);
100
101 ASSERT(p != buffer.end() && p->get() == x);
102 }
103
104 {
105 vector<__A__>::iterator p = partition(buffer.begin(), buffer.end(), make_predicate(&__A__::a, x, JComparison::lt()));
106
107 print(cout, debug, "selection:", buffer.begin(), p);
108
109 for (vector<__A__>::iterator i = buffer.begin(); i != p; ++i) {
110 ASSERT(i->a < x);
111 }
112 }
113
114 {
115 ASSERT(count_if(buffer.begin(), buffer.end(), make_predicate(&__A__::a, 1, JComparison::eq())) == 1);
116 ASSERT(count_if(buffer.begin(), buffer.end(), make_predicate(&__A__::a, 2, JComparison::gt())) == 3);
117 }
118
119 return 0;
120}
General purpose messaging.
#define DEBUG(A)
Message macros.
Definition JMessage.hh:62
#define ASSERT(A,...)
Assert macro.
Definition JMessage.hh:90
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:72
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2142
int main(int argc, char **argv)
Definition JPredicate.cc:56
void print(const TH1 &h1, std::ostream &out)
Print histogram parameters.
Utility class to parse command line options.
Definition JParser.hh:1698
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
const double a
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition JHead.cc:163
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).