Blame test/mpi/errors/cxx/io/fileerrretx.cxx

Packit 0848f5
/* -*- Mode: C++; c-basic-offset:4 ; -*- */
Packit 0848f5
/*
Packit 0848f5
 *
Packit 0848f5
 *  (C) 2006 by Argonne National Laboratory.
Packit 0848f5
 *      See COPYRIGHT in top-level directory.
Packit 0848f5
 */
Packit 0848f5
#include "mpi.h"
Packit 0848f5
#include "mpitestconf.h"
Packit 0848f5
#ifdef HAVE_IOSTREAM
Packit 0848f5
// Not all C++ compilers have iostream instead of iostream.h
Packit 0848f5
#include <iostream>
Packit 0848f5
#ifdef HAVE_NAMESPACE_STD
Packit 0848f5
// Those that do often need the std namespace; otherwise, a bare "cout"
Packit 0848f5
// is likely to fail to compile
Packit 0848f5
using namespace std;
Packit 0848f5
#endif
Packit 0848f5
#else
Packit 0848f5
#include <iostream.h>
Packit 0848f5
#endif
Packit 0848f5
Packit 0848f5
#ifdef HAVE_STRING_H
Packit 0848f5
#include <string.h>
Packit 0848f5
#endif
Packit 0848f5
#include "mpitestcxx.h"
Packit 0848f5
Packit 0848f5
static int verbose = 0;
Packit 0848f5
Packit 0848f5
static int ncalls = 0;
Packit 0848f5
void efn( MPI::File &fh, int *code, ... )
Packit 0848f5
{
Packit 0848f5
    ncalls ++;
Packit 0848f5
}
Packit 0848f5
Packit 0848f5
int main( int argc, char *argv[] )
Packit 0848f5
{
Packit 0848f5
    MPI::File fh;
Packit 0848f5
    MPI::Errhandler eh;
Packit 0848f5
    char *filename;
Packit 0848f5
    int errs = 0, toterrs, rank;
Packit 0848f5
    int sawErr;
Packit 0848f5
Packit 0848f5
    MPI::Init();
Packit 0848f5
    // Test that the default error handler is errors return for files
Packit 0848f5
Packit 0848f5
    filename = new char[10];
Packit 0848f5
    strncpy( filename, "t1", 10 );
Packit 0848f5
Packit 0848f5
    MPI::FILE_NULL.Set_errhandler(MPI::ERRORS_THROW_EXCEPTIONS);
Packit 0848f5
    sawErr = 0;
Packit 0848f5
    try {
Packit 0848f5
	fh = MPI::File::Open(MPI::COMM_WORLD, filename, 
Packit 0848f5
			 MPI::MODE_RDWR, MPI::INFO_NULL );
Packit 0848f5
    } catch (MPI::Exception ex) {
Packit 0848f5
	if (verbose) cout << "Caught exception from open\n";
Packit 0848f5
	if (ex.Get_error_class() == MPI_SUCCESS) {
Packit 0848f5
	    errs++;
Packit 0848f5
	    cout << "Unexpected error from Open" << endl;
Packit 0848f5
	}
Packit 0848f5
	sawErr = 1;
Packit 0848f5
    }
Packit 0848f5
    if (!sawErr) {
Packit 0848f5
	errs++;
Packit 0848f5
	cout << "Did not see error when opening a non-existent file for writing and reading (without MODE_CREATE)\n";
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    // Test that we can change the default error handler by changing
Packit 0848f5
    // the error handler on MPI::FILE_NULL.
Packit 0848f5
    eh = MPI::File::Create_errhandler( efn );
Packit 0848f5
    MPI::FILE_NULL.Set_errhandler( eh );
Packit 0848f5
    eh.Free();
Packit 0848f5
    sawErr = 0;
Packit 0848f5
    try {
Packit 0848f5
	fh = MPI::File::Open(MPI::COMM_WORLD, filename, 
Packit 0848f5
			 MPI::MODE_RDWR, MPI::INFO_NULL );
Packit 0848f5
    } catch (MPI::Exception ex) {
Packit 0848f5
	cout << "Caught exception from open (should have called error handler instead)\n";
Packit 0848f5
	errs++;
Packit 0848f5
	if (ex.Get_error_class() == MPI_SUCCESS) {
Packit 0848f5
	    errs++;
Packit 0848f5
	    cout << "Unexpected error from Open" << endl;
Packit 0848f5
	}
Packit 0848f5
	sawErr = 1;
Packit 0848f5
    }
Packit 0848f5
    if (ncalls != 1) {
Packit 0848f5
	errs++;
Packit 0848f5
	cout << "Did not invoke error handler when opening a non-existent file for writing and reading (without MODE_CREATE)" << endl;
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    // Find out how many errors we saw
Packit 0848f5
    MPI::COMM_WORLD.Allreduce( &errs, &toterrs, 1, MPI::INT, MPI::SUM );
Packit 0848f5
    if (MPI::COMM_WORLD.Get_rank() == 0) {
Packit 0848f5
	if (toterrs == 0) {
Packit 0848f5
	    cout << " No Errors" << endl;
Packit 0848f5
	}
Packit 0848f5
	else {
Packit 0848f5
	    cout << " Saw " << toterrs << " errors" << endl;
Packit 0848f5
	}
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    delete[] filename;
Packit 0848f5
    MPI::Finalize();
Packit 0848f5
Packit 0848f5
    return 0;
Packit 0848f5
}