summaryrefslogtreecommitdiffstats
path: root/test/extend.c
blob: 0c28c711d2e0c48f7e8247c08fc58d73584f328a (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
353
354
355
356
357
358
359
360
361
362
363
364
365
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * 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 COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://www.hdfgroup.org/licenses.               *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 * Programmer:  Robb Matzke
 *              Friday, January 30, 1998
 *
 * Purpose:	Tests extendible datasets.
 */

#include "h5test.h"

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

#define N1X 100 /* USE AN EVEN NUMBER! */
#define N1Y 100 /* USE AN EVEN NUMBER! */
#define N2X (N1X / 2)
#define N2Y (N1Y / 2)

/* Data buffers */
static int **buf1      = NULL;
static int  *buf1_data = NULL;
static int **buf2      = NULL;
static int  *buf2_data = NULL;

/*-------------------------------------------------------------------------
 * Function:	write_data
 *
 * Purpose:	Create extendible dataset and test extend/write/read
 *
 * Return:	Success:	0
 *		Failure:	-1
 *
 * Programmer:	Quincey Koziol
 *              Tuesday, June 10, 2003
 *
 *-------------------------------------------------------------------------
 */
static int
write_data(const char *msg, hid_t file, const char *name, hid_t cparms, hid_t mem_space)
{
    hid_t                dataset, file_space, half_space;
    static const hsize_t dims[2]      = {N1X, N1Y};
    static const hsize_t half_dims[2] = {N2X, N2Y};
    hsize_t              size[2];
    hsize_t              max_size[2] = {0, 0};
    hsize_t              offset[2];
    int                  i, j, k, m;

    TESTING(msg);

    /* Create the dataset */
    if ((dataset = H5Dcreate2(file, name, H5T_NATIVE_INT, mem_space, H5P_DEFAULT, cparms, H5P_DEFAULT)) < 0)
        TEST_ERROR;

    /* Write the data */
    for (i = 0; i < 5; i++)
        for (j = 0; j < 5; j++) {

            /* Extend the dataset */
            offset[0] = (hsize_t)(i * N1X);
            offset[1] = (hsize_t)(j * N1Y);
            size[0]   = offset[0] + N1X;
            size[1]   = offset[1] + N1Y;
            if (size[0] > max_size[0] || size[1] > max_size[1]) {
                if (size[0] > max_size[0])
                    max_size[0] = size[0];
                if (size[1] > max_size[1])
                    max_size[1] = size[1];
                if (H5Dset_extent(dataset, max_size) < 0)
                    TEST_ERROR;
            } /* end if */

            /* Select a hyperslab */
            if ((file_space = H5Dget_space(dataset)) < 0)
                TEST_ERROR;
            if (H5Sselect_hyperslab(file_space, H5S_SELECT_SET, offset, NULL, dims, NULL) < 0)
                TEST_ERROR;

            /* Write to the hyperslab */
            if (H5Dwrite(dataset, H5T_NATIVE_INT, mem_space, file_space, H5P_DEFAULT, buf1_data) < 0)
                TEST_ERROR;
            if (H5Sclose(file_space) < 0)
                TEST_ERROR;
        } /* end for */

    /* Read the data */
    if ((half_space = H5Screate_simple(2, half_dims, NULL)) < 0)
        TEST_ERROR;
    if ((file_space = H5Dget_space(dataset)) < 0)
        TEST_ERROR;
    for (i = 0; i < 10; i++) {
        for (j = 0; j < 10; j++) {

            /* Select a hyperslab */
            offset[0] = (hsize_t)(i * N2X);
            offset[1] = (hsize_t)(j * N2Y);
            if (H5Sselect_hyperslab(file_space, H5S_SELECT_SET, offset, NULL, half_dims, NULL) < 0)
                TEST_ERROR;

            /* Read */
            if (H5Dread(dataset, H5T_NATIVE_INT, half_space, file_space, H5P_DEFAULT, buf2_data) < 0)
                TEST_ERROR;

            /* Compare */
            for (k = 0; k < N2X; k++)
                for (m = 0; m < N2Y; m++)
                    if (buf2[k][m] != buf1[(i % 2) * N2X + k][(j % 2) * N2Y + m]) {
                        HDprintf("    i=%d, j=%d, k=%d, m=%d\n", i, j, k, m);
                        HDprintf("    buf2[%d][%d]=%d\n", k, m, buf2[k][m]);
                        HDprintf("    buf1[%d][%d]=%d\n", (i % 2) * N2X + k, (j % 2) * N2Y + m,
                                 buf1[(i % 2) * N2X + k][(j % 2) * N2Y + m]);
                        TEST_ERROR;
                    } /* end if */
        }             /* end for */
    }                 /* end for */

    /* Cleanup */
    if (H5Dclose(dataset) < 0)
        TEST_ERROR;
    if (H5Sclose(file_space) < 0)
        TEST_ERROR;
    if (H5Sclose(half_space) < 0)
        TEST_ERROR;

    PASSED();
    return 0;

error:
    return -1;
} /* end write_data() */

#ifndef H5_NO_DEPRECATED_SYMBOLS

/*-------------------------------------------------------------------------
 * Function:	write_data_deprec
 *
 * Purpose:	Create extendible dataset and test extend/write/read, with
 *              deprecated API routine (H5Dextend)
 *
 * Return:	Success:	0
 *		Failure:	-1
 *
 * Programmer:	Quincey Koziol
 *              Monday, October 8, 2007
 *
 *-------------------------------------------------------------------------
 */
static int
write_data_deprec(const char *msg, hid_t file, const char *name, hid_t cparms, hid_t mem_space)
{
    hid_t                dataset, file_space, half_space;
    static const hsize_t dims[2]      = {N1X, N1Y};
    static const hsize_t half_dims[2] = {N2X, N2Y};
    static hsize_t       size[2];
    hsize_t              offset[2];
    int                  i, j, k, m;

    TESTING(msg);

    /* Create the dataset */
    if ((dataset = H5Dcreate2(file, name, H5T_NATIVE_INT, mem_space, H5P_DEFAULT, cparms, H5P_DEFAULT)) < 0)
        TEST_ERROR;

    /* Write the data */
    for (i = 0; i < 5; i++)
        for (j = 0; j < 5; j++) {

            /* Extend the dataset */
            offset[0] = (hsize_t)(i * N1X);
            offset[1] = (hsize_t)(j * N1Y);
            size[0]   = offset[0] + N1X;
            size[1]   = offset[1] + N1Y;
            if (H5Dextend(dataset, size) < 0)
                TEST_ERROR;

            /* Select a hyperslab */
            if ((file_space = H5Dget_space(dataset)) < 0)
                TEST_ERROR;
            if (H5Sselect_hyperslab(file_space, H5S_SELECT_SET, offset, NULL, dims, NULL) < 0)
                TEST_ERROR;

            /* Write to the hyperslab */
            if (H5Dwrite(dataset, H5T_NATIVE_INT, mem_space, file_space, H5P_DEFAULT, buf1_data) < 0)
                TEST_ERROR;
            if (H5Sclose(file_space) < 0)
                TEST_ERROR;
        } /* end for */

    /* Read the data */
    if ((half_space = H5Screate_simple(2, half_dims, NULL)) < 0)
        TEST_ERROR;
    if ((file_space = H5Dget_space(dataset)) < 0)
        TEST_ERROR;
    for (i = 0; i < 10; i++) {
        for (j = 0; j < 10; j++) {

            /* Select a hyperslab */
            offset[0] = (hsize_t)(i * N2X);
            offset[1] = (hsize_t)(j * N2Y);
            if (H5Sselect_hyperslab(file_space, H5S_SELECT_SET, offset, NULL, half_dims, NULL) < 0)
                TEST_ERROR;

            /* Read */
            if (H5Dread(dataset, H5T_NATIVE_INT, half_space, file_space, H5P_DEFAULT, buf2_data) < 0)
                TEST_ERROR;

            /* Compare */
            for (k = 0; k < N2X; k++)
                for (m = 0; m < N2Y; m++)
                    if (buf2[k][m] != buf1[(i % 2) * N2X + k][(j % 2) * N2Y + m]) {
                        HDprintf("    i=%d, j=%d, k=%d, m=%d\n", i, j, k, m);
                        HDprintf("    buf2[%d][%d]=%d\n", k, m, buf2[k][m]);
                        HDprintf("    buf1[%d][%d]=%d\n", (i % 2) * N2X + k, (j % 2) * N2Y + m,
                                 buf1[(i % 2) * N2X + k][(j % 2) * N2Y + m]);
                        TEST_ERROR;
                    } /* end if */
        }             /* end for */
    }                 /* end for */

    /* Cleanup */
    if (H5Dclose(dataset) < 0)
        TEST_ERROR;
    if (H5Sclose(file_space) < 0)
        TEST_ERROR;
    if (H5Sclose(half_space) < 0)
        TEST_ERROR;

    PASSED();
    return 0;

error:
    return -1;
} /* end write_data_deprec() */
#endif /* H5_NO_DEPRECATED_SYMBOLS */

/*-------------------------------------------------------------------------
 * Function:    main
 *
 * Purpose:     Tests extendible datasets
 *
 * Return:      EXIT_SUCCESS/EXIT_FAILURE
 *
 * Programmer:	Robb Matzke
 *              Friday, January 30, 1998
 *
 *-------------------------------------------------------------------------
 */
int
main(void)
{
    hid_t                file, mem_space, cparms;
    hid_t                fapl;
    int                  nerrors       = 0;
    static const hsize_t dims[2]       = {N1X, N1Y};
    static const hsize_t chunk_dims[2] = {N2X, N2Y};
    static hsize_t       maxdims[2]    = {H5S_UNLIMITED, H5S_UNLIMITED};
    char                 filename[1024];
    int                  i, j;

    h5_reset();
    fapl = h5_fileaccess();

    /* Initialize buffer and space */
    if (NULL == (buf1_data = (int *)HDcalloc(N1X * N1Y, sizeof(int))))
        TEST_ERROR;
    if (NULL == (buf1 = (int **)HDcalloc(N1X, sizeof(buf1_data))))
        TEST_ERROR;
    for (i = 0; i < N1X; i++)
        buf1[i] = buf1_data + (i * N1Y);

    if (NULL == (buf2_data = (int *)HDcalloc(N2X * N2Y, sizeof(int))))
        TEST_ERROR;
    if (NULL == (buf2 = (int **)HDcalloc(N2X, sizeof(buf2_data))))
        TEST_ERROR;
    for (i = 0; i < N2X; i++)
        buf2[i] = buf2_data + (i * N2Y);

    for (i = 0; i < N1X; i++)
        for (j = 0; j < N1Y; j++)
            buf1[i][j] = i * N1Y + j;

    if ((mem_space = H5Screate_simple(2, dims, maxdims)) < 0)
        TEST_ERROR;

    /* Create the file */
    h5_fixname(FILENAME[0], fapl, filename, sizeof filename);
    if ((file = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0)
        TEST_ERROR;

    /* Create the dataset creation property list */
    if ((cparms = H5Pcreate(H5P_DATASET_CREATE)) < 0)
        TEST_ERROR;
    if (H5Pset_chunk(cparms, 2, chunk_dims) < 0)
        TEST_ERROR;

    /* Test with incremental allocation */
    nerrors +=
        write_data("extendible dataset with incr. allocation", file, "dataset1a", cparms, mem_space) < 0 ? 1
                                                                                                         : 0;
#ifndef H5_NO_DEPRECATED_SYMBOLS
    nerrors += write_data_deprec("extendible dataset with incr. allocation w/deprec. symbols", file,
                                 "dataset1b", cparms, mem_space) < 0
                   ? 1
                   : 0;
#endif /* H5_NO_DEPRECATED_SYMBOLS */

    /* Test with early allocation */
    if (H5Pset_alloc_time(cparms, H5D_ALLOC_TIME_EARLY) < 0)
        TEST_ERROR;
    nerrors +=
        write_data("extendible dataset with early allocation", file, "dataset2a", cparms, mem_space) < 0 ? 1
                                                                                                         : 0;
#ifndef H5_NO_DEPRECATED_SYMBOLS
    nerrors += write_data_deprec("extendible dataset with early allocation w/deprec. symbols", file,
                                 "dataset2b", cparms, mem_space) < 0
                   ? 1
                   : 0;
#endif /* H5_NO_DEPRECATED_SYMBOLS */

    if (H5Pclose(cparms) < 0)
        TEST_ERROR;
    if (H5Sclose(mem_space) < 0)
        TEST_ERROR;
    if (H5Fclose(file) < 0)
        TEST_ERROR;

    /* Verify symbol table messages are cached */
    nerrors += (h5_verify_cached_stabs(FILENAME, fapl) < 0 ? 1 : 0);

    if (nerrors) {
        HDprintf("***** %d FAILURE%s! *****\n", nerrors, (1 == nerrors) ? "" : "S");
        HDexit(EXIT_FAILURE);
    } /* end if */

    HDprintf("All extend tests passed.\n");
    h5_cleanup(FILENAME, fapl);

    HDfree(buf1);
    HDfree(buf1_data);
    HDfree(buf2);
    HDfree(buf2_data);

    HDexit(EXIT_SUCCESS);

error:

    HDfree(buf1);
    HDfree(buf1_data);
    HDfree(buf2);
    HDfree(buf2_data);

    HDprintf("*** One or more extend tests failed ***\n");
    HDexit(EXIT_FAILURE);
} /* end main() */