cgma
fileoptions.cpp
Go to the documentation of this file.
00001 
00006 #include "CGMFileOptions.hpp"
00007 
00008 #include <ctype.h>
00009 #include <stdlib.h>
00010 #include <string.h>
00011 
00012 const char DEFAULT_SEPARATOR = ';';
00013 static inline bool strempty( const char* s ) { return !*s; }
00014 
00015 #include <iostream>
00016 
00017 #define CHECK(A) \
00018   if (FO_SUCCESS != (A)) { \
00019     std::cerr << "Failure at line " << __LINE__ << ": error code " << (A) << std::endl; \
00020     return 1; \
00021   }
00022 
00023 #define EQUAL(A,B) \
00024   if (A != B) { \
00025     std::cerr << "Failure at line " << __LINE__ << ": expected " << (B) << " but got " << (A) << std::endl; \
00026     return 2; \
00027   }
00028 
00029 int main()
00030 {
00031   CGMFileOptions tool( "INT1=1;NUL1;STR1=ABC;DBL1=1.0;dbl2=2.0;DBL3=3.0;INT2=2;nul2;NUL3;INT3=3;str2=once upon a time;str3==fubar=;;" );
00032 
00033   std::string s;
00034   int i;
00035   double d;
00036   CGMFOErrorCode rval;
00037   
00038     // test basic get_option method without deleting entry
00039   rval = tool.get_option( "STR1", s );
00040   CHECK(rval);
00041   EQUAL( s, "ABC" );
00042   
00043     // test basic get_option method again, this time deleting the entry
00044   rval = tool.get_option( "STR1", s );
00045   CHECK(rval);
00046   EQUAL( s, "ABC" );
00047   
00048     // test basig get_option method with a null option
00049   rval = tool.get_option( "NUL2", s );
00050   CHECK( rval );
00051   EQUAL( s.empty(), true );
00052 
00053   
00054     // test null option
00055   rval = tool.get_null_option( "nul1" );
00056   CHECK( rval );
00057   
00058     // try null option method on non-null value
00059   rval = tool.get_null_option( "INT1" ) ;
00060   EQUAL( rval, FO_TYPE_OUT_OF_RANGE) ;
00061   
00062 
00063     // test integer option
00064   rval = tool.get_int_option( "int1", i );
00065   CHECK( rval );
00066   EQUAL( i, 1 );
00067   
00068   rval = tool.get_int_option( "int2", i );
00069   CHECK( rval );
00070   EQUAL( i, 2 );
00071   
00072     // test integer option on non-integer value
00073   rval = tool.get_int_option( "dbl2", i );
00074   EQUAL( rval, FO_TYPE_OUT_OF_RANGE );
00075   
00076     // test integer option on null value
00077   rval = tool.get_int_option( "NUL3", i);
00078   EQUAL( rval, FO_TYPE_OUT_OF_RANGE );
00079   
00080     // test double option
00081   rval = tool.get_real_option( "dbl1", d );
00082   CHECK( rval );
00083   EQUAL( d, 1.0 );
00084   
00085   rval = tool.get_real_option( "dbl2", d );
00086   CHECK( rval );
00087   EQUAL( d, 2.0 );
00088   
00089   rval = tool.get_real_option( "int3", d );
00090   CHECK( rval );
00091   EQUAL( d, 3.0 );
00092   
00093     // test real option on non-real value
00094   rval = tool.get_real_option( "str2", d );
00095   EQUAL( rval, FO_TYPE_OUT_OF_RANGE );
00096   
00097   
00098     // test real option on null value
00099   rval = tool.get_real_option( "NUL3", d );
00100   EQUAL( rval, FO_TYPE_OUT_OF_RANGE );
00101   
00102     // test get a simple string option
00103   rval = tool.get_str_option( "DBL3", s );
00104   CHECK( rval );
00105   EQUAL( s, "3.0" );
00106   
00107     // test get a string with spaces
00108   rval = tool.get_str_option("STR2", s );
00109   CHECK( rval );
00110   EQUAL( s, "once upon a time" );
00111   
00112     // try to get a string value for a null option
00113   rval = tool.get_str_option( "nul3", s );
00114   EQUAL( rval, FO_TYPE_OUT_OF_RANGE );
00115   
00116     // test options using generic get_option method
00117     
00118   rval = tool.get_option( "NUL3", s );
00119   CHECK( rval );
00120   EQUAL( s.empty(), true );
00121   
00122   rval = tool.get_option( "STR3", s );
00123   CHECK( rval );
00124   EQUAL( s, "=fubar=" );
00125   
00126     // test size of options string
00127   unsigned l = tool.size();
00128   EQUAL( l, 12u );
00129   
00130   
00131     // test alternate separator
00132   
00133   CGMFileOptions tool2( ";+OPT1=ABC+OPT2=" );
00134   l = tool2.size();
00135   EQUAL( l, 2 );
00136   
00137   rval = tool2.get_option( "opt1", s );
00138   CHECK( rval );
00139   EQUAL( s, "ABC" );
00140   
00141   rval = tool2.get_option( "opt2", s );
00142   CHECK( rval );
00143   bool e = s.empty();
00144   EQUAL( e, true );
00145   
00146   l = tool2.size();
00147   EQUAL( l, 2 );
00148   
00149     
00150     // test empty options string
00151     
00152   CGMFileOptions tool3( ";;;;" );
00153   e = tool3.empty();
00154   EQUAL( e, true );
00155   l = tool3.size();
00156   EQUAL( l, 0 );
00157   
00158   CGMFileOptions tool4(NULL);
00159   e = tool4.empty();
00160   EQUAL( e, true );
00161   l = tool4.size();
00162   EQUAL( l, 0 );
00163   
00164   CGMFileOptions tool5(";+");
00165   e = tool5.empty();
00166   EQUAL( e, true );
00167   l = tool5.size();
00168   EQUAL( l, 0 );
00169   
00170     // test copy constructor
00171   
00172   CGMFileOptions tool6( tool2 );
00173   
00174   rval = tool6.get_option( "opt1", s );
00175   CHECK( rval );
00176   EQUAL( s, "ABC" );
00177   
00178   rval = tool6.get_option( "opt2", s );
00179   CHECK( rval );
00180   e = s.empty();
00181   EQUAL( e, true );
00182   
00183   l = tool6.size();
00184   EQUAL( l, 2 );
00185   
00186   CGMFileOptions tool7( tool5 );
00187   e = tool7.empty();
00188   EQUAL( e, true );
00189   l = tool7.size();
00190   EQUAL( l, 0 );
00191   
00192     // test assignment operator
00193   
00194   CGMFileOptions tool8( tool2 );
00195   tool8 = tool;
00196   EQUAL( tool8.size(), tool.size() );
00197     
00198   return 0;
00199 }
00200 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines