netcdf-c/nc_test/tst_nofill2.c

108 lines
3.1 KiB
C
Raw Normal View History

2011-04-27 01:06:04 +08:00
/*
Copyright 2011, UCAR/Unidata
See COPYRIGHT file for copying and redistribution conditions.
This is part of netCDF.
2011-04-27 01:06:04 +08:00
This program tests for a bug discovered with nofill mode that failed
only on file systems with block size in a particular range. It fails
when invoked with the blksize argument between 2091953 and 2150032,
inclusive, and succeeds for other blksizes.
*/
#include <config.h>
#include <nc_tests.h>
#include "err_macros.h"
2011-04-27 01:06:04 +08:00
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <errno.h>
#include <netcdf.h>
#define FILE_NAME "tst_nofill2.nc"
#define LON_LEN 240
#define LAT_LEN 121
#define LVL_LEN 31
#define TIME_LEN 1
2011-04-27 04:57:24 +08:00
#define NDIMS1 1
#define NDIMS4 4
2011-04-27 01:06:04 +08:00
int
create_file(char *file_name, int fill_mode, size_t* sizehintp)
2011-04-27 01:06:04 +08:00
{
2011-04-27 04:57:24 +08:00
int ncid, time_id, zonal_wnd_id;
int dimids[NDIMS4];
size_t start[NDIMS4] = {0, 0, 0, 0};
size_t count[NDIMS4] = {TIME_LEN, 1, LAT_LEN, LON_LEN};
float zonal_wnd[LON_LEN * LAT_LEN * TIME_LEN];
2011-04-27 01:06:04 +08:00
size_t default_initialsize = 0;
double time[TIME_LEN] = {1.};
2011-04-27 04:57:24 +08:00
int i;
/* Init data. */
for(i = 0; i < TIME_LEN * LAT_LEN * LON_LEN; i++)
2011-04-27 04:57:24 +08:00
zonal_wnd[i] = 100 + i;
2011-04-27 01:06:04 +08:00
/* To test bug on filesystem without large block size, we can get
* the same effect by providing the desired value as sizehint to
* nc__create() instead of calling nc_create() and getting the
* block size reported by fstat */
if (nc__create(file_name, NC_CLOBBER, default_initialsize, sizehintp, &ncid)) ERR;
if (nc_set_fill(ncid, fill_mode, NULL)) ERR;
/* define dimensions */
2011-04-27 04:57:24 +08:00
if (nc_def_dim(ncid, "lon", LON_LEN, &dimids[3])) ERR;
if (nc_def_dim(ncid, "lat", LAT_LEN, &dimids[2])) ERR;
if (nc_def_dim(ncid, "lvl", LVL_LEN, &dimids[1])) ERR;
if (nc_def_dim(ncid, "time", TIME_LEN, &dimids[0])) ERR;
2011-04-27 01:06:04 +08:00
/* define variables */
2011-04-27 04:57:24 +08:00
if (nc_def_var(ncid, "time", NC_DOUBLE, NDIMS1, &dimids[0], &time_id)) ERR;
if (nc_def_var(ncid, "zonal_wnd", NC_FLOAT, NDIMS4, dimids, &zonal_wnd_id)) ERR;
2011-04-27 01:06:04 +08:00
if (nc_enddef (ncid)) ERR;
if (nc_put_var_double(ncid, time_id, time)) ERR;
2011-04-27 04:57:24 +08:00
2011-04-27 01:06:04 +08:00
/* Bug exposed when written in reverse order. */
for(i = LVL_LEN - 1; i>=0; i--)
{
2011-04-27 04:57:24 +08:00
start[1] = i;
if (nc_put_vara_float(ncid, zonal_wnd_id, start, count, zonal_wnd)) ERR;
2011-04-27 01:06:04 +08:00
}
if (nc_close(ncid)) ERR;
return 0;
}
int
main(int argc, char **argv)
{
size_t sizehint = (1750000); /* default if not set on command line,
* exposes bug. It turns out any
* value between 2091953 and 2150032
* triggers bug, whereas all other
* values work fine. */
printf("\n*** Testing nofill mode.\n");
printf("*** Create file in nofill mode, writing all values...");
{
2011-04-29 03:50:56 +08:00
#define NUM_TRIES 1
2011-04-27 01:06:04 +08:00
int ncid;
size_t idx[1] = {0};
double data;
int i;
for (i = 0; i < NUM_TRIES; i++)
{
printf(", trying sizehint of %lu ...", (unsigned long)sizehint);
2011-04-27 01:06:04 +08:00
if (create_file(FILE_NAME, NC_NOFILL, &sizehint)) ERR;
if (nc_open(FILE_NAME, 0, &ncid)) ERR;
if (nc_get_var1(ncid, 0, idx, &data)) ERR;
if (!data) ERR;
if (nc_close(ncid)) ERR;
sizehint += 10000;
}
}
SUMMARIZE_ERR;
FINAL_RESULTS;
}