File: file.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 (224 lines) | stat: -rw-r--r-- 7,294 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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * 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.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
   * Hopen
   ** Create a file.
   ** Open an existing file.
   *** Normally.
   *** Read-only file with DFACC_WRITE.
   ** Open non-existent file.
   *** With DFACC_READ.
   *** With DFACC_WRITE.
   ** Create an illegal file.
   ** Open the same file twice.
   *** First with DFACC_WRITE then with DFACC_READ.
   *** First with DFACC_WRITE then with DFACC_WRITE.
   *** First with DFACC_READ and then with DFACC_WRITE.
   *** First with DFACC_<any> and then with DFACC_CREATE.
   ** Open more files than there is slots.

   * Hclose
   ** Close a proper file.
   ** Close with an illegal file id.
   *** Random file id.
   *** Correct tag but bad slot.

   * Hstartread
   ** Normal.
   ** With illegal file id.
   ** With illegal tag/ref.
   ** With wildcard.
   ** Open more access elements than there is space.

 */

#include "tproto.h"
#define TESTFILE_NAME "t.hdf"
#define BUF_SIZE      4096

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

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

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

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

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

    MESSAGE(5, printf("Creating a file %s\n", TESTFILE_NAME););
    fid = Hopen(TESTFILE_NAME, DFACC_CREATE, 0);
    CHECK_VOID(fid, FAIL, "Hopen");

    ret_bool = (intn)Hishdf(TESTFILE_NAME);
    CHECK_VOID(ret_bool, FALSE, "Hishdf");

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

    MESSAGE(5, printf("Reading / Writing to file\n"););
    ret =
        Hputelement(fid, (uint16)100, 1, (const uint8 *)"testing 100 1", (int32)strlen("testing 100 1") + 1);
    CHECK_VOID(ret, FAIL, "Hputelement");

    ret = Hputelement(fid, (uint16)100, (uint16)4, outbuf, 2000);
    CHECK_VOID(ret, FAIL, "Hputelement");

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

    ret = Hputelement(fid, (uint16)103, (uint16)2, (const uint8 *)"element 103 2",
                      (int32)strlen("element 103 2") + 1);
    CHECK_VOID(ret, FAIL, "Hputlement");

    ret = Hgetelement(fid, (uint16)100, (uint16)4, inbuf);
    if (ret != 2000) {
        fprintf(stderr, "Line %d: Hgetelement returned wrong count: %d\n", (int)__LINE__, (int)ret);
        errors++;
    }

    for (i = 0; i < ret; i++) {
        if (inbuf[i] != outbuf[i])
            printf("Wrong data at %d, out %d in %d\n", i, outbuf[i], inbuf[i]);
        inbuf[i] = '\0';
    }

    ret = Hputelement(fid, 102, 2, outbuf, BUF_SIZE);
    CHECK_VOID(ret, FAIL, "Hputlement");

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

    MESSAGE(5, printf("Closing and re-opening file %s\n", TESTFILE_NAME););
    fid = Hopen(TESTFILE_NAME, DFACC_RDWR, 0);
    CHECK_VOID(fid, FAIL, "Hopen");

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

    aid1 = Hstartread(fid, 100, 1);
    CHECK_VOID(aid1, FAIL, "Hstartread");

    ret = Hinquire(aid1, &fileid, &tag, &ref, &length, &offset, &posn, &acc_mode, &special);
    CHECK_VOID(ret, FAIL, "Hinquire");

    MESSAGE(5, printf("Verifying data\n\n"););
    ret = Hread(aid1, length, inbuf);
    if (ret != 14) {
        fprintf(stderr, "ERROR: Hread returned the wrong length: %d\n", (int)ret);
        errors++;
    }

    if (strcmp((const char *)inbuf, (const char *)"testing 100 1")) {
        fprintf(stderr, "ERROR: Hread returned the wrong data\n");
        fprintf(stderr, "\t       Is: %s\n", (char *)inbuf);
        fprintf(stderr, "\tShould be: testing 100 1\n");
        errors++;
    }

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

    MESSAGE(5, printf("Testing a number of searching schemes\n"););
    ret = Hnextread(aid1, 100, DFREF_WILDCARD, DF_CURRENT);
    CHECK_VOID(ret, FAIL, "Hnextread");

    ret = Hinquire(aid1, &fileid, &tag, &ref, &length, &offset, &posn, &acc_mode, &special);
    CHECK_VOID(ret, FAIL, "Hinquire");

    ret = Hnextread(aid1, 100, DFREF_WILDCARD, DF_CURRENT);
    if (ret != FAIL) {
        fprintf(stderr, "ERROR: Found a non-existent element at line %d\n", __LINE__);
        errors++;
    }

    ret = Hnextread(aid1, DFTAG_WILDCARD, DFREF_WILDCARD, DF_START);
    CHECK_VOID(ret, FAIL, "Hnextread");

    ret = Hinquire(aid1, &fileid, &tag, &ref, &length, &offset, &posn, &acc_mode, &special);
    CHECK_VOID(ret, FAIL, "Hinquire");

    ret = Hnextread(aid1, DFTAG_WILDCARD, 3, DF_CURRENT);
    if (ret != FAIL) {
        fprintf(stderr, "ERROR: Found a non-existent element at line %d\n", __LINE__);
        errors++;
    }

    ret = Hnextread(aid1, DFTAG_WILDCARD, 2, DF_CURRENT);
    CHECK_VOID(ret, FAIL, "Hnextread");

    ret = Hinquire(aid1, &fileid, &tag, &ref, &length, &offset, &posn, &acc_mode, &special);
    CHECK_VOID(ret, FAIL, "Hinquire");

    aid2 = Hstartwrite(fid, 100, 1, 4);
    if (aid2 == FAIL) {
        fprintf(stderr, "ERROR: was not allowed to startwrite on existing object\n");
        errors++;
    }

    ret = Hwrite(aid1, 4, "ABCD");
    if (ret != FAIL) {
        fprintf(stderr, "ERROR: was allowed to write to read access object\n");
        errors++;
    }

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

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

    MESSAGE(5, printf("Attempting to gain multiple access to file (is allowed)\n"););
    fid1 = Hopen(TESTFILE_NAME, DFACC_READ, 0);
    if (fid1 == FAIL) {
        fprintf(stderr, "ERROR: Failed to have two concurrent access to file\n");
        errors++;
    }

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

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

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

    ret_bool = (intn)Hishdf(TESTFILE_NAME);
    CHECK_VOID(ret_bool, FALSE, "Hishdf");

    ret_bool = (intn)Hishdf(__FILE__);
    CHECK_VOID(ret_bool, TRUE, "Hishdf");

    ret_bool = (intn)Hishdf("qqqqqqqq.qqq"); /* I sure hope it isn't there */
    CHECK_VOID(ret, TRUE, "Hishdf");

    free(outbuf);
    free(inbuf);
}