CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
testBug90848.cc
Go to the documentation of this file.
1// test Bug #90848 in CLHEP::Evaluator
2// Author: Tom Roberts
3
4#include <stdlib.h>
5#include <stdio.h>
6#include <cmath>
7#include <limits>
8
9#include "CLHEP/Evaluator/Evaluator.h"
11public:
13 bool isOK() { return status() == OK; }
14 double evaluate(const char *e) {
15 double v = HepTool::Evaluator::evaluate(e);
16 if(status() != OK) v = std::numeric_limits<double>::quiet_NaN();
17 return v;
18 }
19};
20
21struct Test {
22 const char *expr;
23 double value1; // x=1, y=2, z=3
24 double value2; // x=10, y=-20 z=-30
25};
26
28 { "x", 1.0, 10.0},
29 { "y", 2.0, -20.0},
30 { "z", 3.0, -30.0},
31 { "x+y", 3.0, -10.0},
32 { "-z", -3.0, 30.0},
33 { "0-z", -3.0, 30.0},
34 { "0 - -z", 3.0, -30.0},
35 { "10/-x", -10.0, -1.0},
36 { "exp(-y)*exp(y)", 1.0, 1.0},
37 { "exp(-y^2)*exp(y^2)", 1.0, 1.0},
38 { "-5*x", -5.0, -50.0},
39 { "5+-x", 4.0, -5.0},
40 { "5+(-x)", 4.0, -5.0},
41 { "5*-x", -5.0, -50.0},
42 { "5*(-x)", -5.0, -50.0},
43 { "-z^2", -9.0, -900.0},
44 { "0-z^2", -9.0, -900.0},
45 { "exp(-0.2*z^2)*exp(0.2*z^2)", 1.0, 1.0},
46 { "exp(0.2*-z^2)*exp(0.2*z^2)", 1.0, 1.0},
47 { "exp(-z^2*0.2)*exp(0.2*z^2)", 1.0, 1.0},
48 { "exp(0.2*-(z^2))*exp(0.2*z^2)", 1.0, 1.0},
49 { "exp(-(z^2)*0.2)*exp(0.2*z^2)", 1.0, 1.0},
50 { "exp(-0.2*z)*exp(0.2*z)", 1.0, 1.0},
51 { "exp(0.2*-z)*exp(0.2*z)", 1.0, 1.0},
52 { "exp(-z*0.2)*exp(0.2*z)", 1.0, 1.0},
53};
54
55int main() {
57
58 e.setVariable("x",1.0);
59 e.setVariable("y",2.0);
60 e.setVariable("z",3.0);
61//printf("x=1.0 y=2.0 z=3.0\n");
62 int err=0;
63 for(unsigned i=0; i<sizeof(tests)/sizeof(Test); ++i) {
64 double v=e.evaluate(tests[i].expr);
65 if(std::isnan(v) || std::fabs(v-tests[i].value1) > 1E-12 || !e.isOK()) {
66 printf("%s = %.6f should be %.6f\n",tests[i].expr,
67 e.evaluate(tests[i].expr),tests[i].value1);
68 err = 1;
69 }
70 }
71
72 e.setVariable("x",10.0);
73 e.setVariable("y",-20.0);
74 e.setVariable("z",-30.0);
75//printf("x=10.0 y=-20.0 z=-30.0\n");
76 for(unsigned i=0; i<sizeof(tests)/sizeof(Test); ++i) {
77 double v=e.evaluate(tests[i].expr);
78 if(std::isnan(v) || std::fabs(v-tests[i].value2) > 1E-12 || !e.isOK()) {
79 printf("%s = %.6f should be %.6f\n",tests[i].expr,
80 e.evaluate(tests[i].expr),tests[i].value2);
81 err = 1;
82 }
83 }
84
85 double v=e.evaluate("unknown(0.0)");
86 if(!std::isnan(v) || e.isOK()) {
87 printf("%s succeeded\n","unknown(0.0)");
88 err=1;
89 }
90
91 v = e.evaluate("unknown+0.0");
92 if(!std::isnan(v) || e.isOK()) {
93 printf("%s succeeded\n","unknown+0.0");
94 err=1;
95 }
96
97 exit(err);
98}
double evaluate(const char *expression)
Definition: Evaluator.cc:637
void setVariable(const char *name, double value)
Definition: Evaluator.cc:713
int status() const
Definition: Evaluator.cc:657
double evaluate(const char *e)
Definition: testBug90848.cc:14
#define exit(x)
const char * expr
Definition: testBug90848.cc:22
double value1
Definition: testBug90848.cc:23
double value2
Definition: testBug90848.cc:24
Test tests[]
Definition: testBug90848.cc:27
int main()
Definition: testBug90848.cc:55