File: tst_udf.c

package info (click to toggle)
netcdf-parallel 1%3A4.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 105,352 kB
  • sloc: ansic: 229,114; sh: 11,180; yacc: 2,561; makefile: 1,390; lex: 1,173; xml: 173; awk: 2
file content (349 lines) | stat: -rw-r--r-- 8,790 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
/* This is part of the netCDF package. Copyright 2005-2018 University
   Corporation for Atmospheric Research/Unidata. See COPYRIGHT file
   for conditions of use.

   Test user-defined formats.

   Ed Hartnett
*/

#include "config.h"
#include <nc_tests.h>
#include "err_macros.h"
#include "netcdf.h"
#include "nc4dispatch.h"
#include "netcdf_dispatch.h"

#define FILE_NAME "tst_udf.nc"

#if defined(_WIN32) || defined(_WIN64)
int
NC4_show_metadata(int ncid)
{
    return 0;
}
#endif

int
tst_open(const char *path, int mode, int basepe, size_t *chunksizehintp,
         void *parameters, const NC_Dispatch *dispatch, int ncid)
{
    return NC_NOERR;
}

int
tst_abort(int ncid)
{
    return TEST_VAL_42;
}

int
tst_close(int ncid, void *v)
{
    return NC_NOERR;
}

int
tst_inq_format(int ncid, int *formatp)
{
    return TEST_VAL_42;
}

int
tst_inq_format_extended(int ncid, int *formatp, int *modep)
{
    return TEST_VAL_42;
}

int
tst_get_vara(int ncid, int varid, const size_t *start, const size_t *count,
             void *value, nc_type t)
{
    return TEST_VAL_42;
}

/* This is the dispatch object that holds pointers to all the
 * functions that make up the test dispatch interface. */
static NC_Dispatch tst_dispatcher = {

    NC_FORMATX_UDF0,
    NC_DISPATCH_VERSION,

    NC_RO_create,
    tst_open,

    NC_RO_redef,
    NC_RO__enddef,
    NC_RO_sync,
    tst_abort,
    tst_close,
    NC_RO_set_fill,
    tst_inq_format,
    tst_inq_format_extended,

    NC4_inq,
    NC4_inq_type,

    NC_RO_def_dim,
    NC4_inq_dimid,
    NC4_inq_dim,
    NC4_inq_unlimdim,
    NC_RO_rename_dim,

    NC4_inq_att,
    NC4_inq_attid,
    NC4_inq_attname,
    NC_RO_rename_att,
    NC_RO_del_att,
    NC4_get_att,
    NC_RO_put_att,

    NC_RO_def_var,
    NC4_inq_varid,
    NC_RO_rename_var,
    tst_get_vara,
    NC_RO_put_vara,
    NCDEFAULT_get_vars,
    NCDEFAULT_put_vars,
    NCDEFAULT_get_varm,
    NCDEFAULT_put_varm,

    NC4_inq_var_all,

    NC_NOTNC4_var_par_access,
    NC_RO_def_var_fill,

    NC4_show_metadata,
    NC4_inq_unlimdims,

    NC4_inq_ncid,
    NC4_inq_grps,
    NC4_inq_grpname,
    NC4_inq_grpname_full,
    NC4_inq_grp_parent,
    NC4_inq_grp_full_ncid,
    NC4_inq_varids,
    NC4_inq_dimids,
    NC4_inq_typeids,
    NC4_inq_type_equal,
    NC_NOTNC4_def_grp,
    NC_NOTNC4_rename_grp,
    NC4_inq_user_type,
    NC4_inq_typeid,

    NC_NOTNC4_def_compound,
    NC_NOTNC4_insert_compound,
    NC_NOTNC4_insert_array_compound,
    NC_NOTNC4_inq_compound_field,
    NC_NOTNC4_inq_compound_fieldindex,
    NC_NOTNC4_def_vlen,
    NC_NOTNC4_put_vlen_element,
    NC_NOTNC4_get_vlen_element,
    NC_NOTNC4_def_enum,
    NC_NOTNC4_insert_enum,
    NC_NOTNC4_inq_enum_member,
    NC_NOTNC4_inq_enum_ident,
    NC_NOTNC4_def_opaque,
    NC_NOTNC4_def_var_deflate,
    NC_NOTNC4_def_var_fletcher32,
    NC_NOTNC4_def_var_chunking,
    NC_NOTNC4_def_var_endian,
    NC_NOTNC4_def_var_filter,
    NC_NOTNC4_set_var_chunk_cache,
    NC_NOTNC4_get_var_chunk_cache
};

/* This is the dispatch object that holds pointers to all the
 * functions that make up the test dispatch interface with a bad
 * version number. */
static NC_Dispatch tst_dispatcher_bad_version = {

    NC_FORMATX_UDF0,
    NC_DISPATCH_VERSION - 1,

    NC_RO_create,
    tst_open,

    NC_RO_redef,
    NC_RO__enddef,
    NC_RO_sync,
    tst_abort,
    tst_close,
    NC_RO_set_fill,
    tst_inq_format,
    tst_inq_format_extended,

    NC4_inq,
    NC4_inq_type,

    NC_RO_def_dim,
    NC4_inq_dimid,
    NC4_inq_dim,
    NC4_inq_unlimdim,
    NC_RO_rename_dim,

    NC4_inq_att,
    NC4_inq_attid,
    NC4_inq_attname,
    NC_RO_rename_att,
    NC_RO_del_att,
    NC4_get_att,
    NC_RO_put_att,

    NC_RO_def_var,
    NC4_inq_varid,
    NC_RO_rename_var,
    tst_get_vara,
    NC_RO_put_vara,
    NCDEFAULT_get_vars,
    NCDEFAULT_put_vars,
    NCDEFAULT_get_varm,
    NCDEFAULT_put_varm,

    NC4_inq_var_all,

    NC_NOTNC4_var_par_access,
    NC_RO_def_var_fill,

    NC4_show_metadata,
    NC4_inq_unlimdims,

    NC4_inq_ncid,
    NC4_inq_grps,
    NC4_inq_grpname,
    NC4_inq_grpname_full,
    NC4_inq_grp_parent,
    NC4_inq_grp_full_ncid,
    NC4_inq_varids,
    NC4_inq_dimids,
    NC4_inq_typeids,
    NC4_inq_type_equal,
    NC_NOTNC4_def_grp,
    NC_NOTNC4_rename_grp,
    NC4_inq_user_type,
    NC4_inq_typeid,

    NC_NOTNC4_def_compound,
    NC_NOTNC4_insert_compound,
    NC_NOTNC4_insert_array_compound,
    NC_NOTNC4_inq_compound_field,
    NC_NOTNC4_inq_compound_fieldindex,
    NC_NOTNC4_def_vlen,
    NC_NOTNC4_put_vlen_element,
    NC_NOTNC4_get_vlen_element,
    NC_NOTNC4_def_enum,
    NC_NOTNC4_insert_enum,
    NC_NOTNC4_inq_enum_member,
    NC_NOTNC4_inq_enum_ident,
    NC_NOTNC4_def_opaque,
    NC_NOTNC4_def_var_deflate,
    NC_NOTNC4_def_var_fletcher32,
    NC_NOTNC4_def_var_chunking,
    NC_NOTNC4_def_var_endian,
    NC_NOTNC4_def_var_filter,
    NC_NOTNC4_set_var_chunk_cache,
    NC_NOTNC4_get_var_chunk_cache
};

#define NUM_UDFS 2

int mode[NUM_UDFS] = {NC_UDF0, NC_UDF1};

