CLHEP 2.0.4.7 Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

testBug66214.cc

Go to the documentation of this file.
00001 // ---------------------------------------------------------------------------
00002 //
00003 // This file is a part of the CLHEP - a Class Library for High Energy Physics.
00004 //
00005 // This is a test for bug report 66214 in the Evaluator class.
00006 //
00007 #include <iostream>
00008 #include <fstream>
00009 #include <CLHEP/Evaluator/Evaluator.h>
00010 
00011 double eval( std::string expr, int& numbad, std::ofstream& os)
00012 {
00013     static HepTool::Evaluator *ev=0;
00014     if(ev == 0) ev = new HepTool::Evaluator();
00015     ev->setStdMath();                 // set standard constants and functions
00016     ev->setSystemOfUnits();           // set SI units
00017 
00018     double v = ev->evaluate(expr.data());
00019 
00020     os << "CALC> " << expr << ": ";
00021     if(ev->status() != HepTool::Evaluator::OK) {
00022         os << ev->error_name() << std::endl;
00023         ++numbad;
00024     } else {
00025         os << v << std::endl;
00026     }
00027     return v;
00028 }
00029 
00030 int main()
00031 {
00032 
00033     int numbad = 0;
00034     double result;
00035     // many of these expressions fail in releases prior to 2.0.4.7
00036     std::string exp[38] = {   " +1",     " -1",     "1 + 1",  "1 + -1","1 + (-1)",
00037                      "1 + +1","1 + (+1)",    "1 * -1","1 * (-1)",  "-1 * 1",
00038                       "10^-1", "10^(-1)",       "9*4",  "9 * -4","9 * (-4)",
00039                      "4*---2","4*(---2)","4*(-(--2))","4*(--(-2))","4*(-(-(-2)))",
00040                       "4*--2", "4*(--2)", "4*(-(-2))",    "-5^2",   "9*4+2",
00041                      "231/-11","231/-11+10","231/-11/3","(231/-11)+10",
00042                      "100/5^2","100/+5^2","100/-5^2",   "9*4+30", "9*4+-30",
00043                      "-9*4+30","9*-4+30","9*(-4)+30","(9*-4)+30" };
00044     double res[38] = {     1.,       -1.,          2.,        0.,        0., 
00045                            2.,        2.,         -1.,       -1.,       -1.,
00046                           0.1,       0.1,         36.,      -36.,      -36.,
00047                           -8.,       -8.,         -8.,       -8.,       -8.,
00048                            8.,        8.,          8.,       25.,       38.,
00049                          -21.,        -11.,       -7.,      -11.,
00050                            4.,        4.,          4.,       66.,        6., 
00051                           -6.,       -6.,       -6.,         -6. };
00052     std::string exp2[3] = { "sin(45*deg)", "sin(45*pi/-180)", "232/22" };
00053 
00054     std::ofstream os("testBug66214.cout");  
00055 
00056     for(int i=0; i<38; ++i ) {
00057         result=eval(exp[i],numbad,os);
00058         if( result != res[i] ) {
00059             ++numbad;
00060             os << "ERROR:  expected " << res[i] << " got " << result << std::endl;
00061             os << std::endl;
00062         }
00063     }
00064 
00065     // inspect these by hand
00066     // return values: 0.707107, -0.707107, 10.5455
00067     for(int i=0; i<3; ++i ) {
00068         eval(exp2[i],numbad,os);
00069     }
00070 
00071     return numbad;
00072 }

Generated on Thu Jul 1 22:02:31 2010 for CLHEP by  doxygen 1.4.7