/*
 *  Copyright (C) 2013, Northwestern University and Argonne National Laboratory
 *  See COPYRIGHT notice in top-level directory.
 */
/* $Id$ */

/* This program tests if PnetCDF can report correct file formats */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h> /* basename() */
#include <unistd.h> /* getopt() */

#include <mpi.h>
#include <pnetcdf.h>
#include <testutils.h>

static
int test_io(const char *out_path, /* ignored */
            const char *in_path,
            int         format,
            int         coll_io,
            MPI_Info    info)
{
    char filename[512];
    int err, nerrs=0, fmt, ncid;

    sprintf(filename,"%s/test_cdf.nc%d",in_path, format);
    // printf("%s at %d: input filename %s\n",basename(__FILE__),__LINE__,filename);

    err = ncmpi_open(MPI_COMM_WORLD, filename, NC_NOWRITE, MPI_INFO_NULL,
                     &ncid);
    if (format == NC_FORMAT_NETCDF4 && PNETCDF_DRIVER_NETCDF4 == 0) {
        EXP_ERR(NC_ENOTBUILT)
        return 0;
    }
    else {
        CHECK_ERR
        if (err != NC_NOERR) {
            int rank;
            MPI_Comm_rank(MPI_COMM_WORLD, &rank);
            fprintf(stderr,"Error in %s at %d: rank %d failed to open file %s\n",
                   basename(__FILE__), __LINE__, rank, filename);
            return 1;
        }
    }

    /* test NULL argument */
    err = ncmpi_inq_format(ncid, NULL); CHECK_ERR

    err = ncmpi_inq_format(ncid, &fmt); CHECK_ERR

    if (fmt != format) {
        fprintf(stderr,"Error in %s at %d: expect CDF-%d format for file %s but got %d\n",
               __FILE__,__LINE__,format,filename,fmt);
        nerrs++;
    }
    err = ncmpi_close(ncid); CHECK_ERR

    /* test NULL argument */
    err = ncmpi_inq_file_format(filename, NULL); CHECK_ERR

    err = ncmpi_inq_file_format(filename, &fmt); CHECK_ERR

    if (fmt != format) {
        fprintf(stderr,"Error in %s at %d: expect CDF-%d format for file %s but got %d\n",
               __FILE__,__LINE__,format,filename,fmt);
        nerrs++;
    }

    return nerrs;
}

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

    int err;
    loop_opts opt;

    MPI_Init(&argc, &argv);

    opt.num_fmts = sizeof(nc_formats) / sizeof(int);
    opt.formats  = nc_formats;
    opt.ina      = 0;    /* skip test intra-node aggregation */
    opt.drv      = 2;    /* test GIO and MPI-IO driver */
    opt.ibuf     = 0;    /* test default hint nc_ibuf_size only */
    opt.bb       = 0;    /* skip test burst-buffering feature */
    opt.mod      = 2;    /* collective and independent data mode */
    opt.hdr_diff = false;/* skip ncmpidiff for file header */
    opt.var_diff = false;/* skip ncmpidiff for variables */

    err = tst_main(argc, argv, "inquiring file formats", opt, test_io);

    MPI_Finalize();

    return err;
}

