ptest.cc

The classic way to read the parameter file.

#include <stdlib.h>
#include <iostream>

#include <testlib/testlib.h>

#include <paramxx/ParFile.h>

static int test_boolean( ParFile& pf ) {

  try {

    bool abool = pf.pgetb( "aboolean" );

    bool anotherbool;
    pf.pget( "aboolean", anotherbool );

    TEST( "boolean", abool, anotherbool );

    return EXIT_SUCCESS;

  } catch( ParFileException& pfe ) {

    cerr << pfe;
    return EXIT_FAILURE;

  } catch ( Exception& pfe ) {

    cerr << pfe;
    return EXIT_FAILURE;

  }

}

static int test_double( ParFile& pf ) {

  try {

    double adouble = pf.pgetd( "areal" );

    double anotherdouble;
    pf.pget( "areal", anotherdouble );

    TEST( "double", adouble, anotherdouble );

    return EXIT_SUCCESS;

  } catch( ParFileException& pfe ) {
    cerr << pfe;
    return EXIT_FAILURE;
  } catch ( Exception& pfe ) {

    cerr << pfe;
    return EXIT_FAILURE;

  }

}

static int test_int( ParFile& pf ) {

  try {

    int anint = pf.pgeti( "anint" );

    int anotherint;

    pf.pget( "anint", anotherint );

    TEST( "integer", anint, anotherint );

    return EXIT_SUCCESS;

  } catch ( ParFileException& pfe ) {

    cerr << pfe;
    return EXIT_FAILURE;

  } catch ( Exception& pfe ) {

    cerr << pfe;
    return EXIT_FAILURE;

  }

}

static int test_long( ParFile& pf ) {

  try {
    long along = pf.pgetl( "along" );

    long anotherlong;
    pf.pget( "along", anotherlong );

    TEST( "long", along, anotherlong );

    return EXIT_SUCCESS;

  } catch ( ParFileException& pfe ) {

    cerr << pfe;
    return EXIT_FAILURE;

  } catch ( Exception& pfe ) {

    cerr << pfe;
    return EXIT_FAILURE;

  }

}

static int test_string( ParFile& pf ) {


  try {

    string astring = pf.pgetstring( "astr" );
    
    string anotherstring;
    pf.pget( "astr", anotherstring );

    TESTSTRING( "string", astring.c_str( ), anotherstring.c_str( ) );

    return EXIT_SUCCESS;

  } catch ( ParFileException& pfe ) {

    cerr << pfe;
    return EXIT_FAILURE;

  } catch ( Exception& pfe ) {

    cerr << pfe;
    return EXIT_FAILURE;

  }

}

static int test( int argc, char** argv ) {

  try {
    ParFile pf( argc, argv );

    char str[ 256 ];
    pf.pgetstr( "foo", str, 256 );

    if ( EXIT_FAILURE == test_boolean( pf ) )
      return EXIT_FAILURE;

    if ( EXIT_FAILURE == test_int( pf ) )
      return EXIT_FAILURE;

    if ( EXIT_FAILURE == test_long( pf ) )
      return EXIT_FAILURE;

    if ( EXIT_FAILURE == test_string( pf ) )
      return EXIT_FAILURE;

    if ( EXIT_FAILURE == test_double( pf ) )
      return EXIT_FAILURE;

  } catch ( ParFileException& pfe ) {
    cerr << pfe;
    return EXIT_FAILURE;
  } catch ( Exception& e ) {
    cerr << e;
    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;

}

int main( int argc, char** argv ) {

  START( "Testing ParFile with indirrection" );

  if ( EXIT_FAILURE == test( argc, argv ) )
    return EXIT_FAILURE;

  SUMMARY();

  return EXIT_SUCCESS;
}

Generated on Thu Oct 2 17:54:19 2008 for paramxx by  doxygen 1.5.6