function_tests.h

00001 /***************************************************************************
00002  *   clipsmm C++ wrapper for the CLIPS c library                           *
00003  *   Copyright (C) 2006 by Rick L. Vinyard, Jr.                            *
00004  *   rvinyard@cs.nmsu.edu                                                  *
00005  *                                                                         *
00006  *   This program is free software; you can redistribute it and/or modify  *
00007  *   it under the terms of version 2 of the GNU General Public License as  *
00008  *   published by the Free Software Foundation.                            *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU Lesser General Public      *
00016  *   License along with this library; if not, write to the                 *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA              *
00019  ***************************************************************************/
00020 #ifndef FUNCTIONTEST_H
00021 #define FUNCTIONTEST_H
00022 
00023 #include <cppunit/TestFixture.h>
00024 
00025 #include <clipsmm/clipsmm.h>
00026 #include <clips/clips.h>
00027 
00028 #include <sstream>
00029 #include <cstdlib>
00030 
00031 using namespace CLIPS;
00032 
00033 int global_int = 0;
00034 
00035 void function0() { global_int = 42; }
00036 
00037 int function1(double d) { return (int)(d+42); }
00038 
00039 double function2(int i, double d) { return i+d; }
00040 
00041 std::string function3(std::string s1, std::string s2) { return s1+s2; }
00042 
00043 double function4(int i1, int i2, float f, double d) {
00044   return i1+i2+f+d;
00045 }
00046 
00047 int function5(int i, double d, std::string s2, long l, float f) {
00048   int t;
00049   std::istringstream sin(s2.substr(1,std::string::npos));
00050   sin >> t;
00051   return (int)(i+d+t+l+f);
00052 }
00053 
00054 long function6(int i1, long l1, double d, float f, int i2, long l2) {
00055   return (long)(i1+l1+d+f+i2+l2);
00056 }
00057 
00058 float function7(int i1, long l1, double d1, float f, int i2, long l2, double d2) {
00059   CPPUNIT_ASSERT( i1 == 3 );
00060   CPPUNIT_ASSERT( l1 == 56l );
00061   CPPUNIT_ASSERT( d1 == 3.8 );
00062   CPPUNIT_ASSERT( f  == 1.3f );
00063   CPPUNIT_ASSERT( i2 == 42 );
00064   CPPUNIT_ASSERT( l2 == 192l );
00065   CPPUNIT_ASSERT( d2 == 28.444 );
00066   return i1+l1+d1+f+i2+l2+d2;
00067 }
00068 
00069 
00070 class FunctionTest : public  CppUnit::TestFixture {
00071   public:
00072 
00073     CPPUNIT_TEST_SUITE( FunctionTest );
00074     CPPUNIT_TEST( expression_test_1 );
00075     CPPUNIT_TEST( expression_test_2 );
00076     CPPUNIT_TEST( builtin_function_test_1 );
00077     CPPUNIT_TEST( builtin_function_test_2 );
00078     CPPUNIT_TEST( class_method_test_0 );
00079     CPPUNIT_TEST( class_method_test_1 );
00080   CPPUNIT_TEST( class_method_test_2 );
00081   CPPUNIT_TEST( class_method_test_3 );
00082   CPPUNIT_TEST( class_method_test_5 );
00083   CPPUNIT_TEST( class_method_test_6 );
00084   CPPUNIT_TEST( class_method_test_7 );
00085   CPPUNIT_TEST( function_test_0 );
00086   CPPUNIT_TEST( function_test_1 );
00087   CPPUNIT_TEST( function_test_2 );
00088   CPPUNIT_TEST( function_test_3 );
00089   CPPUNIT_TEST( function_test_4 );
00090   CPPUNIT_TEST( function_test_5 );
00091   CPPUNIT_TEST( function_test_6 );
00092   CPPUNIT_TEST( function_test_7 );
00093   CPPUNIT_TEST_SUITE_END();
00094 
00095   protected:
00096     CLIPS::Environment environment;
00097     int temp_class_int;
00098 
00099   public:
00100     void setUp() {
00101       temp_class_int = 0;
00102     }
00103 
00104     void tearDown() { }
00105 
00106     void expression_test_1() {
00107       Values values = environment.evaluate( "(+ 3 4)" );
00108       CPPUNIT_ASSERT( values.size() == 1 );
00109       CPPUNIT_ASSERT( values[0] == (3+4) );
00110     }
00111 
00112   void expression_test_2() {
00113     Values values = environment.evaluate( "(- (+ 3 4) 2)" );
00114     CPPUNIT_ASSERT( values.size() == 1 );
00115     CPPUNIT_ASSERT( values[0] == ((3+4)-2) );
00116   }
00117 
00118   void builtin_function_test_1() {
00119     Values values = environment.function( "+", "3 4");
00120     CPPUNIT_ASSERT( values.size() == 1 );
00121     CPPUNIT_ASSERT( values[0] == (3+4) );
00122   }
00123 
00124   void builtin_function_test_2() {
00125     Values values = environment.function( "-", "4 2");
00126     CPPUNIT_ASSERT( values.size() == 1 );
00127     CPPUNIT_ASSERT( values[0] == (4-2) );
00128   }
00129 
00130   void class_method0() { temp_class_int = 42; }
00131 
00132   void class_method_test_0() {
00133     CPPUNIT_ASSERT( temp_class_int == 0 );
00134     environment.add_function( "class_method0", sigc::slot<void>(sigc::mem_fun( *this, &FunctionTest::class_method0 )) );
00135     environment.function( "class_method0" );
00136     CPPUNIT_ASSERT( temp_class_int == 42 );
00137   }
00138 
00139   int class_method1(double d) { return (int)(d+42); }
00140 
00141   void class_method_test_1() {
00142     environment.add_function( "class_method1", sigc::slot<int,double>(sigc::mem_fun( *this, &FunctionTest::class_method1 )) );
00143     Values values = environment.function( "class_method1", "38.8" );
00144     // Only 38 is used to keep the function at an integer value
00145     CPPUNIT_ASSERT( values.size() == 1 );
00146     CPPUNIT_ASSERT( values[0] == 38+42 );
00147   }
00148 
00149   double class_method2(int i, double d) { return i+d; }
00150 
00151   void class_method_test_2() {
00152     environment.add_function( "class_method2", sigc::slot<double,int,double>(sigc::mem_fun( *this, &FunctionTest::class_method2 )) );
00153     Values values = environment.function( "class_method2", "42 38.8" );
00154     CPPUNIT_ASSERT( values.size() == 1 );
00155     CPPUNIT_ASSERT( values[0] == 42+38.8 );
00156   }
00157 
00158   std::string class_method3(std::string s1, std::string s2) { return s1+s2; }
00159 
00160   void class_method_test_3() {
00161     environment.add_function( "class_method3", sigc::slot<std::string,std::string,std::string>(sigc::mem_fun( *this, &FunctionTest::class_method3 )) );
00162     Values values = environment.function( "class_method3", "hello world" );
00163     CPPUNIT_ASSERT( values.size() == 1 );
00164     CPPUNIT_ASSERT( values[0] == "helloworld" );
00165   }
00166 
00167   std::string class_method4(int i, double d, std::string s1, float f) {
00168     std::ostringstream sout;
00169     sout << i << " " << d << s1 << f;
00170     return sout.str();
00171   }
00172 
00173   int class_method5(int i, double d, std::string s2, long l, float f) {
00174     int t;
00175     std::istringstream sin(s2.substr(1,std::string::npos));
00176     sin >> t;
00177     return (int)(i+d+t+l+f);
00178   }
00179 
00180   void class_method_test_5() {
00181     environment.add_function( "class_method5", sigc::slot<int,int,double,std::string,long,float>(sigc::mem_fun( *this, &FunctionTest::class_method5 )) );
00182     Values values = environment.function( "class_method5", "3 3.8 c4 2 1.3" );
00183     CPPUNIT_ASSERT( values.size() == 1 );
00184     CPPUNIT_ASSERT( values[0] == (int)(3+3.8+4+2+1.3) );
00185   }
00186 
00187   long class_method6(int i1, long l1, double d, float f, int i2, long l2) {
00188     return (long)(i1+l1+d+f+i2+l2);
00189   }
00190 
00191   void class_method_test_6() {
00192     environment.add_function( "class_method6", sigc::slot<long,int,long,double,float,int,long>(sigc::mem_fun( *this, &FunctionTest::class_method6 )) );
00193     Values values = environment.function( "class_method6", "3 56 3.8 1.3 42 192" );
00194     CPPUNIT_ASSERT( values.size() == 1 );
00195     CPPUNIT_ASSERT( values[0] == (long)(3+56+3.8+1.3+42+192) );
00196   }
00197 
00198   float class_method7(int i1, long l1, double d1, float f, int i2, long l2, double d2) {
00199     CPPUNIT_ASSERT( i1 == 3 );
00200     CPPUNIT_ASSERT( l1 == 56l );
00201     CPPUNIT_ASSERT( d1 == 3.8 );
00202     CPPUNIT_ASSERT( f  == 1.3f );
00203     CPPUNIT_ASSERT( i2 == 42 );
00204     CPPUNIT_ASSERT( l2 == 192l );
00205     CPPUNIT_ASSERT( d2 == 28.444 );
00206     return i1+l1+d1+f+i2+l2+d2;
00207   }
00208 
00209   void class_method_test_7() {
00210     environment.add_function( "class_method7", sigc::slot<float,int,long,double,float,int,long,double>(sigc::mem_fun( *this, &FunctionTest::class_method7 )) );
00211     Values values = environment.function( "class_method7", "3 56 3.8 1.3 42 192 28.444" );
00212     CPPUNIT_ASSERT( values.size() == 1 );
00213     CPPUNIT_ASSERT( values[0] == (float)(3+56l+3.8+1.3f+42+192l+28.444) );
00214   }
00215 
00216   void function_test_0() {
00217     CPPUNIT_ASSERT( global_int == 0 );
00218     environment.add_function( "function0", sigc::slot<void>(sigc::ptr_fun( &function0 )) );
00219     environment.function( "function0" );
00220     CPPUNIT_ASSERT( global_int == 42 );
00221   }
00222 
00223   void function_test_1() {
00224     environment.add_function( "function1", sigc::slot<int,double>(sigc::ptr_fun( &function1 )) );
00225     Values values = environment.function( "function1", "38.8" );
00226     // Only 38 is used to keep the function at an integer value
00227     CPPUNIT_ASSERT( values.size() == 1 );
00228     CPPUNIT_ASSERT( values[0] == 38+42 );
00229   }
00230 
00231   void function_test_2() {
00232     environment.add_function( "function2", sigc::slot<double,int,double>(sigc::ptr_fun( &function2 )) );
00233     Values values = environment.function( "function2", "42 38.8" );
00234     CPPUNIT_ASSERT( values.size() == 1 );
00235     CPPUNIT_ASSERT( values[0] == 42+38.8 );
00236   }
00237 
00238   void function_test_3() {
00239     environment.add_function( "function3", sigc::slot<std::string,std::string,std::string>(sigc::ptr_fun( &function3 )) );
00240     Values values = environment.function( "function3", "hello world" );
00241     CPPUNIT_ASSERT( values.size() == 1 );
00242     CPPUNIT_ASSERT( values[0] == "helloworld" );
00243   }
00244 
00245   void function_test_4() {
00246     environment.add_function( "function4", sigc::slot<double,int,int,float,double>(sigc::ptr_fun( &function4 )) );
00247     Values values = environment.function( "function4", "1 2 3.0 4.0" );
00248     CPPUNIT_ASSERT( values.size() == 1 );
00249     CPPUNIT_ASSERT( values[0] == 10.0 );
00250   }
00251 
00252   void function_test_5() {
00253     environment.add_function( "function5", sigc::slot<int,int,double,std::string,long,float>(sigc::ptr_fun( &function5 )) );
00254     Values values = environment.function( "function5", "3 3.8 c4 2 1.3" );
00255     CPPUNIT_ASSERT( values.size() == 1 );
00256     CPPUNIT_ASSERT( values[0] == (int)(3+3.8+4+2+1.3) );
00257   }
00258 
00259   void function_test_6() {
00260     environment.add_function( "function6", sigc::slot<long,int,long,double,float,int,long>(sigc::ptr_fun( &function6 )) );
00261     Values values = environment.function( "function6", "3 56 3.8 1.3 42 192" );
00262     CPPUNIT_ASSERT( values.size() == 1 );
00263     CPPUNIT_ASSERT( values[0] == (long)(3+56+3.8+1.3+42+192) );
00264   }
00265 
00266   void function_test_7() {
00267     environment.add_function( "function7", sigc::slot<float,int,long,double,float,int,long,double>(sigc::ptr_fun( &function7 )) );
00268     Values values = environment.function( "function7", "3 56 3.8 1.3 42 192 28.444" );
00269     CPPUNIT_ASSERT( values.size() == 1 );
00270     CPPUNIT_ASSERT( values[0] == (float)(3+56l+3.8+1.3f+42+192l+28.444) );
00271   }
00272 
00273 
00274 };
00275 
00276 #endif

Generated on Sun Nov 12 11:55:35 2006 by  doxygen 1.5.1