1717
1818#undef DEBUG
1919
20- #ifndef H5Z_FILTER_BZIP2
21- #define H5Z_FILTER_BZIP2 307
22- #endif
23-
24- #define TEST_ID 32768
20+ #define FILTER_ID 1 /*deflate*/
2521
2622#define MAXERRS 8
2723
@@ -51,8 +47,7 @@ static int nerrs = 0;
5147
5248static int ncid , varid ;
5349static int dimids [MAXDIMS ];
54- static float * array = NULL ;
55- static float * expected = NULL ;
50+ static char * * array = NULL ;
5651
5752/* Forward */
5853static int test_test1 (void );
@@ -100,26 +95,142 @@ defvar(nc_type xtype)
10095 return NC_NOERR ;
10196}
10297
98+ static int
99+ reopen (void )
100+ {
101+ size_t i ;
102+
103+ CHECK (nc_open (testfile , NC_NETCDF4 , & ncid ));
104+ for (i = 0 ;i < ndims ;i ++ ) {
105+ char dimname [1024 ];
106+ snprintf (dimname ,sizeof (dimname ),"dim%u" ,(unsigned )i );
107+ CHECK (nc_inq_dimid (ncid , dimname , & dimids [i ]));
108+ CHECK (nc_inq_dim (ncid , dimids [i ], NULL , & dimsize [i ]));
109+ }
110+ CHECK (nc_inq_varid (ncid , "var" , & varid ));
111+ return NC_NOERR ;
112+ }
113+
114+
115+
116+ /* Test that a filter is a variable length var is defined */
103117static int
104118test_test1 (void )
105119{
106120 int ok = 1 ;
107- int id = -1 ;
108- size_t nparams ;
121+ size_t nfilters = 0 ;
122+ unsigned filterids [64 ];
123+ unsigned params [NPARAMS ] = {5 };
124+ size_t nparams = 0 ;
109125
110- reset ();
111126 fprintf (stderr ,"test4: filter on a variable length type.\n" );
112127 create ();
113128 defvar (NC_STRING );
114129 /* Do explicit filter; should never fail, but may produce log warning */
115- CHECK (nc_def_var_filter (ncid ,varid ,H5Z_FILTER_BZIP2 , 0 , NULL ));
130+ CHECK (nc_def_var_filter (ncid ,varid ,FILTER_ID , 1 , params ));
116131 /* Now see if filter was defined or not */
117- CHECK (nc_inq_var_filter (ncid ,varid ,& id ,& nparams ,NULL ));
118- if (id > 0 ) {
119- fprintf (stderr ,"*** id=%d\n" ,id );
132+ memset (filterids ,0 ,sizeof (filterids ));
133+ params [0 ] = 5 ;
134+ CHECK (nc_inq_var_filter_ids (ncid ,varid ,& nfilters ,filterids ));
135+ fprintf (stderr ,"test_test1: nc_var_filter_ids: nfilters=%u filterids[0]=%d\n" ,(unsigned )nfilters ,filterids [0 ]);
136+ if (nfilters != 1 && filterids [0 ] != FILTER_ID ) {
137+ fprintf (stderr ,"test_test1: nc_var_filter_ids: failed\n" );
120138 ok = 0 ;
121139 }
122- CHECK (nc_abort (ncid ));
140+ params [0 ] = 0 ;
141+ CHECK (nc_inq_var_filter_info (ncid , varid , filterids [0 ], & nparams , params ));
142+ fprintf (stderr ,"test_test1: nc_inq_var_filter_info: nparams=%u params[0]=%u\n" ,(unsigned )nparams ,(unsigned )params [0 ]);
143+ return ok ;
144+ }
145+
146+ /* Test that a filter on a variable length var is suppressed */
147+ static int
148+ test_test2 (void )
149+ {
150+ int stat = NC_NOERR ;
151+ int ok = 1 ;
152+ size_t i ;
153+
154+ reset ();
155+ fprintf (stderr ,"test4: write with filter on a variable length type.\n" );
156+ /* generate the data to write */
157+ for (i = 0 ;i < actualproduct ;i ++ ) {
158+ char digits [64 ];
159+ snprintf (digits ,sizeof (digits ),"%u" ,(unsigned )i );
160+ array [i ] = strdup (digits );
161+ }
162+ /* write the data */
163+ if ((stat = nc_put_var (ncid ,varid ,(void * )array ))) {
164+ fprintf (stderr ,"test_test2: nc_put_var: error = (%d)%s\n" ,stat ,nc_strerror (stat ));
165+ ok = 0 ;
166+ goto done ;
167+ }
168+ /* re-read the data */
169+ reset ();
170+ if ((stat = nc_get_var (ncid ,varid ,(void * )array ))) {
171+ fprintf (stderr ,"test_test2: nc_get_var: error = (%d)%s\n" ,stat ,nc_strerror (stat ));
172+ ok = 0 ;
173+ goto done ;
174+ }
175+ /* verify the data */
176+ for (i = 0 ;i < actualproduct ;i ++ ) {
177+ unsigned value = 0xffffffff ;
178+ if (array [i ] != NULL )
179+ sscanf (array [i ],"%u" ,& value );
180+ if (array [i ] == NULL || i != value ) {
181+ fprintf (stderr ,"test_test2: nc_get_var: value mismatch at %u\n" ,(unsigned )i );
182+ ok = 0 ;
183+ goto done ;
184+ }
185+ }
186+ nc_close (ncid );
187+ done :
188+ return ok ;
189+ }
190+
191+ /* Test that a filter on a variable length var is suppressed */
192+ static int
193+ test_test3 (void )
194+ {
195+ int stat = NC_NOERR ;
196+ int ok = 1 ;
197+ size_t i ,nfilters ;
198+ unsigned filterids [64 ];
199+
200+ fprintf (stderr ,"test4: re-open variable with filter on a variable length type and verify state.\n" );
201+
202+ reopen ();
203+
204+ /* verify filter state */
205+ memset (filterids ,0 ,sizeof (filterids ));
206+ CHECK (nc_inq_var_filter_ids (ncid ,varid ,& nfilters ,filterids ));
207+ fprintf (stderr ,"test_test3: nc_var_filter_ids: nfilters=%u filterids[0]=%d\n" ,(unsigned )nfilters ,filterids [0 ]);
208+ if (nfilters != 1 && filterids [0 ] != FILTER_ID ) {
209+ fprintf (stderr ,"test_test3: nc_var_filter_ids: failed\n" );
210+ ok = 0 ;
211+ goto done ;
212+ }
213+
214+ /* re-read the data */
215+ reset ();
216+ if ((stat = nc_get_var (ncid ,varid ,(void * )array ))) {
217+ fprintf (stderr ,"test_test3: nc_get_var: error = (%d)%s\n" ,stat ,nc_strerror (stat ));
218+ ok = 0 ;
219+ goto done ;
220+ }
221+ /* verify the data */
222+ for (i = 0 ;i < actualproduct ;i ++ ) {
223+ unsigned value = 0xffffffff ;
224+ if (array [i ] != NULL )
225+ sscanf (array [i ],"%u" ,& value );
226+ if (array [i ] == NULL || i != value ) {
227+ fprintf (stderr ,"test_test3: nc_get_var: value mismatch at %u\n" ,(unsigned )i );
228+ ok = 0 ;
229+ goto done ;
230+ }
231+ }
232+ nc_close (ncid );
233+ done :
123234 return ok ;
124235}
125236
@@ -129,7 +240,11 @@ test_test1(void)
129240static void
130241reset ()
131242{
132- memset (array ,0 ,sizeof (float )* actualproduct );
243+ size_t i ;
244+ for (i = 0 ;i < actualproduct ;i ++ ) {
245+ if (array [i ]) free (array [i ]);
246+ array [i ] = NULL ;
247+ }
133248}
134249
135250static void
@@ -153,8 +268,7 @@ init(int argc, char** argv)
153268 }
154269 }
155270 /* Allocate max size */
156- array = (float * )calloc (1 ,sizeof (float )* actualproduct );
157- expected = (float * )calloc (1 ,sizeof (float )* actualproduct );
271+ array = (char * * )calloc (1 ,sizeof (char * )* actualproduct );
158272}
159273
160274/**************************************************/
@@ -167,6 +281,8 @@ main(int argc, char **argv)
167281#endif
168282 init (argc ,argv );
169283 if (!test_test1 ()) ERRR ;
284+ if (!test_test2 ()) ERRR ;
285+ if (!test_test3 ()) ERRR ;
170286 fprintf (stderr ,"*** %s\n" ,(nerrs > 0 ? "FAILED" : "PASS" ));
171287 exit (nerrs > 0 ?1 :0 );
172288}
0 commit comments