#!/bin/bash # special format test so it can be used in the Makefile after building CLHEP if test "$G4BL_DIR" then : elseif type g4bl-config >/dev/null 2>&1 G4BL_DIR=`g4bl-config --dir` else G4BL_DIR=`dirname "$0"` G4BL_DIR=${G4BL_DIR%/test} G4BL_DIR=${G4BL_DIR%/source} fi # need G4beamline source to be built, at least CLHEP if test ! -r $G4BL_DIR/.config -o ! -r $G4BL_DIR/lib/libCLHEP.a then echo test40: BLEvaluator - OMITTED exit 0 fi echo "test40: BLEvaluator (1 sec)" source $G4BL_DIR/.config TMP=/tmp/test40.$$ trap "rm -f $TMP*" 0 cat <$TMP.cc // test40.cc - test BLEvaluator or CLHEP::Evaluator //#define TEST_CLHEP // test the bare CLHEP Evaluator #define TEST_BLEVALUATOR // test BLEvaluator #include #include #include #ifdef TEST_CLHEP #include "CLHEP/Evaluator/Evaluator.h" class BLEvaluator : public HepTool::Evaluator { public: BLEvaluator() : HepTool::Evaluator() { setStdMath(); } bool isOK() { return status() == OK; } double evaluate(const char *e) { double v = HepTool::Evaluator::evaluate(e); if(status() != OK) v = std::numeric_limits::quiet_NaN(); return v; } }; #endif #ifdef TEST_BLEVALUATOR #include "BLEvaluator.hh" #endif struct Test { const char *expr; double value1; // x=1, y=2, z=3 double value2; // x=10, y=-20 z=-30 }; Test tests[] = { { "x", 1.0, 10.0}, { "y", 2.0, -20.0}, { "z", 3.0, -30.0}, { "x+y", 3.0, -10.0}, { "-z", -3.0, 30.0}, { "0-z", -3.0, 30.0}, { "0 - -z", 3.0, -30.0}, { "10/-x", -10.0, -1.0}, { "exp(-z)*exp(z)", 1.0, 1.0}, { "-5*x", -5.0, -50.0}, { "5+-x", 4.0, -5.0}, { "5+(-x)", 4.0, -5.0}, { "5*-x", -5.0, -50.0}, { "5*(-x)", -5.0, -50.0}, { "exp(-0.2*z^2)*exp(0.2*z^2)", 1.0, 1.0}, { "exp(0.2*-z^2)*exp(0.2*z^2)", 1.0, 1.0}, { "exp(-z^2*0.2)*exp(0.2*z^2)", 1.0, 1.0}, { "exp(0.2*-(z^2))*exp(0.2*z^2)", 1.0, 1.0}, { "exp(-(z^2)*0.2)*exp(0.2*z^2)", 1.0, 1.0}, { "exp(-0.2*z)*exp(0.2*z)", 1.0, 1.0}, { "exp(0.2*-z)*exp(0.2*z)", 1.0, 1.0}, { "exp(-z*0.2)*exp(0.2*z)", 1.0, 1.0}, }; int main(int argc, char *argv[]) { BLEvaluator e; e.setVariable("x",1.0); e.setVariable("y",2.0); e.setVariable("z",3.0); //printf("x=1.0 y=2.0 z=3.0\n"); int err=0; for(unsigned i=0; i 1E-12 || !e.isOK()) { printf("%s = %.6f should be %.6f\n",tests[i].expr, e.evaluate(tests[i].expr),tests[i].value1); err = 1; } } e.setVariable("x",10.0); e.setVariable("y",-20.0); e.setVariable("z",-30.0); //printf("x=10.0 y=-20.0 z=-30.0\n"); for(unsigned i=0; i 1E-12 || !e.isOK()) { printf("%s = %.6f should be %.6f\n",tests[i].expr, e.evaluate(tests[i].expr),tests[i].value2); err = 1; } } double v=e.evaluate("unknown(0.0)"); if(!isnan(v) || e.isOK()) { printf("%s succeeded\n","unknown(0.0)"); err=1; } v = e.evaluate("unknown+0.0"); if(!isnan(v) || e.isOK()) { printf("%s succeeded\n","unknown+0.0"); err=1; } exit(err); } ! if ! g++ -DNO_GEANT4 -I$SOURCE/source/include -I$G4BL_DIR/include -o $TMP $TMP.cc $G4BL_DIR/lib/libCLHEP.a then echo >&2 "****** test40: cannot compile test program" exit 1 fi if ! $TMP then echo >&2 "****** test40: FAILED" exit 1 fi exit 0