value_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 VALUETEST_H
00021 #define VALUETEST_H
00022 
00023 #include <cppunit/TestFixture.h>
00024 
00025 #include <clipsmm/clipsmm.h>
00026 
00027 using namespace CLIPS;
00028 
00029 class ValueTest : public CppUnit::TestFixture {
00030   public:
00031 
00032     CPPUNIT_TEST_SUITE( ValueTest );
00033     CPPUNIT_TEST( value_create_and_compare_float );
00034     CPPUNIT_TEST( value_create_and_compare_double );
00035     CPPUNIT_TEST( value_create_and_compare_short_int );
00036     CPPUNIT_TEST( value_create_and_compare_short_unsigned_int );
00037     CPPUNIT_TEST( value_create_and_compare_int );
00038     CPPUNIT_TEST( value_create_and_compare_unsigned_int );
00039     CPPUNIT_TEST( value_create_and_compare_long_int );
00040     CPPUNIT_TEST( value_create_and_compare_string );
00041     CPPUNIT_TEST( value_create_and_compare_cstring );
00042     CPPUNIT_TEST( value_create_and_compare_address );
00043     CPPUNIT_TEST( value_assignment_float );
00044     CPPUNIT_TEST( value_assignment_double );
00045     CPPUNIT_TEST( value_assignment_short_int );
00046     CPPUNIT_TEST( value_assignment_short_unsigned_int );
00047     CPPUNIT_TEST( value_assignment_int );
00048     CPPUNIT_TEST( value_assignment_unsigned_int );
00049     CPPUNIT_TEST( value_assignment_long_int );
00050     CPPUNIT_TEST( value_assignment_string );
00051     CPPUNIT_TEST( value_assignment_cstring );
00052     CPPUNIT_TEST( value_assignment_address );
00053     CPPUNIT_TEST( value_change_type );
00054     CPPUNIT_TEST_SUITE_END();
00055 
00056   protected:
00057     CLIPS::Environment environment;
00058     
00059   public:
00060     void setUp() {
00061     }
00062 
00063     void tearDown() { }
00064 
00065     template <typename T, typename U>
00066     bool test_create_and_compare_equal( T x, U y ) {
00067       Value v(x);
00068       return v == y;
00069     }
00070     
00071     template <typename T, typename U>
00072     bool test_create_and_compare_inequal( T x, U y ) {
00073       Value v(x);
00074       return v != y;
00075     }
00076     
00077     void value_create_and_compare_float() {
00078       CPPUNIT_ASSERT( test_create_and_compare_equal( 3.3f, 3.3f ) );
00079       CPPUNIT_ASSERT( test_create_and_compare_inequal( 3.3f, 3.4f ) );
00080     }
00081 
00082     void value_create_and_compare_double() {
00083       CPPUNIT_ASSERT( test_create_and_compare_equal( 3.3, 3.3 ) );
00084       CPPUNIT_ASSERT( test_create_and_compare_inequal( 3.3, 3.4 ) );
00085     }
00086 
00087     void value_create_and_compare_short_int() {
00088       CPPUNIT_ASSERT( test_create_and_compare_equal( (short)3, (short)3 ) );
00089       CPPUNIT_ASSERT( test_create_and_compare_inequal( (short)3, (short)4 ) );
00090     }
00091 
00092     void value_create_and_compare_short_unsigned_int() {
00093       CPPUNIT_ASSERT( test_create_and_compare_equal( (unsigned short)3, (unsigned short)3 ) );
00094       CPPUNIT_ASSERT( test_create_and_compare_inequal( (unsigned short)3, (unsigned short)4 ) );
00095     }
00096 
00097     void value_create_and_compare_int() {
00098       CPPUNIT_ASSERT( test_create_and_compare_equal( 3, 3 ) );
00099       CPPUNIT_ASSERT( test_create_and_compare_inequal( 3, 4 ) );
00100     }
00101 
00102     void value_create_and_compare_unsigned_int() {
00103       CPPUNIT_ASSERT( test_create_and_compare_equal( 3U, 3U ) );
00104       CPPUNIT_ASSERT( test_create_and_compare_inequal( 3U, 4U ) );
00105     }
00106 
00107     void value_create_and_compare_long_int() {
00108       CPPUNIT_ASSERT( test_create_and_compare_equal( 3L, 3L ) );
00109       CPPUNIT_ASSERT( test_create_and_compare_inequal( 3L, 4L ) );
00110     }
00111 
00112     void value_create_and_compare_string() {
00113       std::string x("one"), y("one"), z("two");
00114       CPPUNIT_ASSERT( test_create_and_compare_equal( x, y ) );
00115       CPPUNIT_ASSERT( test_create_and_compare_inequal( x, z ) );
00116     }
00117 
00118     void value_create_and_compare_cstring() {
00119       CPPUNIT_ASSERT( test_create_and_compare_equal( "one", "one" ) );
00120       CPPUNIT_ASSERT( test_create_and_compare_inequal( "one", "two" ) );
00121     }
00122 
00123     void value_create_and_compare_address() {
00124       double d1=3.3, d2=3.3;
00125       double *p1=&d1, *p2=&d1, *p3=&d2;
00126       CPPUNIT_ASSERT( test_create_and_compare_equal( p1, p2 ) );
00127       CPPUNIT_ASSERT( test_create_and_compare_inequal( p1, p3 ) );
00128     }
00129 
00130     void value_assignment_float() {
00131       Value v(3.3f);
00132       CPPUNIT_ASSERT( ( v = 3.5f ) == 3.5f );
00133       CPPUNIT_ASSERT( v.type() == TYPE_FLOAT );
00134       CPPUNIT_ASSERT( ( v = 3.5 ) == 3.5 );
00135       CPPUNIT_ASSERT( v.type() == TYPE_FLOAT );
00136       CPPUNIT_ASSERT( ( v = (short)3 ) == (short)3 );
00137       CPPUNIT_ASSERT( v.type() == TYPE_FLOAT );
00138       CPPUNIT_ASSERT( ( v = (short unsigned)3 ) == (short unsigned)3 );
00139       CPPUNIT_ASSERT( v.type() == TYPE_FLOAT );
00140       CPPUNIT_ASSERT( ( v = 3 ) == 3 );
00141       CPPUNIT_ASSERT( v.type() == TYPE_FLOAT );
00142       CPPUNIT_ASSERT( ( v = 3U ) == 3U );
00143       CPPUNIT_ASSERT( v.type() == TYPE_FLOAT );
00144       CPPUNIT_ASSERT( ( v = 3L ) == 3L );
00145       CPPUNIT_ASSERT( v.type() == TYPE_FLOAT );
00146     }
00147 
00148     void value_assignment_double() {
00149       Value v(3.3);
00150       CPPUNIT_ASSERT( ( v = 3.5f ) == 3.5f );
00151       CPPUNIT_ASSERT( v.type() == TYPE_FLOAT );
00152       CPPUNIT_ASSERT( ( v = 3.5 ) == 3.5 );
00153       CPPUNIT_ASSERT( v.type() == TYPE_FLOAT );
00154       CPPUNIT_ASSERT( ( v = (short)3 ) == (short)3 );
00155       CPPUNIT_ASSERT( v.type() == TYPE_FLOAT );
00156       CPPUNIT_ASSERT( ( v = (short unsigned)3 ) == (short unsigned)3 );
00157       CPPUNIT_ASSERT( v.type() == TYPE_FLOAT );
00158       CPPUNIT_ASSERT( ( v = 3 ) == 3 );
00159       CPPUNIT_ASSERT( v.type() == TYPE_FLOAT );
00160       CPPUNIT_ASSERT( ( v = 3U ) == 3U );
00161       CPPUNIT_ASSERT( v.type() == TYPE_FLOAT );
00162       CPPUNIT_ASSERT( ( v = 3L ) == 3L );
00163       CPPUNIT_ASSERT( v.type() == TYPE_FLOAT );
00164     }
00165 
00166     void value_assignment_short_int() {
00167       Value v((short)3);
00168       CPPUNIT_ASSERT( ( v = 3.5f ) == 3 );
00169       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00170       CPPUNIT_ASSERT( ( v = 3.5 ) == 3 );
00171       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00172       CPPUNIT_ASSERT( ( v = (short)3 ) == (short)3 );
00173       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00174       CPPUNIT_ASSERT( ( v = (short unsigned)3 ) == (short unsigned)3 );
00175       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00176       CPPUNIT_ASSERT( ( v = 3 ) == 3 );
00177       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00178       CPPUNIT_ASSERT( ( v = 3U ) == 3U );
00179       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00180       CPPUNIT_ASSERT( ( v = 3L ) == 3L );
00181       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00182     }
00183 
00184     void value_assignment_short_unsigned_int() {
00185       Value v((short unsigned)3);
00186       CPPUNIT_ASSERT( ( v = 3.5f ) == 3 );
00187       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00188       CPPUNIT_ASSERT( ( v = 3.5 ) == 3 );
00189       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00190       CPPUNIT_ASSERT( ( v = (short)3 ) == (short)3 );
00191       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00192       CPPUNIT_ASSERT( ( v = (short unsigned)3 ) == (short unsigned)3 );
00193       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00194       CPPUNIT_ASSERT( ( v = 3 ) == 3 );
00195       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00196       CPPUNIT_ASSERT( ( v = 3U ) == 3U );
00197       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00198       CPPUNIT_ASSERT( ( v = 3L ) == 3L );
00199       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00200     }
00201 
00202     void value_assignment_int() {
00203       Value v(3);
00204       CPPUNIT_ASSERT( ( v = 3.5f ) == 3 );
00205       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00206       CPPUNIT_ASSERT( ( v = 3.5 ) == 3 );
00207       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00208       CPPUNIT_ASSERT( ( v = (short)3 ) == (short)3 );
00209       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00210       CPPUNIT_ASSERT( ( v = (short unsigned)3 ) == (short unsigned)3 );
00211       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00212       CPPUNIT_ASSERT( ( v = 3 ) == 3 );
00213       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00214       CPPUNIT_ASSERT( ( v = 3U ) == 3U );
00215       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00216       CPPUNIT_ASSERT( ( v = 3L ) == 3L );
00217       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00218     }
00219 
00220     void value_assignment_unsigned_int() {
00221       Value v(3U);
00222       CPPUNIT_ASSERT( ( v = 3.5f ) == 3 );
00223       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00224       CPPUNIT_ASSERT( ( v = 3.5 ) == 3 );
00225       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00226       CPPUNIT_ASSERT( ( v = (short)3 ) == (short)3 );
00227       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00228       CPPUNIT_ASSERT( ( v = (short unsigned)3 ) == (short unsigned)3 );
00229       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00230       CPPUNIT_ASSERT( ( v = 3 ) == 3 );
00231       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00232       CPPUNIT_ASSERT( ( v = 3U ) == 3U );
00233       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00234       CPPUNIT_ASSERT( ( v = 3L ) == 3L );
00235       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00236     }
00237 
00238     void value_assignment_long_int() {
00239       Value v(3L);
00240       CPPUNIT_ASSERT( ( v = 3.5f ) == 3 );
00241       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00242       CPPUNIT_ASSERT( ( v = 3.5 ) == 3 );
00243       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00244       CPPUNIT_ASSERT( ( v = (short)3 ) == (short)3 );
00245       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00246       CPPUNIT_ASSERT( ( v = (short unsigned)3 ) == (short unsigned)3 );
00247       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00248       CPPUNIT_ASSERT( ( v = 3 ) == 3 );
00249       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00250       CPPUNIT_ASSERT( ( v = 3U ) == 3U );
00251       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00252       CPPUNIT_ASSERT( ( v = 3L ) == 3L );
00253       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00254     }
00255 
00256     void value_assignment_string() {
00257       Value v(std::string("one"));
00258       CPPUNIT_ASSERT( ( v = std::string("two") ) == std::string("two") );
00259       CPPUNIT_ASSERT( v.type() == TYPE_STRING );
00260       CPPUNIT_ASSERT( ( v = "two" ) == "two" );
00261       CPPUNIT_ASSERT( v.type() == TYPE_STRING );
00262     }
00263 
00264     void value_assignment_cstring() {
00265       Value v("one");
00266       CPPUNIT_ASSERT( ( v = std::string("two") ) == std::string("two") );
00267       CPPUNIT_ASSERT( v.type() == TYPE_STRING );
00268       CPPUNIT_ASSERT( ( v = "two" ) == "two" );
00269       CPPUNIT_ASSERT( v.type() == TYPE_STRING );
00270     }
00271 
00272     void value_assignment_address() {
00273       double d=3.3;
00274       double *p;
00275       Value v(TYPE_EXTERNAL_ADDRESS);
00276       CPPUNIT_ASSERT( ( v = &d ) == &d );
00277       p = (double*)(void*)v;
00278       CPPUNIT_ASSERT( *p == 3.3 );
00279     }
00280 
00281     void value_change_type() {
00282       Value v(TYPE_STRING);
00283       CPPUNIT_ASSERT( v.set(3.5f, true) == 3.5f );
00284       CPPUNIT_ASSERT( v.type() == TYPE_FLOAT );
00285       CPPUNIT_ASSERT( v.set(3.6, true) == 3.6 );
00286       CPPUNIT_ASSERT( v.type() == TYPE_FLOAT );
00287       CPPUNIT_ASSERT( v.set( (short)4, true ) == (short)4 );
00288       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00289       CPPUNIT_ASSERT( v.set( (short unsigned)5, true ) == (short unsigned)5 );
00290       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00291       CPPUNIT_ASSERT( v.set( 3, true ) == 3 );
00292       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00293       CPPUNIT_ASSERT( v.set( 3U, true ) == 3U );
00294       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00295       CPPUNIT_ASSERT( v.set( 3L, true ) == 3L );
00296       CPPUNIT_ASSERT( v.type() == TYPE_INTEGER );
00297       CPPUNIT_ASSERT( v.set( std::string("two"), true, TYPE_STRING ) == std::string("two") );
00298       CPPUNIT_ASSERT( v.type() == TYPE_STRING );
00299       CPPUNIT_ASSERT( v.set( "three", true, TYPE_STRING ) == "three" );
00300       CPPUNIT_ASSERT( v.type() == TYPE_STRING );
00301       CPPUNIT_ASSERT( v.set( std::string("four"), true, TYPE_SYMBOL ) == std::string("four") );
00302       CPPUNIT_ASSERT( v.type() == TYPE_SYMBOL );
00303       CPPUNIT_ASSERT( v.set( "five", true, TYPE_SYMBOL ) == "five" );
00304       CPPUNIT_ASSERT( v.type() == TYPE_SYMBOL );
00305       CPPUNIT_ASSERT( v.set( std::string("six"), true, TYPE_INSTANCE_NAME ) == std::string("six") );
00306       CPPUNIT_ASSERT( v.type() == TYPE_INSTANCE_NAME );
00307       CPPUNIT_ASSERT( v.set( "seven", true, TYPE_INSTANCE_NAME ) == "seven" );
00308       CPPUNIT_ASSERT( v.type() == TYPE_INSTANCE_NAME );
00309     }
00310 
00311 };
00312 
00313 #endif

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