Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
JRange.cc File Reference

Example program to test JTOOLS::JRange operations. More...

#include <string>
#include <iostream>
#include <iomanip>
#include "JTools/JRange.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

Example program to test JTOOLS::JRange operations.

Author
mdejong

Definition in file JRange.cc.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 45 of file JRange.cc.

46 {
47  using namespace std;
48 
49  int debug;
50 
51  try {
52 
53  JParser<> zap("Example program to test range operations.");
54 
55  zap['d'] = make_field(debug) = 3;
56 
57  zap(argc, argv);
58  }
59  catch(const exception &error) {
60  FATAL(error.what() << endl);
61  }
62 
63 
64  typedef JTOOLS::JRange<double> JRange_t;
65 
66  const double xmin = -1.0;
67  const double xmax = +1.0;
68  const double dx = (xmax - xmin) / 10;
69 
70  const int WIDTH = 12;
71 
72  {
73  JRange_t A(JRange_t::DEFAULT_RANGE);
74  JRange_t B(xmin, xmax);
75 
76  for (double x = xmin; x < xmax + 0.5 * dx; x += dx) {
77 
78  DEBUG(setw(WIDTH) << left << "inside" << ' ' << B << ' ' << setw(5) << x << ' ' << (B(x) ? "Y" : "N") << endl);
79 
80  ASSERT(B(x) == (x >= xmin && x <= xmax));
81 
82  A.include(x);
83  }
84 
85  DEBUG(setw(WIDTH) << left << "range" << ' ' << A << endl);
86 
87  ASSERT(A == B);
88  }
89 
90 
91  {
92  const double x1 = -1.0;
93  const double x2 = +0.5;
94  const double x3 = -0.5;
95  const double x4 = +1.0;
96 
97  JRange_t A(x1, x2);
98  JRange_t B(x3, x4);
99 
100  DEBUG(setw(WIDTH) << left << "join" << ' ' << A << ' ' << B << " = " << join(A,B) << endl);
101 
102  ASSERT(join(A,B) == JRange_t(x3,x2));
103  }
104 
105  {
106  const double x1 = -1.0;
107  const double x2 = -0.5;
108  const double x3 = +0.5;
109  const double x4 = +1.0;
110 
111  JRange_t A(x1, x2);
112  JRange_t B(x3, x4);
113 
114  DEBUG(setw(WIDTH) << left << "combine" << ' ' << A << ' ' << B << " = " << combine(A,B) << endl);
115 
116  ASSERT(combine(A,B) == JRange_t(x1,x4));
117  }
118 
119  {
120  const double x1 = -1.0;
121  const double x2 = -0.5;
122  const double x3 = +0.5;
123  const double x4 = +1.0;
124 
125  JRange_t A(x1, x2);
126  JRange_t B(x3, x4);
127 
128  DEBUG(setw(WIDTH) << left << "add" << ' ' << A << ' ' << B << " = " << A + B << endl);
129 
130  ASSERT((A + B) == JRange_t(x1+x3,x2+x4));
131  }
132 
133  {
134  struct __A__ {
135  __A__() : value(0.0) {}
136  __A__(const double value) : value(value) {}
137 
138  double get() const { return value; }
139 
140  double value;
141  };
142 
143  vector<__A__> buffer;
144 
145  for (double x = xmin; x < xmax + 0.5 * dx; x += dx) {
146  buffer.push_back(x);
147  }
148 
149  JRange_t A;
150  JRange_t B;
151 
152  A.setRange(buffer.begin(), buffer.end(), &__A__::value);
153  B.setRange(buffer.begin(), buffer.end(), &__A__::get);
154 
155  DEBUG(setw(WIDTH) << left << "setRange" << ' ' << A << endl);
156  DEBUG(setw(WIDTH) << left << "setRange" << ' ' << B << endl);
157 
158  ASSERT(A == JRange_t(xmin,xmax));
159  ASSERT(B == JRange_t(xmin,xmax));
160  }
161 
162  return 0;
163 }
Auxiliary data structure for alignment of data.
Definition: JPrint.hh:248
Utility class to parse command line options.
Definition: JParser.hh:1410
JRange< T, JComparator_t > join(const JRange< T, JComparator_t > &first, const JRange< T, JComparator_t > &second)
Join ranges.
Definition: JRange.hh:573
#define ASSERT(A)
Assert macro.
Definition: JMessage.hh:72
T get(const JHead &head)
Get object from header.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1836
int debug
debug level
Definition: JSirene.cc:59
JRange< T, JComparator_t > combine(const JRange< T, JComparator_t > &first, const JRange< T, JComparator_t > &second)
Combine ranges.
Definition: JRange.hh:590
#define FATAL(A)
Definition: JMessage.hh:65
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:60