MOAB: Mesh Oriented datABase  (version 5.3.1)
FileTokenizerTest.cpp
Go to the documentation of this file.
00001 #include "FileTokenizer.hpp"
00002 #include "MsqError.hpp"
00003 #include "cppunit/extensions/HelperMacros.h"
00004 #include <cassert>
00005 
00006 #include <iostream>
00007 using std::cout;
00008 
00009 #include <cstring>
00010 #include <cstdlib>
00011 #include <cstdio>
00012 #ifdef WIN32
00013 #include <string>
00014 #include <direct.h>
00015 #endif
00016 
00017 using MBMesquite::FileTokenizer;
00018 using MBMesquite::MsqPrintError;
00019 
00020 extern const char* const tokens[] = { "foo", "bar", "0", "123abc", "123", "0x5", "1e200", "jason", "abc123", "1.0", 0 };
00021 extern const char* const spaces[] = { " ", " \n ", "\n\n\n ", "\t", "\t\t", "     ", "\n", "  ", "\t\n", 0 };
00022 
00023 extern const bool doubles[]  = { false, false, true, false, true, false, true, false, false, true };
00024 extern const bool longs[]    = { false, false, true, false, true, true, false, false, false, false };
00025 extern const bool booleans[] = { false, false, true, false, false, false, false, false, false, false };
00026 
00027 class FileTokenizerTest : public CppUnit::TestFixture
00028 {
00029   private:
00030     CPPUNIT_TEST_SUITE( FileTokenizerTest );
00031     CPPUNIT_TEST( token_test );
00032     CPPUNIT_TEST( line_number_test );
00033     CPPUNIT_TEST( newline_test );
00034     CPPUNIT_TEST( match_one_test );
00035     CPPUNIT_TEST( match_multiple_test );
00036     CPPUNIT_TEST( double_test );
00037     CPPUNIT_TEST( long_test );
00038     CPPUNIT_TEST( boolean_test );
00039     CPPUNIT_TEST( unget_test );
00040     CPPUNIT_TEST_SUITE_END();
00041 
00042   public:
00043     FileTokenizerTest() {}
00044 
00045     void setUp() {}
00046 
00047     void tearDown() {}
00048 
00049     FILE* make_file()
00050     {
00051 #ifdef WIN32
00052         char name1[L_tmpnam_s];
00053         errno_t err = tmpnam_s( name1, L_tmpnam_s );
00054 
00055         // Get the current working directory:
00056         char* buffer;
00057         buffer = _getcwd( NULL, 0 );
00058         std::string full_path( buffer );
00059         std::string temp_name( name1 );
00060         full_path  = full_path + temp_name;
00061         FILE* file = fopen( full_path.c_str(), "w+" );
00062 #else
00063         FILE* file = tmpfile();
00064 #endif
00065 
00066         CPPUNIT_ASSERT( !!file );
00067         const char* const* t_iter = tokens;
00068         const char* const* s_iter = spaces;
00069         fputs( *t_iter, file );
00070         ++t_iter;
00071         while( *t_iter )
00072         {
00073             fputs( *s_iter, file );
00074             ++s_iter;
00075             fputs( *t_iter, file );
00076             ++t_iter;
00077         }
00078         rewind( file );
00079         return file;
00080     }
00081 
00082     void token_test()
00083     {
00084         MsqPrintError err( cout );
00085         const char* token;
00086 
00087         FileTokenizer ft( make_file() );
00088 
00089         for( const char* const* t_iter = tokens; *t_iter; ++t_iter )
00090         {
00091             token = ft.get_string( err );
00092             CPPUNIT_ASSERT( token );
00093             CPPUNIT_ASSERT( !err );
00094             CPPUNIT_ASSERT( !strcmp( *t_iter, token ) );
00095         }
00096 
00097         token = ft.get_string( err );
00098         CPPUNIT_ASSERT( !token );
00099         CPPUNIT_ASSERT( ft.eof() );
00100         err.clear();
00101     }
00102 
00103     static int count_newlines( const char* str )
00104     {
00105         int result = 0;
00106         for( ; *str; ++str )
00107             if( *str == '\n' ) ++result;
00108         return result;
00109     }
00110 
00111     void line_number_test()
00112     {
00113         MsqPrintError err( cout );
00114         const char* token;
00115 
00116         FileTokenizer ft( make_file() );
00117 
00118         int lineno = 1;
00119         token      = ft.get_string( err );
00120         CPPUNIT_ASSERT( token );
00121         CPPUNIT_ASSERT( !err );
00122 
00123         for( const char* const* s_iter = spaces; *s_iter; ++s_iter )
00124         {
00125             token = ft.get_string( err );
00126             CPPUNIT_ASSERT( token );
00127             CPPUNIT_ASSERT( !err );
00128 
00129             lineno += count_newlines( *s_iter );
00130             CPPUNIT_ASSERT( ft.line_number() == lineno );
00131         }
00132     }
00133 
00134     void newline_test()
00135     {
00136         MsqPrintError err( cout );
00137         const char* token;
00138 
00139         FileTokenizer ft( make_file() );
00140 
00141         token = ft.get_string( err );
00142         CPPUNIT_ASSERT( token );
00143         CPPUNIT_ASSERT( !err );
00144 
00145         for( const char* const* s_iter = spaces; *s_iter; ++s_iter )
00146         {
00147             int count = count_newlines( *s_iter );
00148             bool b;
00149             while( count-- )
00150             {
00151                 b = ft.get_newline( err );
00152                 CPPUNIT_ASSERT( b && !err );
00153             }
00154 
00155             b = ft.get_newline( err );
00156             CPPUNIT_ASSERT( !b && err );
00157             err.clear();
00158 
00159             token = ft.get_string( err );
00160             CPPUNIT_ASSERT( token );
00161             CPPUNIT_ASSERT( !err );
00162         }
00163     }
00164 
00165     void match_one_test()
00166     {
00167         MsqPrintError err( cout );
00168         const char* const* t_iter;
00169         bool b;
00170 
00171         FileTokenizer ft( make_file() );
00172 
00173         for( t_iter = tokens; *t_iter; ++t_iter )
00174         {
00175             b = ft.match_token( *t_iter, err );
00176             CPPUNIT_ASSERT( b && !err );
00177         }
00178 
00179         FileTokenizer ft2( make_file() );
00180 
00181         b = ft2.match_token( "", err );
00182         CPPUNIT_ASSERT( !b && err );
00183         err.clear();
00184 
00185         b = ft2.match_token( "Mesquite", err );
00186         CPPUNIT_ASSERT( !b && err );
00187         err.clear();
00188     }
00189 
00190     void match_multiple_test()
00191     {
00192         MsqPrintError err( cout );
00193         const char* const* t_iter = tokens;
00194 
00195         FileTokenizer ft( make_file() );
00196 
00197         int result;
00198         const char* const test1[] = { *t_iter, "Mesquite", "x", 0 };
00199         result                    = ft.match_token( test1, err );
00200         CPPUNIT_ASSERT( result == 1 && !err );
00201         ++t_iter;
00202 
00203         const char* const test2[] = { "x", "y", *t_iter, 0 };
00204         result                    = ft.match_token( test2, err );
00205         CPPUNIT_ASSERT( result == 3 && !err );
00206         ++t_iter;
00207 
00208         const char* const test3[] = { *t_iter, 0 };
00209         result                    = ft.match_token( test3, err );
00210         CPPUNIT_ASSERT( result == 1 && !err );
00211         ++t_iter;
00212 
00213         const char* const test4[] = { "Mesquite", "Mesh", 0 };
00214         result                    = ft.match_token( test4, err );
00215         CPPUNIT_ASSERT( result == 0 && err );
00216         err.clear();
00217         ++t_iter;
00218     }
00219 
00220     void double_test()
00221     {
00222         MsqPrintError err( cout );
00223         FileTokenizer ft( make_file() );
00224 
00225         for( int i = 0; tokens[i]; ++i )
00226         {
00227             double value;
00228             ft.get_doubles( 1, &value, err );
00229             if( doubles[i] )
00230             {
00231                 CPPUNIT_ASSERT( !err );
00232                 CPPUNIT_ASSERT( value == strtod( tokens[i], 0 ) );
00233             }
00234             else
00235             {
00236                 CPPUNIT_ASSERT( err );
00237                 err.clear();
00238             }
00239         }
00240     }
00241 
00242     void long_test()
00243     {
00244         MsqPrintError err( cout );
00245         FileTokenizer ft( make_file() );
00246 
00247         for( int i = 0; tokens[i]; ++i )
00248         {
00249             long value;
00250             ft.get_long_ints( 1, &value, err );
00251             if( longs[i] )
00252             {
00253                 CPPUNIT_ASSERT( !err );
00254                 CPPUNIT_ASSERT( value == strtol( tokens[i], 0, 0 ) );
00255             }
00256             else
00257             {
00258                 CPPUNIT_ASSERT( err );
00259                 err.clear();
00260             }
00261         }
00262     }
00263 
00264     void boolean_test()
00265     {
00266         MsqPrintError err( cout );
00267         FileTokenizer ft( make_file() );
00268 
00269         for( int i = 0; tokens[i]; ++i )
00270         {
00271             bool value;
00272             ft.get_booleans( 1, &value, err );
00273             if( booleans[i] )
00274             {
00275                 CPPUNIT_ASSERT( !err );
00276                 CPPUNIT_ASSERT( value == !!atoi( tokens[i] ) );
00277             }
00278             else
00279             {
00280                 CPPUNIT_ASSERT( err );
00281                 err.clear();
00282             }
00283         }
00284     }
00285 
00286     void unget_test()
00287     {
00288         MsqPrintError err( cout );
00289         FileTokenizer ft( make_file() );
00290 
00291         const char* const* t_iter = tokens;
00292         const char* token         = ft.get_string( err );
00293         CPPUNIT_ASSERT( !err );
00294         CPPUNIT_ASSERT( !strcmp( token, *t_iter ) );
00295 
00296         ft.unget_token();
00297         token = ft.get_string( err );
00298         CPPUNIT_ASSERT( !err );
00299         CPPUNIT_ASSERT( !strcmp( token, *t_iter ) );
00300 
00301         ++t_iter;
00302         token = ft.get_string( err );
00303         CPPUNIT_ASSERT( !err );
00304         CPPUNIT_ASSERT( !strcmp( token, *t_iter ) );
00305 
00306         ++t_iter;
00307         token = ft.get_string( err );
00308         CPPUNIT_ASSERT( !err );
00309         CPPUNIT_ASSERT( !strcmp( token, *t_iter ) );
00310 
00311         ft.unget_token();
00312         token = ft.get_string( err );
00313         CPPUNIT_ASSERT( !err );
00314         CPPUNIT_ASSERT( !strcmp( token, *t_iter ) );
00315     }
00316 };
00317 
00318 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileTokenizerTest, "FileTokenizerTest" );
00319 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileTokenizerTest, "Unit" );
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines