/* -*- Mode: C; c-basic-offset:4 ; -*- */
/*  
 *  (C) 2004 by Argonne National Laboratory.
 *      See COPYRIGHT in top-level directory.
 */

/* This is a simple program that demonstrates (and tests) the use of the
   GMPI routines to work with MPI_Status elements */
#include "mpi.h"
#include <stdio.h>

int main( int argc, char *argv[] )
{
    MPI_Status *s;
    int size, rank, buf[2];

    MPI_Init( &argc, &argv );

    MPI_Comm_rank( MPI_COMM_WORLD, &rank );
    MPI_Comm_size( MPI_COMM_SIZE, &size );
    
    if (size < 2) {
	fprintf( stderr, "This test must be run with at least 2 processes\n" );
	MPI_Abort( MPI_COMM_WORLD, 1 );
    }
    
    if (rank == 1) {
	/* This process sends to process 0 */
	buf[0] = size;
	buf[1] = -1;
	MPI_Send( buf, 2, MPI_INT, 0, 17, MPI_COMM_WORLD );
    }
    else if (rank == 0) {
	int myTag, mySource, myCount;
	int errs = 0;

	s        = GMPI_Status_create( 1 );
	MPI_Recv( buf, 2, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, 
		  MPI_COMM_WORLD, s );
	myTag    = GMPI_Status_get_tag( s, 0 );
	mySource = GMPI_Status_get_source( s, 0 );
	myCount  = GMPI_Status_get_count( s, 0, MPI_INT );
	GMPI_Status_free( s );
	
	/* Check for the correct values */
	if (myTag != 17) {
	    errs ++;
	    fprintf( stderr, "Expected tag 17 but got %d\n", myTag );
	}
	if (mySource != 1) {
	    errs ++;
	    fprintf( stderr, "Expected source 1 but got %d\n", mySource );
	}
	if (myCount != 2) {
	    errs ++;
	    fprintf( stderr, "Expected count 2 but got %d\n", myCount );
	}
	/* Print summary of test */
	if (errs) {
	    printf( " Found %d error%c\n", errs, (errs > 1) ? 'c' : ' ' );
	}
	else {
	    printf( " No Errors\n" );
	}
    }
    MPI_Finalize();
    return 0;
}