/* Copyright 2018, UCAR/Unidata See COPYRIGHT file for copying and redistribution conditions. */ #include "config.h" #include #include #include #include #include #include "netcdf.h" #include "netcdf_filter.h" #undef TESTODDSIZE #undef DEBUG #define FILTER_ID 40000 #define MAXERRS 8 #define MAXPARAMS 32 #define MAXDIMS 8 #define DFALT_TESTFILE "testfilter_reg.nc" #define NPARAMS 1 #define PARAMVAL 17 #define NDIMS 4 static size_t dimsize[NDIMS] = {4,4,4,4}; static size_t chunksize[NDIMS] = {4,4,4,4}; static size_t ndims = NDIMS; static size_t totalproduct = 1; /* x-product over max dims */ static size_t actualproduct = 1; /* x-product over actualdims */ static size_t chunkproduct = 1; /* x-product over actual chunks */ static int nerrs = 0; static char* testfile = NULL; static int ncid, varid; static int dimids[MAXDIMS]; static size_t odom[MAXDIMS]; static float* array = NULL; static float* expected = NULL; /* Forward */ static int filter_test1(void); static void init(int argc, char** argv); static void reset(void); static void odom_reset(void); static int odom_more(void); static int odom_next(void); static int odom_offset(void); static float expectedvalue(void); #define ERRR do { \ fflush(stdout); /* Make sure our stdout is synced with stderr. */ \ fprintf(stderr, "Sorry! Unexpected result, %s, line: %d\n", \ __FILE__, __LINE__); \ nerrs++;\ } while (0) static int check(int err,int line) { if(err != NC_NOERR) { fprintf(stderr,"fail (%d): %s\n",line,nc_strerror(err)); } return NC_NOERR; } static void report(const char* msg, int lineno) { fprintf(stderr,"fail: line=%d %s\n",lineno,msg); exit(1); } #define CHECK(x) check(x,__LINE__) #define REPORT(x) report(x,__LINE__) static int verifychunks(void) { int i; int store = -1; size_t localchunks[MAXDIMS]; memset(localchunks,0,sizeof(localchunks)); CHECK(nc_inq_var_chunking(ncid, varid, &store, localchunks)); if(store != NC_CHUNKED) { fprintf(stderr,"bad chunk store\n"); return 0; } for(i=0;i= MAXERRS) break; } } } else { odom_reset(); while(odom_more()) { int offset = odom_offset(); float expect = expectedvalue(); if(array[offset] != expect) { fprintf(stderr,"data mismatch: array[%d]=%f expected=%f\n", offset,array[offset],expect); errs++; if(errs >= MAXERRS) break; } odom_next(); } } if(errs == 0) printf("no data errors\n"); return (errs == 0); } static int filter_test1(void) { int ok = 1; unsigned int params[MAXPARAMS]; reset(); printf("test1: def filter repeat .\n"); create(); setchunking(); params[0] = 1; params[1] = 17; deffilter(FILTER_ID,2,params); params[0] = 0; params[1] = 18; deffilter(FILTER_ID,2,params); CHECK(nc_enddef(ncid)); /* Fill in the array */ fill(); printf("test1: compression.\n"); /* write array */ CHECK(nc_put_var(ncid,varid,expected)); CHECK(nc_close(ncid)); printf("test1: decompression.\n"); reset(); openfile(); CHECK(nc_get_var_float(ncid, varid, array)); ok = compare(); CHECK(nc_close(ncid)); return ok; } /**************************************************/ /* Utilities */ static void reset() { memset(array,0,sizeof(float)*actualproduct); } static void odom_reset(void) { memset(odom,0,sizeof(odom)); } static int odom_more(void) { return (odom[0] < dimsize[0]); } static int odom_next(void) { int i; /* do not make unsigned */ for(i=ndims-1;i>=0;i--) { odom[i] += 1; if(odom[i] < dimsize[i]) break; if(i == 0) return 0; /* leave the 0th entry if it overflows*/ odom[i] = 0; /* reset this position*/ } return 1; } static int odom_offset(void) { int i; int offset = 0; for(i=0;i 1) testfile = argv[1]; else testfile = DFALT_TESTFILE; /* Setup various variables */ totalproduct = 1; actualproduct = 1; chunkproduct = 1; for(i=0;i 0?1:0); }