MOAB: Mesh Oriented datABase  (version 5.4.1)
fileopts_test.cpp
Go to the documentation of this file.
00001 /*
00002  * MOAB, a Mesh-Oriented datABase, is a software component for creating,
00003  * storing and accessing finite element mesh data.
00004  *
00005  * Copyright 2004 Sandia Corporation.  Under the terms of Contract
00006  * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
00007  * retains certain rights in this software.
00008  *
00009  * This library is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 2.1 of the License, or (at your option) any later version.
00013  *
00014  */
00015 
00016 /**\file FileOptions.cpp
00017  *\author Jason Kraftcheck ([email protected])
00018  *\date 2007-08-21
00019  */
00020 
00021 #include "moab/FileOptions.hpp"
00022 
00023 #include <cctype>
00024 #include <cstdlib>
00025 #include <cstring>
00026 #include <algorithm>
00027 #include <iostream>
00028 
00029 using namespace moab;
00030 
00031 #define CHECK( A )                                                                            \
00032     if( MB_SUCCESS != ( A ) )                                                                 \
00033     {                                                                                         \
00034         std::cerr << "Failure at line " << __LINE__ << ": error code " << ( A ) << std::endl; \
00035         return 1;                                                                             \
00036     }
00037 
00038 #define EQUAL( A, B )                                                                                               \
00039     if( ( A ) != ( B ) )                                                                                            \
00040     {                                                                                                               \
00041         std::cerr << "Failure at line " << __LINE__ << ": expected " << ( B ) << " but got " << ( A ) << std::endl; \
00042         return 2;                                                                                                   \
00043     }
00044 
00045 int main()
00046 {
00047     FileOptions 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 "
00048                       "time;str3==fubar=;;INTS=1-3,5,6;DBLS=1.0,2.0, 3.0;STRS=var1, var2_var2;STRS2=" );
00049 
00050     std::string s;
00051     int i;
00052     double d;
00053     ErrorCode rval;
00054 
00055     // test basic get_option method without deleting entry
00056     rval = tool.get_option( "STR1", s );
00057     CHECK( rval );
00058     EQUAL( s, "ABC" );
00059 
00060     // test basic get_option method again, this time deleting the entry
00061     rval = tool.get_option( "STR1", s );
00062     CHECK( rval );
00063     EQUAL( s, "ABC" );
00064 
00065     // test basig get_option method with a null option
00066     rval = tool.get_option( "NUL2", s );
00067     CHECK( rval );
00068     EQUAL( s.empty(), true );
00069 
00070     // test null option
00071     rval = tool.get_null_option( "nul1" );
00072     CHECK( rval );
00073 
00074     // try null option method on non-null value
00075     rval = tool.get_null_option( "INT1" );
00076     EQUAL( rval, MB_TYPE_OUT_OF_RANGE );
00077 
00078     // test integer option
00079     rval = tool.get_int_option( "int1", i );
00080     CHECK( rval );
00081     EQUAL( i, 1 );
00082 
00083     rval = tool.get_int_option( "int2", i );
00084     CHECK( rval );
00085     EQUAL( i, 2 );
00086 
00087     // test integer option on non-integer value
00088     rval = tool.get_int_option( "dbl2", i );
00089     EQUAL( rval, MB_TYPE_OUT_OF_RANGE );
00090 
00091     // test integer option on null value
00092     rval = tool.get_int_option( "NUL3", i );
00093     EQUAL( rval, MB_TYPE_OUT_OF_RANGE );
00094 
00095     // test double option
00096     rval = tool.get_real_option( "dbl1", d );
00097     CHECK( rval );
00098     EQUAL( d, 1.0 );
00099 
00100     rval = tool.get_real_option( "dbl2", d );
00101     CHECK( rval );
00102     EQUAL( d, 2.0 );
00103 
00104     rval = tool.get_real_option( "int3", d );
00105     CHECK( rval );
00106     EQUAL( d, 3.0 );
00107 
00108     // test real option on non-real value
00109     rval = tool.get_real_option( "str2", d );
00110     EQUAL( rval, MB_TYPE_OUT_OF_RANGE );
00111 
00112     // test real option on null value
00113     rval = tool.get_real_option( "NUL3", d );
00114     EQUAL( rval, MB_TYPE_OUT_OF_RANGE );
00115 
00116     // test get a simple string option
00117     rval = tool.get_str_option( "DBL3", s );
00118     CHECK( rval );
00119     EQUAL( s, "3.0" );
00120 
00121     // test get a string with spaces
00122     rval = tool.get_str_option( "STR2", s );
00123     CHECK( rval );
00124     EQUAL( s, "once upon a time" );
00125 
00126     // try to get a string value for a null option
00127     rval = tool.get_str_option( "nul3", s );
00128     EQUAL( rval, MB_TYPE_OUT_OF_RANGE );
00129 
00130     // We haven't looked at all of the options yet
00131     EQUAL( false, tool.all_seen() );
00132     rval = tool.get_unseen_option( s );
00133     CHECK( rval );
00134     EQUAL( s, "str3" );
00135 
00136     // test options using generic get_option method
00137 
00138     rval = tool.get_option( "NUL3", s );
00139     CHECK( rval );
00140     EQUAL( s.empty(), true );
00141 
00142     rval = tool.get_option( "STR3", s );
00143     CHECK( rval );
00144     EQUAL( s, "=fubar=" );
00145 
00146     // test size of options string
00147     unsigned l = tool.size();
00148     EQUAL( l, 16u );
00149 
00150     // test ints option
00151     std::vector< int > ivals;
00152     rval = tool.get_ints_option( "INTS", ivals );
00153     CHECK( rval );
00154     EQUAL( 5, ivals.size() );
00155     EQUAL( 1, ivals[0] );
00156     EQUAL( 2, ivals[1] );
00157     EQUAL( 3, ivals[2] );
00158     EQUAL( 5, ivals[3] );
00159     EQUAL( 6, ivals[4] );
00160 
00161     // test dbls option
00162     std::vector< double > vals;
00163     rval = tool.get_reals_option( "DBLS", vals );
00164     CHECK( rval );
00165     EQUAL( 3, vals.size() );
00166     EQUAL( 1.0, vals[0] );
00167     EQUAL( 2.0, vals[1] );
00168     EQUAL( 3.0, vals[2] );
00169 
00170     // test strs option
00171     std::vector< std::string > svals;
00172     rval = tool.get_strs_option( "STRS", svals );
00173     CHECK( rval );
00174     EQUAL( 2, svals.size() );
00175     EQUAL( "var1", svals[0] );
00176     EQUAL( "var2_var2", svals[1] );
00177 
00178     svals.clear();
00179     rval = tool.get_strs_option( "STRS2", svals );
00180     EQUAL( MB_TYPE_OUT_OF_RANGE, rval );
00181 
00182     // We requested every option
00183     EQUAL( true, tool.all_seen() );
00184     rval = tool.get_unseen_option( s );
00185     EQUAL( MB_ENTITY_NOT_FOUND, rval );
00186 
00187     // test alternate separator
00188 
00189     FileOptions tool2( ";+OPT1=ABC+OPT2=" );
00190     l = tool2.size();
00191     EQUAL( l, 2 );
00192 
00193     // We haven't looked at all of the options yet
00194     EQUAL( false, tool2.all_seen() );
00195     rval = tool2.get_unseen_option( s );
00196     CHECK( rval );
00197     EQUAL( s, "OPT1" );
00198 
00199     rval = tool2.get_option( "opt1", s );
00200     CHECK( rval );
00201     EQUAL( s, "ABC" );
00202 
00203     rval = tool2.get_option( "opt2", s );
00204     CHECK( rval );
00205     bool e = s.empty();
00206     EQUAL( e, true );
00207 
00208     l = tool2.size();
00209     EQUAL( l, 2 );
00210 
00211     // We requested every option
00212     EQUAL( true, tool2.all_seen() );
00213     rval = tool2.get_unseen_option( s );
00214     EQUAL( MB_ENTITY_NOT_FOUND, rval );
00215 
00216     // test empty options string
00217 
00218     FileOptions tool3( ";;;;" );
00219     e = tool3.empty();
00220     EQUAL( e, true );
00221     l = tool3.size();
00222     EQUAL( l, 0 );
00223     EQUAL( true, tool3.all_seen() );
00224 
00225     FileOptions tool4( NULL );
00226     e = tool4.empty();
00227     EQUAL( e, true );
00228     l = tool4.size();
00229     EQUAL( l, 0 );
00230     EQUAL( true, tool4.all_seen() );
00231 
00232     FileOptions tool5( ";+" );
00233     e = tool5.empty();
00234     EQUAL( e, true );
00235     l = tool5.size();
00236     EQUAL( l, 0 );
00237     EQUAL( true, tool5.all_seen() );
00238 
00239     // test copy constructor
00240 
00241     const FileOptions& tool6( tool2 );
00242 
00243     rval = tool6.get_option( "opt1", s );
00244     CHECK( rval );
00245     EQUAL( s, "ABC" );
00246 
00247     rval = tool6.get_option( "opt2", s );
00248     CHECK( rval );
00249     e = s.empty();
00250     EQUAL( e, true );
00251 
00252     l = tool6.size();
00253     EQUAL( l, 2 );
00254 
00255     const FileOptions& tool7( tool5 );
00256     e = tool7.empty();
00257     EQUAL( e, true );
00258     l = tool7.size();
00259     EQUAL( l, 0 );
00260 
00261     // test assignment operator
00262 
00263     FileOptions tool8( tool2 );
00264     tool8 = tool;
00265     EQUAL( tool8.size(), tool.size() );
00266 
00267     return 0;
00268 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines