File: varput.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 (145 lines) | stat: -rw-r--r-- 4,745 bytes parent folder | download | duplicates (4)
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
/*********************************************************************
 *   Copyright 2018, UCAR/Unidata
 *   See netcdf/COPYRIGHT file for copying and redistribution conditions.
 *   $Header: /upc/share/CVS/netcdf-3/nctest/varput.c,v 1.14 2006/10/31 16:22:05 ed Exp $
 *********************************************************************/

#include <config.h>
#include <stdio.h>
#include <stdlib.h>		/* for free() */
#include "netcdf.h"
#include "testcdf.h"		/* defines in-memory test cdf structure */
#include "emalloc.h"
#include "val.h"
#include "error.h"
#include "tests.h"


/*
 * Test ncvarput
 *    check that proper call worked with ncvarget
 *    try with negative coords, edges, check error
 *    try with too-large coords, edges, check error
 *    try with bad variable handle, check error
 *    try in define mode, check error
 *    try with bad netCDF handle, check error
 */
int
test_ncvarput(path)
     const char *path;		/* name of writable netcdf file to open */
{
    int nerrs = 0;
    static char pname[] = "test_ncvarput";
    int cdfid;			/* netcdf id */
    int iv;			/* variable id */
    struct cdfhc {		/* a hypercube with generic values */
	long cor[MAX_NC_DIMS];	/* netcdf coordinates for lower corner */
	long edg[MAX_NC_DIMS];	/* netcdf edge lengths to upper corner */
	void *vals;		/* pointer to block of values */
    } hc;			/* test hypercube */
    long tmp;
    int id;			/* dimension id */

    (void) fprintf(stderr, "*** Testing %s ...\t", &pname[5]);

    /* handle case where struct netcdf test is uninitialised */
    hc.cor[0] = 0 ;
    hc.edg[0] = 1 ;
    hc.vals = 0 ;

    if ((cdfid = ncopen(path, NC_WRITE)) == -1) {
	error("%s: ncopen failed", pname);
	return ++nerrs;
    }

    /* opened in data mode, try putting and getting hypercubes of each type */
    nerrs += test_varputget (cdfid);	/* prints messages for discrepancies */
    
    /* find a variable with at least one dimension */
    iv = 0;
    while (test.vars[iv].ndims <= 0 && iv < test.nvars)
      iv++;
    if (iv < test.nvars) {	/* iv is first varid of var with dimensions */
	/* set coords */
	for (id = 0; id < test.vars[iv].ndims; id++) {
	    hc.cor[id] = 0;
	    hc.edg[id] = 1;
	}
	/* fill in vals with value of appropriate type */
	hc.vals = emalloc(nctypelen(test.vars[iv].type));
	val_fill(test.vars[iv].type, 1, hc.vals);

	id = test.vars[iv].ndims - 1;
	tmp = hc.cor[id];
	hc.cor[id] = -1;	/* try negative coordinate, should fail */
	if(ncvarput (cdfid, iv, hc.cor, hc.edg, hc.vals) != -1) {
	    error("%s: ncvarput should fail for negative corner", pname);
	    ncclose(cdfid); return ++nerrs;
	}
	hc.cor[id] = tmp;
	tmp = hc.edg[id];
	hc.edg[id] = -1;	/* try negative edge, should fail */
	if(ncvarput (cdfid, iv, hc.cor, hc.edg, hc.vals) != -1) {
	    error("%s: ncvarput should fail for negative edge", pname);
	    ncclose(cdfid); return ++nerrs;
	}
	hc.edg[id] = tmp;
	{ 
		long mqv = test.vars[iv].ndims -1 ;
		int dim = test.vars[iv].dims[mqv] ;

		tmp = hc.cor[mqv];
		hc.cor[mqv] = test.dims[dim].size; /* try big coordinate, should fail */
		if(ncvarput (cdfid, iv, hc.cor, hc.edg, hc.vals) != -1) {
		    error("%s: ncvarput should fail for too-high coordinate", pname);
		    ncclose(cdfid); return ++nerrs;
		}
		hc.cor[mqv] = tmp;
	
		tmp = hc.edg[mqv];
		hc.edg[mqv] = test.dims[dim].size + 1; /* try big edge, should fail */
		if(ncvarput (cdfid, iv, hc.cor, hc.edg, hc.vals) != -1) {
		    error("%s: ncvarput should fail for too-high edge", pname);
		    ncclose(cdfid); return ++nerrs;
		}
		hc.edg[mqv] = tmp;
	}

	if (ncredef(cdfid) == -1) {
	    error("%s: ncredef failed", pname);
	    ncclose(cdfid); return ++nerrs;
	}
	/* try in define mode, should fail */
	if(ncvarput (cdfid, iv, hc.cor, hc.edg, hc.vals) != -1) {
	    error("%s: ncvarput should fail in define mode", pname);
	    ncclose(cdfid); return ++nerrs;
	}
	if (ncendef (cdfid) == -1) {
	    error("%s: ncendef failed", pname);
	    ncclose(cdfid); return ++nerrs;
	}
    }
    else
	error("\"struct netcdf test\" uninitialized, no tests performed") ;
    /* try with bad variable handle, should fail */
    if(ncvarput (cdfid, -1, hc.cor, hc.edg, hc.vals) != -1) {
	error("%s: ncvarput should fail for bad variable handle", pname);
	ncclose(cdfid); return ++nerrs;
    }
    if (ncclose (cdfid) == -1) {
	error("%s: ncclose failed", pname);
	return ++nerrs;
    }
    /* try with bad netCDF handle, should fail */
    if(ncvarput (cdfid, 0, hc.cor, hc.edg, hc.vals) != -1) {
	error("%s: ncvarput failed to report bad netcdf handle", pname);
	nerrs++;
    }
    if(hc.vals) free (hc.vals);
    if (nerrs > 0)
      (void) fprintf(stderr,"FAILED! ***\n");
    else
      (void) fprintf(stderr,"ok ***\n");

    return nerrs;
}