* This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.  *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/***********************************************************
*
* Test program:  tarray
* Test program:     tarray
*
* Test the Array Datatype functionality
*
*************************************************************/
#include "testhdf5.h"
#include "H5srcdir.h"
#include "hdf5.h"
#define FILENAME   "tarray1.h5"
#define TESTFILE   "tarrold.h5"
/* 1-D array datatype */
#define ARRAY1_RANK 1
#define ARRAY1_RANK    1
#define ARRAY1_DIM1 4
/* 3-D array datatype */
#define ARRAY2_RANK     3
#define ARRAY2_DIM1     3
#define ARRAY2_DIM2     4
#define ARRAY2_DIM3     5
/* 2-D array datatype */
#define ARRAY3_RANK     2
  char     *name[NMAX];
  size_t   offset[NMAX];
  hid_t    datatype[NMAX];
} CmpDTSinfo;
/* Forward declarations for custom vlen memory manager functions */
void *test_array_alloc_custom(size_t size, void *info);
void test_array_free_custom(void *mem, void *info);
/*-------------------------------------------------------------------------
 * Function:    test_array_atomic_1d
 *
 * Purpose:     Test basic array datatype code.
 *              Tests 1-D array of atomic datatypes.
 *
 * Return:      void
 *
 *-------------------------------------------------------------------------
 */
    /* Close Dataset */
    ret = H5Dclose(dataset);
    CHECK(ret, FAIL, "H5Dclose");
    /* Close file */
    ret = H5Fclose(fid1);
    CHECK(ret, FAIL, "H5Fclose");
} /* end test_array_atomic_1d() */
/*-------------------------------------------------------------------------
 * Function:    test_array_funcs
 *
 * Purpose:     Test some type functions that are and aren't supposed to
 *              work with array type.
 *
 * Return:      void
 *
 *-------------------------------------------------------------------------
 */
    H5E_BEGIN_TRY {
        strpad=H5Tget_strpad(type);
    } H5E_END_TRY;
    VERIFY(strpad, FAIL, "H5Tget_strpad");
    /* Close datatype */
    ret = H5Tclose(type);
    CHECK(ret, FAIL, "H5Tclose");
} /* end test_array_funcs() */
/*-------------------------------------------------------------------------
 * Function:    test_array_atomic_3d
 *
 * Purpose:     Test basic array datatype code.
 *              Tests 3-D array of atomic datatypes.
 *
 * Return:      void
 *
 *-------------------------------------------------------------------------
 */
    /* Close Dataset */
    ret = H5Dclose(dataset);
    CHECK(ret, FAIL, "H5Dclose");
    /* Close file */
    ret = H5Fclose(fid);
    CHECK(ret, FAIL, "H5Fclose");
} /* end test_array_atomic_3d() */
/*-------------------------------------------------------------------------
 * Function:    test_array_array_atomic
 *
 * Purpose:     Test basic array datatype code.
 *              Tests 1-D array 2-D arrays of atomic datatypes.
 *
 * Return:      void
 *
 *-------------------------------------------------------------------------
 */
    /* Close Dataset */
    ret = H5Dclose(dataset);
    CHECK(ret, FAIL, "H5Dclose");
    /* Close file */
    ret = H5Fclose(fid);
    CHECK(ret, FAIL, "H5Fclose");
} /* end test_array_array_atomic() */
/*-------------------------------------------------------------------------
 * Function:    test_array_compound_atomic
 *
 * Purpose:     Test basic array datatype code.
 *              Tests 1-D array of compound datatypes (with no array fields).
 *
 * Return:      void
 *
 *-------------------------------------------------------------------------
 */
    /* Close Dataset */
    ret = H5Dclose(dataset);
    CHECK(ret, FAIL, "H5Dclose");
    /* Close file */
    ret = H5Fclose(fid1);
    CHECK(ret, FAIL, "H5Fclose");
} /* end test_array_compound_atomic() */
/*-------------------------------------------------------------------------
 * Function:    test_array_compound_array
 *
 * Purpose:     Test basic array datatype code.
 *              Tests 1-D array of compound datatypes (with array fields).
 *
 * Return:      void
 *
 *-------------------------------------------------------------------------
 */
} /* end test_array_compound_array() */
/****************************************************************
**
**  test_array_alloc_custom(): Test VL datatype custom memory
**      allocation routines.  This routine just uses malloc to
**      allocate the memory and increments the amount of memory
**      allocated.
**
****************************************************************/
/*-------------------------------------------------------------------------
 * Function:    test_array_alloc_custom
 *
 * Purpose:     Memory allocator for testing VL datatype custom memory
 *              allocation routines.
 *
 *              This routine just uses malloc to allocate the memory and
 *              increments the amount of memory allocated.
 *
 * Return:
    if((ret_value = HDmalloc(extra+size)) != NULL) {
        *(size_t *)ret_value = size;
        *mem_used += size;
    } /* end if */
    ret_value = ((unsigned char *)ret_value) + extra;
    return ret_value;
} /* end test_array_alloc_custom() */
/*-------------------------------------------------------------------------
 * Function:    test_array_free_custom
 *
 * Purpose:     Memory free function for testing VL datatype custom memory
 *              allocation routines.
 *
 *              This routine just uses free to free the memory and
 *              decrements the amount of memory allocated.
 *
 * Return:      void
    if(_mem != NULL) {
        mem = ((unsigned char *)_mem) - extra;
        *mem_used -= *(size_t *)mem;
        HDfree(mem);
    } /* end if */
    return;
} /* end test_array_free_custom() */
/*-------------------------------------------------------------------------
 * Function:    test_array_vlen_atomic
 *
 * Purpose:     Test basic array datatype code.
 *              Tests 1-D array of atomic VL datatypes.
 *
 * Return:      void
 *
 *-------------------------------------------------------------------------
 */
    /* Close Dataset */
    ret = H5Dclose(dataset);
    CHECK(ret, FAIL, "H5Dclose");
    /* Close file */
    ret = H5Fclose(fid1);
    CHECK(ret, FAIL, "H5Fclose");
} /* end test_array_vlen_atomic() */
/*-------------------------------------------------------------------------
 * Function:    test_array_vlen_array
 *
 * Purpose:     Test basic array datatype code.
 *              Tests 1-D array of 1-D array VL datatypes.
 *
 * Return:      void
 *
 *-------------------------------------------------------------------------
 */
    /* Close Dataset */
    ret = H5Dclose(dataset);
    CHECK(ret, FAIL, "H5Dclose");
    /* Close file */
    ret = H5Fclose(fid1);
    CHECK(ret, FAIL, "H5Fclose");
} /* end test_array_vlen_array() */
/*-------------------------------------------------------------------------
 * Function:    test_array_bkg
 *
 * Purpose:     Test basic array datatype code.
 *              Tests reading compound datatype with array fields and
 *              writing partial fields.
 *
 * Return:      void
 *
 *-------------------------------------------------------------------------
    hid_t        type;
    hid_t        dataset;
    hsize_t      dim[] = {LENGTH};
    hsize_t      dima[] = {ALEN};
    int          i, j;
    unsigned     ndims[3] = {1,1,1};
    typedef struct {
      int      a[ALEN];
      float    b[ALEN];
      double   c[ALEN];
    int      a[ALEN];
    float    b[ALEN];
    double   c[ALEN];
    } CmpField;
    CmpField     cf[LENGTH];
    CmpField     cfr[LENGTH];
    CmpDTSinfo   *dtsinfo = NULL;
    typedef struct {
      float   b[ALEN];
    } fld_t;
    status = H5Tclose (type);
    CHECK(status, FAIL, "H5Tclose");
    status = H5Fclose(fid);
    CHECK(status, FAIL, "H5Fclose");
    HDfree(dtsinfo);
} /* end test_array_bkg() */
/*-------------------------------------------------------------------------
 * Function:    test_compat
 *
 * Purpose:     Test array datatype compatibility code.
 *
 *              Reads file containing old version of datatype object header
 *              messages for compound datatypes and verifies reading the older
 *              version of the is working correctly.
 *
 * Return:      void
        /* Close the file */
        ret = H5Fclose(fid1);
        CHECK_I(ret, "H5Fclose");
    } /* end if */
    else
        printf("***cannot open the pre-created compound datatype test file (%s)\n",testfile);
} /* end test_compat() */
/*-------------------------------------------------------------------------
 * Function:    test_array
 *
 * Purpose:     Main array datatype testing routine.
 *
 * Return:      void
 *
 *-------------------------------------------------------------------------
 */
void
    test_array_vlen_array();        /* Test 1-D array of 1-D array VL datatypes                             */
    test_array_funcs();             /* Test type functions with array types                                 */
    test_array_bkg();               /* Read compound datatype with array fields and background fields read  */
    /* This test uses a custom file */
    test_compat();                  /* Test compatibility changes for compound datatype fields              */
}   /* end test_array() */
/*-------------------------------------------------------------------------
 * Function:    cleanup_array
 *
 * Purpose:     Cleanup temporary test files
 *
 * Return:      void
 *
 * Programmer:  Quincey Koziol
 *              June 8, 1999
 *