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 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
|
/*********************************************************************
* Copyright 2018, UCAR/Unidata
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
* $Header: /upc/share/CVS/netcdf-3/nctest/dimtests.c,v 1.14 2006/10/31 16:21:54 ed Exp $
*********************************************************************/
#include <config.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h> /* for free() */
#include "netcdf.h"
#include "emalloc.h"
#include "testcdf.h" /* defines in-memory test cdf structure */
#include "add.h" /* functions to update in-memory netcdf */
#include "error.h"
#include "tests.h"
/*
* Test ncdimdef
* try in data mode, check error
* check that returned id is one more than previous id
* try adding same dimension twice, check error
* try with illegal sizes, check error
* make sure unlimited size works, shows up in ncinquire(...,*xtendim)
* try to define a second unlimited dimension, check error
*/
int
test_ncdimdef(path)
const char *path; /* name of writable netcdf to open */
{
int nerrs = 0;
static char pname[] = "test_ncdimdef";
int cdfid; /* netcdf id */
static struct cdfdim mm = /* dimension */
{"mm", 1}; /* 1 should be a valid dimension size */
static struct cdfdim nn = /* dimension */
{"bogus", ___}; /* used for testing invalid dimension sizes */
static struct cdfdim rec = /* dimension */
{"rec", NC_UNLIMITED};
int ndims; /* number of dimensions */
int nvars; /* number of variables */
int natts; /* number of attributes */
int xdimid; /* id of unlimited dimension, or -1 if none */
int dimid; /* dimension id */
(void) fprintf(stderr, "*** Testing %s ...\t", &pname[5]);
if ((cdfid = ncopen(path, NC_WRITE)) == -1) {
error("%s: ncopen failed", pname);
return ++nerrs;
}
/* opened, defining a dimension should fail in data mode */
if (ncdimdef(cdfid, mm.name, mm.size) != -1) {
error("%s: ncdimdef should have failed in data mode", pname);
ncclose(cdfid); return ++nerrs;
}
/* enter define mode */
if (ncredef(cdfid) == -1) {
error("%s: cdredef failed", pname);
ncclose(cdfid); return ++nerrs;
}
/* in define mode OK, add a dimension */
if ((dimid = ncdimdef(cdfid, mm.name, mm.size)) == -1) {
error("%s: ncdimdef failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_dim(&test, &mm); /* keep in-memory netcdf in sync */
/* check that dim id returned is one more than previous dim id */
if (dimid != test.ndims - 1) {
error("%s: ncdimdef returned %d for dim id, expected %d",
pname, dimid, test.ndims-1);
ncclose(cdfid); return ++nerrs;
}
/* try adding same dimension again, this should fail */
if (ncdimdef(cdfid, mm.name, mm.size) != -1) {
error("%s: ncdimdef should not have allowed redefinition", pname);
ncclose(cdfid); return ++nerrs;
}
/* try adding dimension with negative size, this should fail */
if (ncdimdef(cdfid, nn.name, (long) -10) != -1) {
error("%s: ncdimdef should not allow negative size dimension", pname);
ncclose(cdfid); return ++nerrs;
}
/* if there is not already an unlimited size dimension, try adding one */
if (ncinquire(cdfid, &ndims, &nvars, &natts, &xdimid) == -1) {
error("%s: ncinquire failed", pname);
ncclose(cdfid); return ++nerrs;
}
if (xdimid == -1) {
if (ncdimdef(cdfid, rec.name, rec.size) == -1) {
error("%s: ncdimdef failed on NC_UNLIMITED dimension", pname);
ncclose(cdfid); return ++nerrs;
}
add_dim(&test, &rec);
}
/* try adding another unlimited dimension, which should fail */
if (ncdimdef(cdfid, "rec2", rec.size) != -1) {
error("%s: ncdimdef should not allow second NC_UNLIMITED dimension",
pname);
ncclose(cdfid); return ++nerrs;
}
if (ncendef (cdfid) == -1) {
error("%s: ncendef failed", pname);
ncclose(cdfid); return ++nerrs;
}
if (ncclose (cdfid) == -1) {
error("%s: ncclose failed", pname);
return ++nerrs;
}
if (ncdimdef(cdfid, "rec2", rec.size) != -1) {
error("%s: ncdimdef should fail on bad netCDF id", pname);
nerrs++;
}
if (nerrs > 0)
(void) fprintf(stderr,"FAILED! ***\n");
else
(void) fprintf(stderr,"ok ***\n");
return nerrs;
}
/*
* Test ncdimid
* check return with defined dimension in both modes
* try with undefined dimension, check error
* check return with unlimited size dimension
* try with bad handle, check error
*/
int
test_ncdimid(path)
const char *path; /* name of writable netcdf file to open */
{
int nerrs = 0;
static char pname[] = "test_ncdimid";
int cdfid; /* netcdf id */
int nn_dim; /* dimension id */
static struct cdfdim nn = /* dimension */
{"nn", 1}; /* 1 should be a valid dimension size */
(void) fprintf(stderr, "*** Testing %s ...\t\t", &pname[5]);
if ((cdfid = ncopen(path, NC_WRITE)) == -1) {
error("%s: ncopen failed", pname);
return ++nerrs;
}
/* opened, enter define mode */
if (ncredef(cdfid) == -1) {
error("%s: cdredef failed", pname);
ncclose(cdfid); return ++nerrs;
}
/* in define mode OK, add a dimension */
if ((nn_dim = ncdimdef(cdfid, nn.name, nn.size)) == -1) {
error("%s: ncdimdef failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_dim(&test, &nn); /* keep in-memory netcdf in sync */
/* check id returned for name matches id returned from definition */
if (ncdimid(cdfid, nn.name) != nn_dim) {
error("%s: ncdimid returned wrong value in define mode", pname);
ncclose(cdfid); return ++nerrs;
}
if (ncendef (cdfid) == -1) {
error("%s: ncendef failed", pname);
ncclose(cdfid); return ++nerrs;
}
/* in data mode, check returned id for dimension just added */
if (ncdimid(cdfid, nn.name) != nn_dim) {
error("%s: ncdimid returned wrong value in data mode", pname);
ncclose(cdfid); return ++nerrs;
}
/* try with undefined dimension, should fail */
if (ncdimid(cdfid, "easter-bunny") != -1) {
error("%s: ncdimid with bogus name should have failed ", pname);
ncclose(cdfid); return ++nerrs;
}
/* try with unlimited dimension, assumed to be "rec" from earlier calls */
if (ncdimid(cdfid, "rec") != test.xdimid) {
error("%s: ncdimid returned bad value for record dimension", pname);
ncclose(cdfid); return ++nerrs;
}
if (ncclose (cdfid) == -1) {
error("%s: ncclose failed", pname);
return ++nerrs;
}
/* try on bad handle, should fail */
if (ncdimid(cdfid, nn.name) != -1) {
error("%s: ncdimid failed to report bad netcdf handle", pname);
nerrs++;
}
if (nerrs > 0)
(void) fprintf(stderr,"FAILED! ***\n");
else
(void) fprintf(stderr,"ok ***\n");
return nerrs;
}
/*
* Test ncdiminq
* try in both modes
* check returned name and size against defined name and size
* try with bad dimension handle, check error
* try with bad netCDF handle, check error
*/
int
test_ncdiminq(path)
const char *path; /* name of writable netcdf file to open */
{
int nerrs = 0;
static char pname[] = "test_ncdiminq";
int cdfid; /* netcdf id */
int dimid; /* dimension id */
struct cdfdim dim; /* dimension */
(void) fprintf(stderr, "*** Testing %s ...\t", &pname[5]);
if ((cdfid = ncopen(path, NC_WRITE)) == -1) {
error("%s: ncopen failed", pname);
return ++nerrs;
}
/* opened, in data mode */
dim.name = (char *) emalloc(MAX_NC_NAME);
for (dimid = 0 ; dimid < test.ndims; dimid++) { /* loop on all dim ids */
if (ncdiminq(cdfid, dimid, dim.name, &dim.size) == -1) {
error("%s: ncdiminq in data mode failed on dim id %d",
pname, dimid);
ncclose(cdfid); return ++nerrs;
}
/* compare returned with expected values */
if (strcmp(dim.name, test.dims[dimid].name) != 0) {
error("%s: ncdiminq (data mode), name %s, expected %s for id = %d",
pname, dim.name, test.dims[dimid].name, dimid);
nerrs++;
}
if (dim.size != test.dims[dimid].size) {
error("%s: ncdiminq (data mode), size %d, expected %d for id = %d",
pname, dim.size, test.dims[dimid].size, dimid);
nerrs++;
}
}
if (ncredef(cdfid) == -1) {
error("%s: ncredef failed", pname);
ncclose(cdfid); return ++nerrs;
}
/* in define mode, compare returned with expected values again */
for (dimid = 0 ; dimid < test.ndims; dimid++) { /* loop on all dim ids */
if (ncdiminq(cdfid, dimid, dim.name, &dim.size) == -1) {
error("%s: ncdiminq in define mode failed on dim id %d",
pname, dimid);
ncclose(cdfid); return ++nerrs;
}
/* compare returned with expected values */
if (strcmp(dim.name, test.dims[dimid].name) != 0) {
error("%s: ncdiminq (define), name %s, expected %s for id = %d",
pname, dim.name, test.dims[dimid].name, dimid);
nerrs++;
}
if (dim.size != test.dims[dimid].size) {
error("%s: ncdiminq (define), size %d, expected %d for id = %d",
pname, dim.size, test.dims[dimid].size, dimid);
nerrs++;
}
}
/* try with bad dimension handles, check for failure */
if (ncdiminq(cdfid, -1, dim.name, &dim.size) != -1 ||
ncdiminq(cdfid, test.ndims, dim.name, &dim.size) != -1) {
error("%s: ncdiminq should have failed on bad dimension ids",
pname, dimid);
ncclose(cdfid); return ++nerrs;
}
if (ncendef (cdfid) == -1) {
error("%s: ncendef failed", pname);
ncclose(cdfid); return ++nerrs;
}
if (ncclose (cdfid) == -1) {
error("%s: ncclose failed", pname);
return ++nerrs;
}
/* should fail, since bad handle */
if (test.ndims >= 1) { /* if any dimensions have been defined */
if (ncdiminq (cdfid, 0, dim.name, &dim.size) != -1) {
error("%s: ncdiminq failed to report bad netcdf handle ", pname);
nerrs++;
}
}
free(dim.name);
if (nerrs > 0)
(void) fprintf(stderr,"FAILED! ***\n");
else
(void) fprintf(stderr,"ok ***\n");
return nerrs;
}
/*
* Test ncdimrename
* check that proper rename worked with ncdiminq
* try renaming to existing dimension name, check error
* try with bad dimension handle, check error
* try with bad netCDF handle, check error
*/
int
test_ncdimrename(path)
const char *path; /* name of writable netcdf file to open */
{
int nerrs = 0;
static char pname[] = "test_ncdimrename";
int cdfid; /* netcdf id */
int pp_dim; /* dimension id */
static struct cdfdim pp = /* dimension */
{"pp", 7};
static char newname[MAX_NC_NAME] = /* dimension name */
"new_name";
struct cdfdim dim; /* dimension */
static struct cdfdim qq = /* dimension */
{"qq", 10};
(void) fprintf(stderr, "*** Testing %s ...\t", &pname[5]);
if ((cdfid = ncopen(path, NC_WRITE)) == -1) {
error("%s: ncopen failed", pname);
return ++nerrs;
}
/* opened */
if (ncredef(cdfid) == -1) {
error("%s: ncredef failed", pname);
ncclose(cdfid); return ++nerrs;
}
/* in define mode, add two dimensions */
if ((pp_dim = ncdimdef(cdfid, pp.name, pp.size)) == -1) {
error("%s: ncdimdef failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_dim(&test, &pp); /* keep in-memory netcdf in sync */
if (ncdimdef(cdfid, qq.name, qq.size) == -1) {
error("%s: ncdimdef failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_dim(&test, &qq); /* keep in-memory netcdf in sync */
/* rename first dimension */
if (ncdimrename(cdfid, pp_dim, newname) == -1) {
error("%s: ncdimrename failed", pname);
ncclose(cdfid); return ++nerrs;
}
/* check new name with ncdiminq */
dim.name = (char *) emalloc(MAX_NC_NAME);
if (ncdiminq(cdfid, pp_dim, dim.name, &dim.size) == -1) {
error("%s: ncdiminq failed", pname);
ncclose(cdfid); return ++nerrs;
}
if (strcmp(dim.name,pp.name) == 0) {
error("%s: ncdimrename failed to change name", pname);
ncclose(cdfid); return ++nerrs;
}
if (strcmp(dim.name,newname) != 0) {
error("%s: ncdimrename changed name to %s instead of %s",
pname, dim.name, newname);
ncclose(cdfid); return ++nerrs;
}
test.dims[pp_dim].name = (char *) erealloc((void *)test.dims[pp_dim].name,
strlen(newname)+1);
(void) strcpy(test.dims[pp_dim].name, newname); /* keep test consistent */
/* try to rename first dimension same as second, should fail */
if (ncdimrename(cdfid, pp_dim, qq.name) != -1) {
error("%s: ncdimrename should have failed with used name", pname);
ncclose(cdfid); return ++nerrs;
}
/* try with bad dimension handles, check for failure */
if (ncdimrename(cdfid, -1, dim.name) != -1 ||
ncdimrename(cdfid, test.ndims, dim.name) != -1) {
error("%s: ncdimrename should have failed on bad dimension ids",
pname);
ncclose(cdfid); return ++nerrs;
}
if (ncendef (cdfid) == -1) {
error("%s: ncendef failed", pname);
ncclose(cdfid); return ++nerrs;
}
/* in data mode, rename to shorter name */
if (ncdimrename(cdfid, pp_dim, "p") == -1) {
error("%s: ncdimrename to shorter name failed in data mode", pname);
ncclose(cdfid); return ++nerrs;
}
test.dims[pp_dim].name = (char *) erealloc((void *)test.dims[pp_dim].name,
strlen("p")+1);
(void) strcpy(test.dims[pp_dim].name, "p"); /* keep test consistent */
/* Check with ncdimid */
if (pp_dim != ncdimid(cdfid, "p")) {
error("%s: lookup by name in data mode failed after ncdimrename",
pname);
return ++nerrs;
}
/* in data mode, restore old name */
if (ncdimrename(cdfid, pp_dim, pp.name) == -1) {
error("%s: ncdimrename failed in data mode", pname);
ncclose(cdfid); return ++nerrs;
}
test.dims[pp_dim].name = (char *) erealloc((void *)test.dims[pp_dim].name,
strlen(pp.name)+1);
(void) strcpy(test.dims[pp_dim].name, pp.name); /* keep test consistent */
if (ncclose (cdfid) == -1) {
error("%s: ncclose failed", pname);
return ++nerrs;
}
/* should fail, since bad handle */
if (ncdimrename (cdfid, 0, dim.name) != -1) {
error("%s: ncdimrename failed to report bad netcdf handle ", pname);
nerrs++;
}
free (dim.name);
if (nerrs > 0)
(void) fprintf(stderr,"FAILED! ***\n");
else
(void) fprintf(stderr,"ok ***\n");
return nerrs;
}
|