CLHEP VERSION 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[43] = {   " +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                      "100/(5^2)","100/(+5^2)","100/(-5^2)",   "100/(-5)^2", "100/((-5)^2)",
00044                      "-9*4+30","9*-4+30","9*(-4)+30","(9*-4)+30" };
00045     double res[43] = {     1.,       -1.,          2.,        0.,        0., 
00046                            2.,        2.,         -1.,       -1.,       -1.,
00047                           0.1,       0.1,         36.,      -36.,      -36.,
00048                           -8.,       -8.,         -8.,       -8.,       -8.,
00049                            8.,        8.,          8.,      -25.,       38.,
00050                          -21.,      -11.,         -7.,      -11.,
00051                            4.,        4.,         -4.,       66.,        6., 
00052                            4.,        4.,         -4.,        4.,        4., 
00053                           -6.,       -6.,         -6.,       -6. };
00054     std::string exp2[3] = { "sin(45*deg)", "sin(45*pi/-180)", "232/22" };
00055 
00056     std::ofstream os("testBug66214.cout");  
00057 
00058     for(int i=0; i<43; ++i ) {
00059         result=eval(exp[i],numbad,os);
00060         if( result != res[i] ) {
00061             ++numbad;
00062             os << "ERROR:  expected " << res[i] << " got " << result << std::endl;
00063             os << std::endl;
00064         }
00065     }
00066 
00067     // inspect these by hand
00068     // return values: 0.707107, -0.707107, 10.5455
00069     for(int i=0; i<3; ++i ) {
00070         eval(exp2[i],numbad,os);
00071     }
00072 
00073     return numbad;
00074 }

Generated on 15 Nov 2012 for CLHEP by  doxygen 1.4.7