summaryrefslogtreecommitdiffstats
path: root/test/err_compat.c
blob: fd84824570c8232acb82afd6f742cfcb5f99064e (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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * 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 Error API
 */
#include "h5test.h"

#ifndef H5_WANT_H5_V1_6_COMPAT
int main(void)
{
    printf("Test skipped because backward compatbility with v1.6 is NOT configured in\n"); 
    return 0;
}
#else

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

#define DIM0    100
#define DIM1    200

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

#define DSET_NAME               "a_dataset"
#define FAKE_ID                 -1 

herr_t custom_print_cb(unsigned n, const H5E_error_t *err_desc, void* client_data);


/*-------------------------------------------------------------------------
 * Function:	test_error
 *
 * Purpose:	Test error API functions
 *
 * Return:	Success:	0
 *
 *		Failure:	-1
 *
 * Programmer:	Raymond Lu
 *		July 10, 2003
 *
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
#ifndef TMP
static herr_t
test_error(hid_t file)
{
    hid_t		dataset, space;
    hid_t               estack_id;
    hsize_t		dims[2];
    const char          *FUNC_test_error="test_error";
    H5E_auto_t          old_func;
    void                *old_data;
    
    TESTING("error API based on data I/O");
    fprintf(stderr, "\n");
    
    /* Create the data space */
    dims[0] = DIM0;
    dims[1] = DIM1;
    if ((space = H5Screate_simple(2, dims, NULL))<0) TEST_ERROR;

    /* Test H5E_BEGIN_TRY */
    H5E_BEGIN_TRY {
        dataset = H5Dcreate(FAKE_ID, DSET_NAME, H5T_STD_I32BE, space, H5P_DEFAULT);
    } H5E_END_TRY;
    
    /* Create the dataset */
    if ((dataset = H5Dcreate(file, DSET_NAME, H5T_STD_I32BE, space,
			     H5P_DEFAULT))<0) {
        H5Epush(__FILE__, FUNC_test_error, __LINE__, H5E_ERROR, H5E_CANTCREATE, 
                "H5Dcreate failed");
        goto error;
    }

    /* Test enabling and disabling default printing */
#ifndef TMP
    if (H5Eget_auto(&old_func, &old_data)<0)
	TEST_ERROR;
    if (old_data != NULL) 
	TEST_ERROR;
    if (!old_func) 
	TEST_ERROR;
    if (old_func != (H5E_auto_t)H5Eprint)
	TEST_ERROR;

    if(H5Eset_auto(NULL, NULL)<0)
        TEST_ERROR;
#endif

    /* Make H5Dwrite fail, verify default print is disabled */
    if (H5Dwrite(FAKE_ID, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, ipoints2)<0) {
        H5Epush(__FILE__, FUNC_test_error, __LINE__, H5E_ERROR, H5E_WRITEERROR, 
                "H5Dwrite shouldn't succeed");
        goto error;
    }

    if(H5Eset_auto(old_func, old_data)<0)
        TEST_ERROR;

    /* In case program comes to this point, close dataset */
    if(H5Dclose(dataset)<0) TEST_ERROR;
        
    TEST_ERROR; 

  error:
    return -1;
}
#endif


/*-------------------------------------------------------------------------
 * Function:	error_stack
 *
 * Purpose:     Dummy function.  Simply make it fail.	
 *
 * Return:	Success:	0
 *
 *		Failure:	-1
 *
 * Programmer:	Raymond Lu
 *		July 14, 2003
 *
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t 
error_stack(void)
{
    return -1;
}


/*-------------------------------------------------------------------------
 * Function:    dump_error	
 *
 * Purpose:	Prints error stack in default and customized ways.
 *
 * Return:	Success:	0
 *
 *		Failure:	-1
 *
 * Programmer:	Raymond Lu
 *		July 17, 2003
 *
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t 
dump_error(void)
{
    /* Print errors in library default way */
    fprintf(stderr, "********* Print error stack in HDF5 default way *********\n");
    if(H5Eprint(stderr)<0)
        TEST_ERROR;
    
    /* Customized way to print errors */
    fprintf(stderr, "\n********* Print error stack in customized way *********\n");
    if(H5Ewalk(H5E_WALK_UPWARD, custom_print_cb, stderr)<0)
        TEST_ERROR;
    
    return 0;

  error:
    return -1;
}

/*-------------------------------------------------------------------------
 * Function:    custom_print_cb
 *
 * Purpose:	Callback function to print error stack in customized way.
 *
 * Return:	Success:	0
 *
 *		Failure:	-1
 *
 * Programmer:	Raymond Lu
 *		July 17, 2003
 *
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t 
custom_print_cb(unsigned n, const H5E_error_t *err_desc, void* client_data)
{
    FILE		*stream  = (FILE *)client_data;
    char                *maj;
    char                *min;
    const int		indent = 4;

    if((min = H5Eget_minor(err_desc->min_num))==NULL)
        TEST_ERROR;

    if((maj = H5Eget_major(err_desc->maj_num))==NULL)
        TEST_ERROR;

    fprintf (stream, "%*serror #%03d: %s in %s(): line %u\n",
	     indent, "", n, err_desc->file_name,
	     err_desc->func_name, err_desc->line);
    fprintf (stream, "%*smajor: %s\n", indent*2, "", maj);
    fprintf (stream, "%*sminor: %s\n", indent*2, "", min);

    return 0;

  error:
    return -1;
}
    

/*-------------------------------------------------------------------------
 * 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="main";
    H5E_auto_t          old_func;
    void                *old_data;

    fprintf(stderr, "   This program tests the Error API compatible with HDF5 v1.6.  There're supposed to be some error messages\n");
    /*h5_reset();*/
    fapl = h5_fileaccess();
    
    h5_fixname(FILENAME[0], fapl, filename, sizeof filename);
    if ((file=H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl))<0)
	TEST_ERROR ;

    /* Test error stack */ 
    if(error_stack()<0) {
        /* Push an error onto error stack */
        H5Epush(__FILE__, FUNC_main, __LINE__, H5E_ERROR, H5E_BADVALUE, 
                "Error test failed");
        
        /* Print out the errors on stack */
        dump_error();

        /* Empty error stack */
        H5Eclear();
    }

    /* Test error API */
    if(test_error(file)<0) {
        H5Epush(__FILE__, FUNC_main, __LINE__, H5E_ERROR, H5E_BADMESG, 
                "Error test failed");
        H5Eprint(stderr);
    }

    if (H5Fclose(file)<0) TEST_ERROR ;
    h5_cleanup(FILENAME, fapl);

    printf("All error API tests passed.\n");
    return 0;

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