File: file1.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 (233 lines) | stat: -rw-r--r-- 8,173 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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * 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.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
   test opening files and access elements until limits are reached
 */

#include "tproto.h"
#include "hfile_priv.h"

/* This value can cause platform-specific problems as the number of files
 * that can be open at one time via fopen() is often capped by the OS.
 *
 * The current value doesn't seem to cause problems on any platform.
 */
#define BIG 127

#define TESTFILE_NAME  "thf"
#define TESTREF_NAME   "tref.hdf"
#define MAX_REF_TESTED MAX_REF
static int32 files[BIG];
static int32 accs[BIG];

static void test_file_limits(void);
static void test_ref_limits(void);

static void
test_file_limits(void)
{
    int   i;
    int32 ret;

#ifdef H4_HAVE_WIN32_API
    /* Windows can only have 512 stdio files open by default, so we need
     * to bump this to handle large values for BIG files open at once.
     *
     * Technically no longer necessary since BIG was adjusted to < 512, but
     * worth keeping around in case we need to set this in the future.
     */
    ret = _setmaxstdio(1024);
    CHECK_VOID(ret, FAIL, "_setmaxstdio");
#endif

    MESSAGE(5, puts("Opening many files of same name"););
    for (i = 0; i < BIG; i++) {
        files[i] = Hopen("thf.hdf", DFACC_RDWR, 0);
        CHECK_VOID(files[i], FAIL, "Hopen");
        if (files[i] < 0) {
            break;
        }
    }
    MESSAGE(5, printf("Opening stopped at %d/%d files\n", i, BIG););

    MESSAGE(5, puts("Closing all files"););
    for (i--; i >= 0; i--) {
        ret = Hclose(files[i]);
        CHECK_VOID(ret, FAIL, "Hclose");
        if (ret < 0)
            printf("Error closing file %d\n", i);
    }
    MESSAGE(5, puts("Closed files"););

    MESSAGE(5, puts("Opening many files of different names"););
    for (i = 0; i < BIG; i++) {
        char fname[100];
        sprintf(fname, "%s%1d.hdf", TESTFILE_NAME, i);
        files[i] = Hopen(fname, DFACC_ALL, 0);
        CHECK_VOID(files[i], FAIL, "Hopen");
        if (files[i] < 0) {
            break;
        }
    }
    MESSAGE(5, printf("Opening stopped at %d/%d files\n", i, BIG););

    MESSAGE(5, puts("Closing all files except first open"););
    for (i--; i > 0; i--) {
        ret = Hclose(files[i]);
        CHECK_VOID(ret, FAIL, "Hclose");
        if (ret < 0)
            printf("Error closing file %d\n", i);
    }
    MESSAGE(5, puts("Closed files"););

    MESSAGE(5, puts("Opening write access elements"););
    for (i = 0; i < BIG; i++) {
        accs[i] = Hstartwrite(files[0], (uint16)100, (uint16)(i + 1), 100L);
        CHECK_VOID(accs[i], FAIL, "Hstartwrite");
        if (accs[i] < 0)
            break;
    }
    MESSAGE(5, printf("Opening stopped at %d element\n", i););

    MESSAGE(5, puts("Closing access elements"););
    for (i--; i >= 0; i--) {
        ret = Hendaccess(accs[i]);
        CHECK_VOID(ret, FAIL, "Hendaccess");
        if (ret < 0)
            printf("Error ending access %d\n", i);
    }
    MESSAGE(5, puts("Ended access"););

    ret = Hclose(files[0]);
    CHECK_VOID(ret, FAIL, "Hclose");
} /* end test_file_limits() */

#define TAG1 ((uint16)1000)
#define TAG2 ((uint16)1001)

