summaryrefslogtreecommitdiffstats
path: root/test/filename.c
blob: 9a4af5429241223a215b5d28d114e8f5d4fa840c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the files COPYING and Copyright.html.  COPYING can be found at the root   *
 * of the source code distribution tree; Copyright.html can be found at the  *
 * root level of an installed copy of the electronic HDF5 document set and   *
 * is linked from the top-level documents page.  It can also be found at     *
 * http://hdf.ncsa.uiuc.edu/HDF5/doc/Copyright.html.  If you do not have     *
 * access to either file, you may request a copy from hdfhelp@ncsa.uiuc.edu. *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 * Programmer:  Raymond Lu <slu@ncsa.uiuc.edu>
 *              June 29, 2004
 *
 * Purpose:     Tests the "H5Fget_name" functionality
 */

#include "hdf5.h"
#include "h5test.h"
#include "testhdf5.h"

#define FILENAME "get_file_name"
#define GROUPNAME "group"
#define DSETNAME "dataset"
#define ATTRNAME "attribute"
#define DTYPENAME "compound"
#define NAME_BUF_SIZE   64

#define RANK 2
#define NX 4
#define NY 5

/* Compound datatype */
typedef struct s1_t {
    unsigned int a;
    float        b;
} s1_t;

/* Used to make certain a return name _is_ the file name */
#ifdef H5_WANT_H5_V1_6_COMPAT
#define VERIFY_NAME(x, val, where) do {					      \
    if (GetTestVerbosity()>=VERBO_HI) {				              \
	print_func("   Call to routine: %15s at line %4d in %s had value "    \
		   "%ld \n", (where), (int)__LINE__, __FILE__, (long)(x));    \
    }									      \
    if (strcmp(x, val)) {					              \
	TestErrPrintf("*** UNEXPECTED VALUE from %s should be %s, but is %s at line %4d " \
		   "in %s\n", where, val, x, (int)__LINE__, __FILE__);        \
	H5Eprint (stdout);					              \
    }									      \
    HDstrcmp(x, "");                                                          \
} while(0)
#else /* H5_WANT_H5_V1_6_COMPAT */
#define VERIFY_NAME(x, val, where) do {					      \
    if (GetTestVerbosity()>=VERBO_HI) {				              \
	print_func("   Call to routine: %15s at line %4d in %s had value "    \
		   "%ld \n", (where), (int)__LINE__, __FILE__, (long)(x));    \
    }									      \
    if (strcmp(x, val)) {					              \
	TestErrPrintf("*** UNEXPECTED VALUE from %s should be %s, but is %s at line %4d " \
		   "in %s\n", where, val, x, (int)__LINE__, __FILE__);        \
	H5Eprint (H5E_DEFAULT, stdout);					      \
    }									      \
    HDstrcmp(x, "");                                                          \
} while(0)
#endif /* H5_WANT_H5_V1_6_COMPAT */

int main( void )
{
    char    filename[NAME_BUF_SIZE];
    hid_t   fapl;
    hid_t   file_id;
    hid_t   group_id;
    hid_t   dataset_id;
    hid_t   space_id;  
    hid_t   type_id;
    hid_t   attr_id;
    hsize_t dims[RANK] = {NX, NY};
    char    name[NAME_BUF_SIZE];
    ssize_t name_len;
    herr_t  ret;

    TESTING("H5Fget_name");
         
    /* Reset the library and get the file access property list */
    h5_reset();
    fapl = h5_fileaccess();

    /* Initialize the file names */
    h5_fixname(FILENAME, fapl, filename, sizeof filename);
     
    /* Create a new file_id using default properties. */
    file_id = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl );
    CHECK(file_id, FAIL, "H5Fcreate"); 

    /* Get and verify file name */
    name_len = H5Fget_name(file_id, name, NAME_BUF_SIZE);
    CHECK(name_len, FAIL, "H5Fget_name");
    VERIFY_NAME(name, filename, "H5Fget_name");

    /* Create a group in the root group */
    group_id = H5Gcreate(file_id, GROUPNAME, 0);
    CHECK(group_id, FAIL, "H5Gcreate"); 

    /* Get and verify file name */
    name_len = H5Fget_name(group_id, name, NAME_BUF_SIZE);
    CHECK(name_len, FAIL, "H5Fget_name");
    VERIFY_NAME(name, filename, "H5Fget_name");

    /* Create the data space  */
    space_id = H5Screate_simple(RANK, dims, NULL);
    CHECK(space_id, FAIL, "H5Screate_simple"); 

    /* Try get file name from data space.  Supposed to fail because 
     * it's illegal operation. */
    H5E_BEGIN_TRY {
        name_len = H5Fget_name(space_id, name, NAME_BUF_SIZE);
    } H5E_END_TRY;
    VERIFY(name_len, FAIL, "H5Fget_name");

    /* Create a new dataset */
    dataset_id = H5Dcreate(file_id, DSETNAME, H5T_NATIVE_INT, space_id, H5P_DEFAULT);
    CHECK(dataset_id, FAIL, "H5Dcreate"); 

    /* Get and verify file name */
    name_len = H5Fget_name(dataset_id, name, NAME_BUF_SIZE);
    CHECK(name_len, FAIL, "H5Fget_name");
    VERIFY_NAME(name, filename, "H5Fget_name");

    /* Create an attribute for the dataset */
    attr_id = H5Acreate(dataset_id,ATTRNAME,H5T_NATIVE_INT,space_id,H5P_DEFAULT);
    CHECK(attr_id, FAIL, "H5Acreate");

    /* Get and verify file name */
    name_len = H5Fget_name(attr_id, name, NAME_BUF_SIZE);
    CHECK(name_len, FAIL, "H5Fget_name");
    VERIFY_NAME(name, filename, "H5Fget_name");

    /* Create a compound datatype */
    type_id = H5Tcreate(H5T_COMPOUND, sizeof(s1_t));
    CHECK(type_id, FAIL, "H5Tcreate"); 

    /* Insert fields */
    ret = H5Tinsert (type_id, "a", HOFFSET(s1_t,a), H5T_NATIVE_INT);
    CHECK(ret, FAIL, "H5Tinsert"); 

    ret = H5Tinsert (type_id, "b", HOFFSET(s1_t,b), H5T_NATIVE_FLOAT);
    CHECK(ret, FAIL, "H5Tinsert"); 
  
    /* Save it on file */
    ret = H5Tcommit(file_id, DTYPENAME, type_id);
    CHECK(ret, FAIL, "H5Tcommit"); 

    /* Get and verify file name */
    name_len = H5Fget_name(type_id, name, NAME_BUF_SIZE);
    CHECK(name_len, FAIL, "H5Fget_name");
    VERIFY_NAME(name, filename, "H5Fget_name");

    H5Tclose(type_id);
    H5Aclose(attr_id);
    H5Dclose(dataset_id);
    H5Sclose(space_id);
    H5Gclose(group_id);
    H5Fclose(file_id);

    PASSED();
    return 0;
}