int
main(int argc, char **argv)
{
    printf("\n*** Testing user-defined formats.\n");
    printf("*** testing simple user-defined format...");
    {
        int ncid;
        NC_Dispatch *disp_in;
        int i;

        /* Create an empty file to play with. */
        if (nc_create(FILE_NAME, 0, &ncid)) ERR;
        if (nc_close(ncid)) ERR;

        /* Test all available user-defined format slots. */
        for (i = 0; i < NUM_UDFS; i++)
        {
            /* Add our user defined format. */
            if (nc_def_user_format(mode[i], &tst_dispatcher, NULL)) ERR;

            /* Check that our user-defined format has been added. */
            if (nc_inq_user_format(mode[i], &disp_in, NULL)) ERR;
            if (disp_in != &tst_dispatcher) ERR;

            /* Open file with our defined functions. */
            if (nc_open(FILE_NAME, mode[i], &ncid)) ERR;
            if (nc_close(ncid)) ERR;

            /* Open file again and abort, which is the same as closing
             * it. This also tests that the UDF flags are given
             * priority. If NC_NETCDF4 flag were given priority, then
             * nc_abort() will not return TEST_VAL_42, but instead will
             * return 0. */
            if (nc_open(FILE_NAME, mode[i]|NC_NETCDF4, &ncid)) ERR;
            if (nc_inq_format(ncid, NULL) != TEST_VAL_42) ERR;
            if (nc_inq_format_extended(ncid, NULL, NULL) != TEST_VAL_42) ERR;
            if (nc_abort(ncid) != TEST_VAL_42) ERR;
        }
    }
    SUMMARIZE_ERR;
    printf("*** testing user-defined format with magic number...");
    {
        int ncid;
        NC_Dispatch *disp_in;
        char magic_number[5] = "1111";
        char dummy_data[11] = "0123456789";
        char magic_number_in[NC_MAX_MAGIC_NUMBER_LEN];
        FILE *FP;
        int i;

        /* Create a file with magic number at start. */
        if (!(FP = fopen(FILE_NAME, "w"))) ERR;
        if (fwrite(magic_number, sizeof(char), strlen(magic_number), FP)
            != strlen(magic_number)) ERR;
        if (fwrite(dummy_data, sizeof(char), strlen(dummy_data), FP)
            != strlen(dummy_data)) ERR;
        if (fclose(FP)) ERR;

        /* Test all available user-defined format slots. */
        for (i = 0; i < NUM_UDFS; i++)
        {
            /* Add our test user defined format. */
            if (nc_def_user_format(mode[i], &tst_dispatcher, magic_number)) ERR;

            /* Check that our user-defined format has been added. */
            if (nc_inq_user_format(mode[i], &disp_in, magic_number_in)) ERR;
            if (disp_in != &tst_dispatcher) ERR;
            if (strncmp(magic_number, magic_number_in, strlen(magic_number))) ERR;

            /* Open file with our defined functions. */
            if (nc_open(FILE_NAME, mode[i], &ncid)) ERR;
            if (nc_close(ncid)) ERR;

            /* Open file again and abort, which is the same as closing
             * it. This time we don't specify a mode, because the magic
             * number is used to identify the file. */
            if (nc_open(FILE_NAME, 0, &ncid)) ERR;
            if (nc_inq_format(ncid, NULL) != TEST_VAL_42) ERR;
            if (nc_inq_format_extended(ncid, NULL, NULL) != TEST_VAL_42) ERR;
            if (nc_abort(ncid) != TEST_VAL_42) ERR;
        }
    }
    SUMMARIZE_ERR;
    printf("*** testing bad version causes dispatch table to be rejected...");
    {
        int i;

        /* Test all available user-defined format slots. */
        for (i = 0; i < NUM_UDFS; i++)
        {
            /* Make sure our bad version format is rejected. */
            if (nc_def_user_format(mode[i], &tst_dispatcher_bad_version,
                                   NULL) != NC_EINVAL) ERR;
        }
    }
    SUMMARIZE_ERR;
    FINAL_RESULTS;
}