File: extelt.c

package info (click to toggle)
libhdf4 4.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 30,384 kB
  • sloc: ansic: 128,700; sh: 15,015; fortran: 12,444; java: 5,863; xml: 1,205; makefile: 794; yacc: 678; pascal: 418; perl: 360; javascript: 203; lex: 163; csh: 41
file content (404 lines) | stat: -rw-r--r-- 14,423 bytes parent folder | download | duplicates (2)
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF.  The full HDF 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/HDF/releases/.  *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 * These is a first pass at rewriting how these tests for exteranl
 * elements were done -GV
 */
#include "tproto.h"
#define TESTFILE_NAME  "t.hdf"                  /* file for first 4 series of tests */
#define TESTFILE_NAME1 "tx.hdf"                 /* file for last test */
#define STRING         "element 1000 2"         /* 14 bytes */
#define STRING2        "element 1000 1   wrong" /* 22 bytes */
#define STRING3        "element 1000 1 correct" /* 22 bytes */

#define BUF_SIZE 4096

static uint8 *outbuf = NULL;
static uint8 *inbuf  = NULL;

void
test_hextelt(void)
{
    int32  fid, fid1;
    int32  aid1, aid2;
    int32  fileid, length, offset, posn;
    uint16 tag, ref;
    int16  acc_mode, special;
    int    i;
    int32  ret;
    intn   errflag = 0;
    intn   errors  = 0;

    outbuf = (uint8 *)calloc(BUF_SIZE, sizeof(uint8));
    inbuf  = (uint8 *)calloc(BUF_SIZE, sizeof(uint8));

    CHECK_ALLOC(outbuf, "outbuf", "test_hextelt");
    CHECK_ALLOC(inbuf, "outbuf", "test_hextelt");

    /* Initialize buffer */
    for (i = 0; i < BUF_SIZE; i++)
        outbuf[i] = (char)(i % 256);

    /* Create header file */
    MESSAGE(5, printf("Creating base file %s\n", TESTFILE_NAME););

    fid = Hopen(TESTFILE_NAME, DFACC_CREATE, 0);
    CHECK_VOID(fid, FAIL, "Hopen");

    /* Write first object to header file */
    MESSAGE(5, printf("Writing object(%lu bytes) into base file\n", (unsigned long)strlen(STRING2)););
    ret = Hputelement(fid, (uint16)1000, (uint16)1, (const uint8 *)STRING2, (int32)strlen(STRING2) + 1);
    CHECK_VOID(ret, FAIL, "Hputelement");

    /* Promote the above object to an external object */
    MESSAGE(5, printf("Promoting above object to external element in file #1\n"););
    aid1 = HXcreate(fid, 1000, 1, "t1.hdf", (int32)0, (int32)0);
    CHECK_VOID(aid1, FAIL, "HXcreate");

    ret = Hseek(aid1, (int32)strlen("element 1000 1") + 1, DF_START);
    CHECK_VOID(ret, FAIL, "Hseek");

    /* Now verify that the new promoted object can be written to */
    MESSAGE(5, printf("Writing to promoted object now in file #1 \n"););

    ret = Hwrite(aid1, (int32)strlen("correct") + 1, "correct");
    if (ret != (int32)strlen("correct") + 1) {
        fprintf(stderr, "Hwrite failed (code %d)\n", (int)ret);
        HEprint(stderr, 0);
        errors++;
    }

    ret = Hendaccess(aid1);
    CHECK_VOID(ret, FAIL, "Hendaccess");

    /* Create a new external object of size 2000 bytes in a separate file */
    MESSAGE(5, printf("Creating an external element in file #2\n"););
    aid1 = HXcreate(fid, 1000, 4, "t2.hdf", (int32)0, (int32)0);
    CHECK_VOID(aid1, FAIL, "HXcreate");

    MESSAGE(5, printf("Writing 2000 bytes to file #2\n"););
    ret = Hwrite(aid1, 2000, outbuf);
    CHECK_VOID(ret, FAIL, "Hwrite");

    ret = Hendaccess(aid1);
    CHECK_VOID(ret, FAIL, "Hendaccess");

    /* Create a new external string object  in a separate file */
    MESSAGE(5, printf("Creating an external element in file #3\n"););
    aid1 = HXcreate(fid, 1000, 2, "t3.hdf", (int32)0, (int32)0);
    CHECK_VOID(aid1, FAIL, "HXcreate");

    MESSAGE(5, printf("Writing string '%s'(%lu bytes) to file #3\n", STRING, (unsigned long)strlen(STRING)););
    ret = Hwrite(aid1, (int32)strlen(STRING) + 1, STRING);
    if (ret != (int32)strlen(STRING) + 1) {
        fprintf(stderr, "Hwrite failed (code %d)\n", (int)ret);
        HEprint(stderr, 0);
        errors++;
    }

    ret = Hendaccess(aid1);
    CHECK_VOID(ret, FAIL, "Hendaccess");

    /* Create a new external object that points to part of an existing element */
    MESSAGE(5, printf("Creating an overlapping element that already exists in file #3\n"););
    aid2 = HXcreate(fid, 1001, 2, "t3.hdf", (int32)8, (int32)4);
    CHECK_VOID(aid2, FAIL, "HXcreate");

    ret = Hendaccess(aid2);
    CHECK_VOID(ret, FAIL, "Hendaccess");

    /* Create a new external object of size 4096 bytes */
    MESSAGE(5, printf("Creating an external element in file #4\n"););
    aid1 = HXcreate(fid, 1020, 2, "t4.hdf", (int32)0, (int32)0);
    CHECK_VOID(aid1, FAIL, "HXcreate");

    MESSAGE(5, printf("Writing %d bytes to file #4\n", BUF_SIZE););
    ret = Hwrite(aid1, BUF_SIZE, outbuf);
    if (ret != BUF_SIZE) {
        fprintf(stderr, "Hwrite failed (code %d)\n", (int)ret);
        HEprint(stderr, 0);
        errors++;
    }

    ret = Hendaccess(aid1);
    CHECK_VOID(ret, FAIL, "Hendaccess");

    /* Close the file */
    ret = Hclose(fid);
    CHECK_VOID(ret, FAIL, "Hclose");

    /* Now re-open for reading and verifying the elements */
    MESSAGE(5, printf("Closing and re-opening base file %s\n", TESTFILE_NAME););
    fid = Hopen(TESTFILE_NAME, DFACC_RDWR, 0);
    CHECK_VOID(ret, FAIL, "Hopen");

    /* Verify element in file #1 */
    aid1 = Hstartread(fid, 1000, 1);
    CHECK_VOID(aid1, FAIL, "Hstartread");

    MESSAGE(5, printf("Inquiring about external element in file #1\n"););
    ret = Hinquire(aid1, &fileid, &tag, &ref, &length, &offset, &posn, &acc_mode, &special);
    CHECK_VOID(ret, FAIL, "Hinquire");

    for (i = 0; i < BUF_SIZE; i++)
        inbuf[i] = '\0';

    MESSAGE(5, printf("Reading external element in file #1\n"););
    ret = Hread(aid1, length, inbuf);
    if (ret != length) {
        fprintf(stderr, "Hread failed (code %d)\n", (int)ret);
        HEprint(stderr, 0);
        errors++;
    }

    MESSAGE(5, printf("Verifying data(%d bytes) in external element in file #1\n", (int)ret););
    if (strcmp((const char *)inbuf, (const char *)STRING3)) {
        fprintf(stderr, "Error: Object stored in file #1 is wrong\n");
        fprintf(stderr, "\t       Is: %s\n", (char *)inbuf);
        fprintf(stderr, "\tShould be: %s\n", STRING3);
        errors++;
    }

    ret = Hendaccess(aid1);
    CHECK_VOID(ret, FAIL, "Hendaccess");

    /* Verify element in file #2 */
    aid1 = Hstartread(fid, 1000, 4);
    CHECK_VOID(aid1, FAIL, "Hstartread");

    MESSAGE(5, printf("Inquiring about external element in file #2\n"););
    ret = Hinquire(aid1, &fileid, &tag, &ref, &length, &offset, &posn, &acc_mode, &special);
    CHECK_VOID(ret, FAIL, "Hinquire");

    for (i = 0; i < BUF_SIZE; i++)
        inbuf[i] = 0;

    ret = Hgetelement(fid, (uint16)tag, (uint16)ref, inbuf);
    if (ret != length) {
        fprintf(stderr, "Incorrect element size returned from Hgetelement: %d\n", (int)ret);
        HEprint(stderr, 0);
        errors++;
    }
    MESSAGE(5, printf("Verifying data(%d bytes) that was stored to file #2\n", (int)ret););

    errflag = 0;
    for (i = 0; i < ret; i++) {
        if (inbuf[i] != outbuf[i]) {
            errflag = 1;
            MESSAGE(8, printf("Wrong data at %d, out %d in %d\n", i, outbuf[i], inbuf[i]););
            errors++;
        }
        inbuf[i] = '\0';
    }
    if (errflag)
        fprintf(stderr, "Error: Wrong data in inbuf[] from external element in file #2\n");

    ret = Hendaccess(aid1);
    CHECK_VOID(ret, FAIL, "Hendaccess");

    /* Verify overlapping element in file #3 */
    aid1 = Hstartread(fid, 1001, 2);
    CHECK_VOID(aid1, FAIL, "Hstartread");

    MESSAGE(5, printf("Inquiring about overlapping external element in file #3\n"););
    ret = Hinquire(aid1, &fileid, &tag, &ref, &length, &offset, &posn, &acc_mode, &special);
    CHECK_VOID(ret, FAIL, "Hinquire");

    for (i = 0; i < BUF_SIZE; i++)
        inbuf[i] = '\0';

    ret = Hgetelement(fid, (uint16)tag, (uint16)ref, inbuf);
    if (ret != length) {
        fprintf(stderr, "Incorrect element size returned from Hgetelement: %d\n", (int)ret);
        HEprint(stderr, 0);
        errors++;
    }
    MESSAGE(
        5, printf("Verifying data(%d bytes) that was stored in overlapping element in file #3\n", (int)ret););

    if (inbuf[0] != '1' || inbuf[1] != '0' || inbuf[2] != '0' || inbuf[3] != '0') {
        fprintf(stderr, "Error: One or more errors in overlapping element in file #3\n");
        fprintf(stderr, "\t       is: %s\n", (char *)inbuf);
        fprintf(stderr, "\tShould be: %s\n", "1000");
        errors++;
    }

    ret = Hendaccess(aid1);
    CHECK_VOID(ret, FAIL, "Hendaccess");

    /* Verify the whole element in file #3 */
    aid1 = Hstartread(fid, 1000, 2);
    CHECK_VOID(aid1, FAIL, "Hstartread");

    MESSAGE(5, printf("Inquiring about external element in file #3\n"););
    ret = Hinquire(aid1, &fileid, &tag, &ref, &length, &offset, &posn, &acc_mode, &special);
    CHECK_VOID(ret, FAIL, "Hinquire");

    for (i = 0; i < BUF_SIZE; i++)
        inbuf[i] = '\0';

    MESSAGE(5, printf("Reading whole external element in file #3\n"););
    ret = Hread(aid1, length, inbuf);
    if (ret != length) {
        fprintf(stderr, "Hread failed (code %d)\n", (int)ret);
        HEprint(stderr, 0);
        errors++;
    }

    MESSAGE(5, printf("Verifying data(%d bytes) in whole external element in file #3\n", (int)ret););
    if (strcmp((const char *)inbuf, (const char *)STRING)) {
        fprintf(stderr, "Error: Object stored in file #3 is wrong\n");
        fprintf(stderr, "\t       is: %s\n", (char *)inbuf);
        fprintf(stderr, "\tShould be: %s\n", STRING);
        errors++;
    }

    ret = Hendaccess(aid1);
    CHECK_VOID(ret, FAIL, "Hendaccess");

    /* Verify element in file #4 */
    aid1 = Hstartread(fid, 1020, 2);
    CHECK_VOID(aid1, FAIL, "Hstartread");

    MESSAGE(5, printf("Inquiring about access element in file #4\n"););
    ret = Hinquire(aid1, &fileid, &tag, &ref, &length, &offset, &posn, &acc_mode, &special);
    CHECK_VOID(ret, FAIL, "Hinquire");

    for (i = 0; i < BUF_SIZE; i++)
        inbuf[i] = 0;

    ret = Hread(aid1, length, inbuf);
    if (ret != length) {
        fprintf(stderr, "Incorrect element size returned from Hread: %d\n", (int)ret);
        HEprint(stderr, 0);
        errors++;
    }

    MESSAGE(5, printf("Verifying data(%d bytes) in external element in file #4\n", (int)ret););

    errflag = 0;
    for (i = 0; i < ret; i++) {
        if (inbuf[i] != outbuf[i]) {
            errflag = 1;
            MESSAGE(8, printf("Wrong data at %d, out %d in %d\n", i, outbuf[i], inbuf[i]););
            errors++;
        }
        inbuf[i] = '\0';
    }
    if (errflag)
        fprintf(stderr, "Error: Wrong data in inbuf[]  from external element in file #4\n");

    ret = Hendaccess(aid1);
    CHECK_VOID(ret, FAIL, "Hendaccess");

    /* Write to the first element in file #1 again */
    MESSAGE(5, printf("Now writing again to external element in file #1\n"););
    aid2 = Hstartwrite(fid, 1000, 1, 4);
    CHECK_VOID(aid2, FAIL, "Hstartwrite");

    ret = Hwrite(aid2, 4, "ABCD");
    if (ret != 4) {
        fprintf(stderr, "Hwrite failed (code %d)\n", (int)ret);
        HEprint(stderr, 0);
        errors++;
    }

    ret = Hendaccess(aid2);
    CHECK_VOID(ret, FAIL, "Hendaccess");

    /* Second file open rest for reading */
    fid1 = Hopen(TESTFILE_NAME, DFACC_READ, 0);
    CHECK_VOID(fid1, FAIL, "Hopen");

    ret = (int32)Hnewref(fid1);
    CHECK_VOID(ret, FAIL, "Hnewref");

    /* Close first open of file */
    ret = Hclose(fid);
    CHECK_VOID(ret, FAIL, "Hclose");

    /* Close second open of file */
    ret = Hclose(fid1);
    CHECK_VOID(ret, FAIL, "Hclose");

    /*==============================*/
    /* Test External Path functions */
    /*==============================*/
    MESSAGE(5, printf("testing External Path functions\n"););

    /* start with a brand new file */
    MESSAGE(5, printf("Creating header file %s for external element \n", TESTFILE_NAME1););
    fid = Hopen(TESTFILE_NAME1, DFACC_CREATE, 0);
    CHECK_VOID(fid, FAIL, "Hopen");

    ret = HXsetcreatedir("testdir");
    CHECK_VOID(ret, FAIL, "HXsetcreatedir");

    MESSAGE(5, printf("Creating an external element in file testdir/t5.hdf\n"););
    aid1 = HXcreate(fid, 1000, 5, "t5.hdf", (int32)0, (int32)0);
    CHECK_VOID(aid1, FAIL, "HXcreate");

    MESSAGE(5, printf("Writing 2000 bytes to file t5.hdf\n"););
    ret = Hwrite(aid1, 2000, outbuf);
    CHECK_VOID(ret, FAIL, "Hwrite");

    MESSAGE(5, printf("Ending access to element and closing header file %s\n", TESTFILE_NAME1););
    ret = Hendaccess(aid1);
    CHECK_VOID(ret, FAIL, "Hendaccess");

    ret = Hclose(fid);
    CHECK_VOID(ret, FAIL, "Hclose");

    MESSAGE(5, printf("Re-open file and try read to external element.  Should fail the first time.\n"););

    fid = Hopen(TESTFILE_NAME1, DFACC_READ, 0);
    CHECK_VOID(fid, FAIL, "Hopen");

    ret = Hgetelement(fid, (uint16)1000, (uint16)5, inbuf);
    VERIFY_VOID(ret, FAIL, "Hgetelement");

    ret = HXsetdir("nosuchdir|testdir");
    CHECK_VOID(ret, FAIL, "HXsetdir");

    MESSAGE(5, printf("Try read it again.  Should not fail this time.\n"););

    ret = Hgetelement(fid, (uint16)1000, (uint16)5, inbuf);
    CHECK_VOID(ret, FAIL, "Hgetelement");

    errflag = 0;
    for (i = 0; i < ret; i++) {
        if (inbuf[i] != outbuf[i]) {
            errflag = 1;
            MESSAGE(8, printf("Wrong data at %d, out %d in %d\n", i, outbuf[i], inbuf[i]););
            errors++;
        }
        inbuf[i] = '\0';
    }
    if (errflag)
        fprintf(stderr, "Error: Wrong data in inbuf[]  from external element in file #5\n");

    ret = Hclose(fid);
    CHECK_VOID(ret, FAIL, "Hclose");

    /* unset the external paths directory variables */
    ret = HXsetcreatedir(NULL);
    CHECK_VOID(ret, FAIL, "HXsetcreatedir");
    ret = HXsetdir(NULL);
    CHECK_VOID(ret, FAIL, "HXsetdir");

    free(outbuf);
    free(inbuf);

    num_errs += errors; /* increment global error count */
}