summaryrefslogtreecommitdiffstats
path: root/test/errors.c
blob: edffa2665078c6ff35b2668953703c8162838872 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * 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
 *              October 14, 2001	
 *
 * Purpose:	Tests the H5Tget_native_type function.
 */

#include "h5test.h"

const char *FILENAME[] = {
    "errors",
    NULL
};

#define DIM0    100
#define DIM1    200

int	ipoints2[DIM0][DIM1], icheck2[DIM0][DIM1];

hid_t   ERR_CLS;
hid_t   ERR_MAJ_TEST;
hid_t   ERR_MIN_SUBROUTINE;
hid_t   ERR_STACK;

#define DSET_NAME               "a_dataset"

#define ERR_CLS_NAME            "Error Test"
#define PROG_NAME               "Error Program"
#define PROG_VERS               "1.0"
#define ERR_MAJ_MSG             "Error in test"
#define ERR_MIN_MSG             "Error in subroutine"

#define SPACE1_DIM1             4
#define SPACE1_RANK             1
#define SPACE2_RANK	        2
#define SPACE2_DIM1	        10
#define SPACE2_DIM2	        10


/*-------------------------------------------------------------------------
 * Function:	test_error
 *
 * Purpose:	Test error API functions
 *
 * Return:	Success:	0
 *
 *		Failure:	-1
 *
 * Programmer:	Raymond Lu
 *		July 10, 2003
 *
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_error(hid_t file)
{
    hid_t		dataset, space;
    hid_t               dtype, native_type;
    int			i, j, n;
    hsize_t		dims[2];
    void                *tmp;

    TESTING("error API based on atomic datatype");

    /* Initialize the dataset */
    for (i = n = 0; i < DIM0; i++) {
	for (j = 0; j < DIM1; j++) {
	    ipoints2[i][j] = n++;
	}
    }

    /* Create the data space */
    dims[0] = DIM0;
    dims[1] = DIM1;
    if ((space = H5Screate_simple(2, dims, NULL))<0) TEST_ERROR;

    /*------------------- Test data values ------------------------*/
    /* Create the dataset */
    if ((dataset = H5Dcreate(file, DSET_NAME, H5T_STD_I32BE, space,
			     H5P_DEFAULT))<0) TEST_ERROR;

    /* Write the data to the dataset */
    if (H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, ipoints2)<0)
	TEST_ERROR;

    /* Close dataset */
    if(H5Dclose(dataset)<0) TEST_ERROR;

    /* Open dataset again to check H5Tget_native_type */
    if((dataset=H5Dopen(file, DSET_NAME))<0) TEST_ERROR;

    if((dtype=H5Dget_type(dataset))<0) TEST_ERROR;
    
    if((native_type=H5Tget_native_type(dtype, H5T_DIR_DEFAULT))<0)
        TEST_ERROR;

    /* Verify the datatype retrieved and converted */
    if(H5Tget_order(native_type) != H5Tget_order(H5T_NATIVE_INT)) 
        TEST_ERROR;
    if(H5Tget_size(native_type) < H5Tget_size(H5T_STD_I32BE))
        TEST_ERROR;
    if(H5T_INTEGER!=H5Tget_class(native_type))
        TEST_ERROR;

    /* Read the dataset back.  The temporary buffer is for special platforms 
     * like Cray. */
    tmp = malloc((size_t)(DIM0*DIM1*H5Tget_size(native_type)));
    
    if (H5Dread(dataset, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, tmp)<0)
	TEST_ERROR;
    
    /* Copy data from temporary buffer to destination buffer */
    memcpy(icheck2, tmp, (size_t)(DIM0*DIM1*H5Tget_size(native_type))); 
    free(tmp);
    
    /* Convert to the integer type */
    if(H5Tconvert(native_type, H5T_NATIVE_INT, (hsize_t)(DIM0*DIM1), icheck2, NULL, H5P_DEFAULT)<0)
        TEST_ERROR;
    
    /* Check that the values read are the same as the values written */
    for (i = 0; i < DIM0; i++) {
	for (j = 0; j < DIM1; j++) {
	    if (ipoints2[i][j] != icheck2[i][j]) {
		H5_FAILED();
		printf("    Read different values than written.\n");
		printf("    At index %d,%d\n", i, j);
		TEST_ERROR ;
	    }
	}
    }

    if(H5Dclose(dataset)<0) TEST_ERROR;
    if(H5Tclose(dtype)<0) TEST_ERROR;
    if(H5Sclose(space)<0) TEST_ERROR;
        
    PASSED();
    return 0;

  error:
    return -1;
}


