File: tst_cdf5_begin.c

package info (click to toggle)
netcdf-parallel 1%3A4.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 101,668 kB
  • sloc: ansic: 200,241; sh: 10,807; yacc: 2,522; makefile: 1,306; lex: 1,153; xml: 173; awk: 2
file content (56 lines) | stat: -rw-r--r-- 1,998 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
#include <stdio.h>
#include <stdlib.h>
#include <netcdf.h>

/* When using NetCDF 4.4.1 ad prior to create a CDF-5 file and defining a small
 * variable after a big variable (> 2^32-3 bytes), the file starting offset of
 * the small variable (and all variables defined after the big variable) is
 * calculated incorrectly. This test program detects this bug by checking the
 * contents of the possible overlaps between the two variables.
 */

#define ERR {if(err!=NC_NOERR){printf("Error at line %d in %s: %s\n", __LINE__,__FILE__, nc_strerror(err));nerrs++;}}

#define FILE_NAME "tst_cdf5_begin.nc"

int main(int argc, char *argv[])
{
    int i, err, nerrs=0, ncid, dimid[2], varid[2];
    short buf[10];
    size_t start, count;
  
    err = nc_create(FILE_NAME, NC_CLOBBER|NC_64BIT_DATA, &ncid); ERR;
    err = nc_def_dim(ncid, "dim0", NC_MAX_UINT, &dimid[0]); ERR
    err = nc_def_dim(ncid, "dim1", 10,          &dimid[1]); ERR

    /* define one small variable after one big variable */
    err = nc_def_var(ncid, "var_big",   NC_SHORT, 1, &dimid[0], &varid[0]); ERR
    err = nc_def_var(ncid, "var_small", NC_SHORT, 1, &dimid[1], &varid[1]); ERR
    err = nc_set_fill(ncid, NC_NOFILL, NULL); ERR
    err = nc_enddef(ncid); ERR

    /* write to var_big in location overlapping with var_small when using
     * netCDF 4.4.x or prior */
    start = NC_MAX_UINT/sizeof(short);
    count = 10;
    for (i=0; i<10; i++) buf[i] = i;
    err = nc_put_vara_short(ncid, varid[0], &start, &count, buf); ERR

    /* write var_small */
    for (i=0; i<10; i++) buf[i] = -1;
    err = nc_put_var_short(ncid, varid[1], buf); ERR

    /* read back var_big and check contents */
    for (i=0; i<10; i++) buf[i] = -1;
    err = nc_get_vara_short(ncid, varid[0], &start, &count,buf); ERR
    for (i=0; i<10; i++) {
        if (buf[i] != i) {
            printf("Error at buf[%d] expect %d but got %hd\n",i,i,buf[i]);
            nerrs++;
        }
    }
    err = nc_close(ncid); ERR

    return (nerrs > 0);
}