static void
test_ref_limits(void)
{
    int32 i;   /* local counting variable */
    int32 fid; /* file ID */
    int32 iloop;

    MESSAGE(6, printf("Testing reference # limits\n"););
    MESSAGE(7, printf("Writing out data\n"););
    /* Write out MAX_REF number of data items for each tag */
    fid = Hopen(TESTREF_NAME, DFACC_CREATE, 512);
    CHECK_VOID(fid, FAIL, "Hopen");

    if (fid != FAIL) {
        iloop = MAX_REF_TESTED;
        for (i = 1; i <= (iloop / 2) + 5; i++) {
            int32  aid;
            uint16 ref;
            int32  data;
            int32  ret;

            /* Write out data to tag1 */
            ref = Htagnewref(fid, TAG1);
            CHECK_VOID(ref, 0, "Htagnewref");
            aid = Hstartwrite(fid, TAG1, ref, sizeof(int32));
            CHECK_VOID(aid, FAIL, "Hstartwrite");
            data = (int32)ref;
            ret  = Hwrite(aid, sizeof(int32), &data);
            CHECK_VOID(ret, FAIL, "Hwrite");
            ret = Hendaccess(aid);
            CHECK_VOID(ret, FAIL, "Hendaccess");

            /* lets be a little smatter here */
            if (ret == FAIL)
                break;

            /* Write out data to tag2 */
            ref = Htagnewref(fid, TAG2);
            CHECK_VOID(ref, 0, "Htagnewref");
            aid = Hstartwrite(fid, TAG2, ref, sizeof(int32));
            CHECK_VOID(aid, FAIL, "Hstartwrite");
            data = ref << 16;
            ret  = Hwrite(aid, sizeof(int32), &data);
            CHECK_VOID(ret, FAIL, "Hwrite");
            ret = Hendaccess(aid);
            CHECK_VOID(ret, FAIL, "Hendaccess");
            /* lets be a little smatter here */
            if (ret == FAIL)
                break;

        } /* end for */
        Hclose(fid);

        MESSAGE(7, printf("Verifying data\n"););

        /* Check the data written earlier */
        fid = Hopen(TESTREF_NAME, DFACC_READ, 0);
        CHECK_VOID(fid, FAIL, "Hopen");

        if (fid != FAIL) {
            uint16 ref;
            int32  aid1, aid2;
            int32  data;
            int32  ret;

            /* Read in data from tag1 */
            aid1 = Hstartread(fid, TAG1, DFREF_WILDCARD);
            CHECK_VOID(aid1, FAIL, "Hstartread");
            ret = Hread(aid1, sizeof(int32), &data);
            CHECK_VOID(ret, FAIL, "Hread");
            ret = Hinquire(aid1, NULL, NULL, &ref, NULL, NULL, NULL, NULL, NULL);
            CHECK_VOID(ret, FAIL, "Hinquire");
            VERIFY_VOID((uint16)data, ref, "Hread");

            /* Read in data from tag2 */
            aid2 = Hstartread(fid, TAG2, DFREF_WILDCARD);
            CHECK_VOID(aid2, FAIL, "Hstartread");
            ret = Hread(aid2, sizeof(int32), &data);
            CHECK_VOID(ret, FAIL, "Hread");
            ret = Hinquire(aid2, NULL, NULL, &ref, NULL, NULL, NULL, NULL, NULL);
            CHECK_VOID(ret, FAIL, "Hinquire");
            VERIFY_VOID((uint32)data, (((uint32)ref) << 16), "Hread");

            while (Hnextread(aid1, TAG1, DFTAG_WILDCARD, DF_CURRENT) != FAIL) {
                ret = Hread(aid1, sizeof(int32), &data);
                CHECK_VOID(ret, FAIL, "Hread");
                ret = Hinquire(aid1, NULL, NULL, &ref, NULL, NULL, NULL, NULL, NULL);
                CHECK_VOID(ret, FAIL, "Hinquire");
                VERIFY_VOID((uint16)data, ref, "Hread");

                if (Hnextread(aid2, TAG2, DFTAG_WILDCARD, DF_CURRENT) != FAIL) {
                    ret = Hread(aid2, sizeof(int32), &data);
                    CHECK_VOID(ret, FAIL, "Hread");
                    ret = Hinquire(aid2, NULL, NULL, &ref, NULL, NULL, NULL, NULL, NULL);
                    CHECK_VOID(ret, FAIL, "Hinquire");
                    VERIFY_VOID((uint32)data, (((uint32)ref) << 16), "Hread");
                } /* end while */
            }     /* end while */
            ret = Hendaccess(aid1);
            CHECK_VOID(ret, FAIL, "Hendaccess");

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

            Hclose(fid);
        } /* end if */
    }     /* end if */
} /* end test_ref_limits() */

void
test_hfile1(void)
{
    test_file_limits();
    test_ref_limits();
}