/*-------------------------------------------------------------------------
 * Function:	init_error
 *
 * Purpose:	Initialize error information.
 *
 * Return:	Success:	0
 *
 *		Failure:	-1
 *
 * Programmer:	Raymond Lu
 *		July 10, 2003
 *
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
#ifndef NEW_ERR 
static herr_t 
init_error(void)
{
    size_t cls_size = strlen(ERR_CLS_NAME)+1;
    char *cls_name = malloc(strlen(ERR_CLS_NAME)+1);
    size_t msg_size = strlen(ERR_MIN_MSG) + 1;
    char *msg = malloc(strlen(ERR_MIN_MSG)+1);
    H5E_type_t *msg_type= malloc(sizeof(H5E_type_t));
    
    if((ERR_CLS = H5Eregister_class(ERR_CLS_NAME, PROG_NAME, PROG_VERS))<0)
        TEST_ERROR;

    if(cls_size != H5Eget_class(ERR_CLS, cls_name, cls_size) + 1) 
        TEST_ERROR;
    if(strcmp(ERR_CLS_NAME, cls_name)) 
        TEST_ERROR;
   
    if((ERR_MAJ_TEST = H5Ecreate_msg(ERR_CLS, H5E_MAJOR_new, ERR_MAJ_MSG))<0)
        TEST_ERROR;
    if((ERR_MIN_SUBROUTINE = H5Ecreate_msg(ERR_CLS, H5E_MINOR_new, ERR_MIN_MSG))<0)
        TEST_ERROR;

    if(msg_size != H5Eget_msg(ERR_MIN_SUBROUTINE, msg_type, msg, msg_size) + 1)
        TEST_ERROR;
    if(*msg_type != H5E_MINOR_new)
        TEST_ERROR;
    if(strcmp(msg, ERR_MIN_MSG))
        TEST_ERROR;

    free(cls_name);
    free(msg);
    free(msg_type);

    PASSED();
    return 0;

  error:
    return -1;
}


/*-------------------------------------------------------------------------
 * Function:	error_stack
 *
 * Purpose:	Manipulates current error stack.
 *
 * Return:	Success:	0
 *
 *		Failure:	-1
 *
 * Programmer:	Raymond Lu
 *		July 14, 2003
 *
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t 
error_stack(void)
{
    if((ERR_STACK = H5Eget_current_stack())<0)
        TEST_ERROR;

    if(H5Eclose_stack(ERR_STACK)<0)
        TEST_ERROR;
    
    PASSED();
    return 0;

  error:
    return -1;
}



/*-------------------------------------------------------------------------
 * Function:	close_error
 *
 * Purpose:	Closes error information.
 *
 * Return:	Success:	0
 *
 *		Failure:	-1
 *
 * Programmer:	Raymond Lu
 *		July 10, 2003
 *
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t 
close_error(void)
{
    /*
    if(H5Eclose_msg(ERR_MAJ_TEST)<0)
        TEST_ERROR ;
    if(H5Eclose_msg(ERR_MIN_SUBROUTINE)<0)
        TEST_ERROR ;
    */
    
    if(H5Eunregister_class(ERR_CLS)<0)
        TEST_ERROR ;

    PASSED();
    return 0;

  error:
    return -1;
}

#endif /* NEW_ERR */
    

/*-------------------------------------------------------------------------
 * Function:	main
 *
 * Purpose:	Test error API.
 *
 * Programmer:	Raymond Lu
 *		July 10, 2003
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
int
main(void)
{
    hid_t		file, fapl;
    char		filename[1024];
    const char          *FUNC="main()";
    
    h5_reset();
#ifndef NEW_ERR 
    if(init_error()<0)
        TEST_ERROR ;
#endif /* NEW_ERR */
    
    fapl = h5_fileaccess();
    
    h5_fixname(FILENAME[0], fapl, filename, sizeof filename);
    if ((file=H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl))<0)
	TEST_ERROR ;
    
#ifndef NEW_ERR 
    if(error_stack()<0)
        TEST_ERROR ;
#endif /* NEW_ERR */

    /*if(test_error(file)<0) {*/
#ifndef NEW_ERR
        /*H5Epush(H5E_DEFAULT, __FILE__, FUNC, __LINE__, ERR_MAJ_TEST, ERR_MIN_SUBROUTINE, "Error test failed");*/
#endif /* NEW_ERR */        
    /*    TEST_ERROR ;
    }*/
    
    if (H5Fclose(file)<0) TEST_ERROR ;
    h5_cleanup(FILENAME, fapl);

#ifndef NEW_ERR 
    if(close_error()<0)
        TEST_ERROR ;
#endif /* NEW_ERR */
    printf("All error API test based on native datatype test passed.\n");
    
    return 0;

 error:
    printf("***** ERROR TEST FAILED! *****\n");
    return 1;
}