Skip to content

Commit 58f99e1

Browse files
Unit tests for grb2_inq.c (#555)
* initial test set up * set up for testable static variables * Update grb2_inq.c * try making compile definition public? * refactor and print to test changes * add some error codes to grb2_get_data() & set up first test * debug broken test * change a few error codes * add tests for grb2_get_lonlat() * another test * more tests * Update test_grb2_inq.c * debug failing case * tests for grb2_size_gridmeta() * fix test * more tests * gridmeta fix * documented changes for wrong function * Update test_grb2_inq.c * Update test_grb2_inq.c * Update test_grb2_inq.c * add some error codes * debug * more tests * Update test_grb2_inq.c * update test and see if it gives -2 * check current output * another test * another test * remove last to debug * debug * Update test_grb2_inq.c * Update test_grb2_inq.c * Update test_grb2_inq.c * trying to get -4 return * Update CMakeLists.txt * Update test_grb2_inq.c * remove the failing test and set up * Update test_grb2_inq.c * set up with different file * Update test_grb2_inq.c * Update test_grb2_inq.c * Update test_grb2_inq.c * Update test_grb2_inq.c * Update test_grb2_inq.c * Update test_grb2_inq.c * Update test_grb2_inq.c * Update test_grb2_inq.c * Update test_grb2_inq.c * Update test_grb2_inq.c * Update test_grb2_inq.c * Update test_grb2_inq.c * done with test * remove some new functions that ended up not being used * debug test * debug * add more print statements to test * debugging * more debug * more debug, add NULL to end of varargs * debug spack * turn off part of test if jpeg not available
1 parent 6491896 commit 58f99e1

5 files changed

Lines changed: 557 additions & 36 deletions

File tree

.github/workflows/Spack.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ jobs:
3333
package-name: wgrib2
3434
package-variants: ${{ matrix.variants }}
3535
custom-recipe: spack/package.py
36-
use-repo-cache: true
36+
use-repo-cache: false
3737
spack-compiler: gcc
3838
use-common-build-cache: true
39-
cache-spack-lock: true
39+
cache-spack-lock: false
4040
repo-cache-key-suffix: ${{ matrix.os }}-${{ matrix.variants }}-
4141

4242
recipe-check:

src/c_api/c_wgrib2api.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ int wgrib2_add_cmd(const char *string);
5353
int wgrib2_cmd(void);
5454
int wgrib2_list_cmd(void);
5555

56-
5756
void fatal_error(const char *fmt, ...);
5857

58+
/* Functions to support unit testing only. */
59+
void grb2_inq_set_state(int last_options_val, int good_val, int npnts_val);

src/c_api/grb2_inq.c

Lines changed: 130 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,26 @@ static int inv_no;
5252
/**
5353
* This function performs an inquiry of a grib message.
5454
*
55+
* ### Program History Log
56+
* Date | Programmer | Comments
57+
* -----|------------|---------
58+
* 3/2018 | W. Ebisuzaki | Initial
59+
* 7/2026 | A. Stahl | New error codes for testing purposes
60+
*
5561
* @param grb Input grib file.
5662
* @param inv Inventory of input file.
5763
* @param options Bitwise OR of option flags.
5864
* options = SEQUENTIAL | DATA | LATLON | WENS | RAW_ORDER | META | GRIDMETA | REGEX
5965
* @param ... Additional optional arguments.
6066
*
67+
* @return The number of points in the grid, error code otherwise
68+
* - 0 :: Failure in wgrib2_get_mem_buffer_size()
69+
* - -1 :: Conflicting options (e.g., WENS and LATLON used together)
70+
* - -2 :: Failed to call wgrib2
71+
* - -3 :: Error retrieving memory buffer
72+
* - -4 :: Error parsing basic grid info
73+
* - -5 :: Inventory number greater than 1
74+
*
6175
* @return The number of points in the grid, or 0 if an error occurred.
6276
*
6377
* @author Wesley Ebisuzaki @date 3/2018
@@ -119,11 +133,11 @@ long long int grb2_inqVA(const char *grb, const char *inv, unsigned int options,
119133
if (options & WENS) {
120134
if (options & LATLON) {
121135
fprintf(stderr,"grb2_inq: WENS option cannot be used at same time as LATLON option\n");
122-
return 1;
136+
return -1;
123137
}
124138
if (options & RAW_ORDER) {
125139
fprintf(stderr,"grb2_inq: WENS option cannot be used at same time as RAW_ORDER option\n");
126-
return 1;
140+
return -1;
127141
}
128142
wgrib2_add_cmd("-order");
129143
wgrib2_add_cmd("we:ns");
@@ -133,7 +147,7 @@ long long int grb2_inqVA(const char *grb, const char *inv, unsigned int options,
133147
if (options & RAW_ORDER) {
134148
if (options & LATLON) {
135149
fprintf(stderr,"grb2_inq: RAW_ORDER option cannot be used at same time as LATLON option\n");
136-
return 1;
150+
return -1;
137151
}
138152
wgrib2_add_cmd("-order");
139153
wgrib2_add_cmd("raw");
@@ -156,19 +170,19 @@ long long int grb2_inqVA(const char *grb, const char *inv, unsigned int options,
156170
wgrib2_list_cmd();
157171

158172
i = wgrib2_cmd();
159-
if (i) return 0; /* failed call to wgrib2 */
173+
if (i) return -2; /* failed call to wgrib2 */
160174

161175
/* read basic parameters in register 19 */
162176

163177
bufsize = sizeof(buffer);
164178
i = wgrib2_get_mem_buffer((unsigned char *) buffer, bufsize, 19);
165-
if (i != 0) return 0; /* something wrong .. probably not found */
179+
if (i != 0) return -3; /* something wrong .. probably not found */
166180

167181
i = sscanf(buffer, "%11d %11u %11u %11u %11d %11d",&inv_no,&npnts,&nx_,&ny_,&msg_no, &submsg);
168182
printf(">>> wgrb2_scannf = %d\n",i);
169-
if (i != 6) return 0;
183+
if (i != 6) return -4;
170184

171-
if (inv_no > 1) return 0;
185+
if (inv_no > 1) return -5;
172186

173187
/* finally success */
174188
good = 1;
@@ -179,10 +193,22 @@ long long int grb2_inqVA(const char *grb, const char *inv, unsigned int options,
179193
/**
180194
* Get memory-copy of grid data from RPN register.
181195
*
196+
* ### Program History Log
197+
* Date | Programmer | Comments
198+
* -----|------------|---------
199+
* 3/2018 | W. Ebisuzaki | Initial
200+
* 7/2026 | A. Stahl | New error codes for testing purposes
201+
*
182202
* @param data Pointer to the data array.
183203
* @param ndata Number of data points.
184204
*
185-
* @return 0 on success, 1 on error.
205+
* @return
206+
* - 0 :: success
207+
* - 10 :: last find did not work
208+
* - 11 :: wrong size data
209+
* - 12 :: grb2_inq did not request reading data
210+
*
211+
* See documentation for wgrib2_get_reg_data() for additional error codes.
186212
*
187213
* @note Grid data stored in register 19.
188214
*
@@ -192,15 +218,15 @@ int grb2_get_data(float *data, int ndata) {
192218

193219
if (good == 0) {
194220
fprintf(stderr,"grb2_get_data: last find did not work.\n");
195-
return 1;
221+
return 10;
196222
}
197223
if (ndata != npnts) {
198224
fprintf(stderr,"grb2_get_data: wrong size data.\n");
199-
return 1;
225+
return 11;
200226
}
201227
if ((last_options & DATA) == 0) {
202228
fprintf(stderr,"grb2_get_data: grb2_inq did not request reading data.\n");
203-
return 1;
229+
return 12;
204230
}
205231

206232
return wgrib2_get_reg_data(data, ndata, 19);
@@ -209,11 +235,24 @@ int grb2_get_data(float *data, int ndata) {
209235
/**
210236
* Get memory-copy of longitude and latitude data from RPN registers.
211237
*
238+
* ### Program History Log
239+
* Date | Programmer | Comments
240+
* -----|------------|---------
241+
* 3/2018 | W. Ebisuzaki | Initial
242+
* 7/2026 | A. Stahl | New error codes for testing purposes
243+
*
212244
* @param lon Pointer to the longitude array.
213245
* @param lat Pointer to the latitude array.
214246
* @param ndata Number of data points.
215247
*
216-
* @return 0 on success, error code otherwise.
248+
* @return
249+
* - 0 :: success
250+
* - 10 :: last find did not work
251+
* - 11 :: wrong size data
252+
* - 12 :: grb2_inq did not request reading lonlat
253+
*
254+
* See documentation for wgrib2_get_reg_data() for additional error codes. This function
255+
* returns the sum of the error codes from reading the longitude and latitude registers.
217256
*
218257
* @note Longitude and latitude data stored in registers 17 and 18, respectively.
219258
*
@@ -223,15 +262,15 @@ int grb2_get_lonlat(float *lon, float *lat, int ndata) {
223262
int err1, err2;
224263
if (good == 0) {
225264
fprintf(stderr,"grb2_get_lonlat: last find did not work.\n");
226-
return 1;
265+
return 10;
227266
}
228267
if (ndata != npnts) {
229268
fprintf(stderr,"grb2_get_lonlat: wrong size data.\n");
230-
return 1;
269+
return 11;
231270
}
232271
if ((last_options & LONLAT) == 0) {
233272
fprintf(stderr,"grb2_get_lonlat: grb2_inq did not request reading lonlat.\n");
234-
return 1;
273+
return 12;
235274
}
236275

237276
err1 = wgrib2_get_reg_data(lon, ndata, 17);
@@ -242,8 +281,17 @@ int grb2_get_lonlat(float *lon, float *lat, int ndata) {
242281
/**
243282
* Get the size of the memory buffer for metadata stored in RPN register.
244283
*
245-
* @return Size of the memory buffer for metadata, or 0 if an error occurred.
246-
*
284+
* ### Program History Log
285+
* Date | Programmer | Comments
286+
* -----|------------|---------
287+
* 3/2018 | W. Ebisuzaki | Initial
288+
* 7/2026 | A. Stahl | New error codes for testing purposes
289+
*
290+
* @return Size of the memory buffer for metadata on success, error code otherwise
291+
* - 0 :: Failure in wgrib2_get_mem_buffer_size()
292+
* - -1 :: last find did not work
293+
* - -2 :: grb2_inq did not request reading metadata
294+
*
247295
* @note Metadata stored in register 18.
248296
*
249297
* @author Wesley Ebisuzaki @date 3/2018
@@ -253,11 +301,11 @@ int grb2_size_meta(void) {
253301

254302
if (good == 0) {
255303
fprintf(stderr,"grb2_size_meta: last find did not work.\n");
256-
return 0;
304+
return -1;
257305
}
258306
if ((last_options & META) == 0) {
259307
fprintf(stderr,"grb2_size_meta: grb2_inq did not request reading metadata.\n");
260-
return 0;
308+
return -2;
261309
}
262310
size = (unsigned int) wgrib2_get_mem_buffer_size(18);
263311
if (size == 0) return 0;
@@ -267,10 +315,23 @@ int grb2_size_meta(void) {
267315
/**
268316
* Get memory-copy of metadata from RPN register.
269317
*
318+
* ### Program History Log
319+
* Date | Programmer | Comments
320+
* -----|------------|---------
321+
* 3/2018 | W. Ebisuzaki | Initial
322+
* 7/2026 | A. Stahl | New error codes for testing purposes
323+
*
270324
* @param meta Pointer to the metadata array.
271325
* @param nbytes Size of the metadata buffer.
272326
*
273-
* @return 0 on success, error code otherwise.
327+
* @return
328+
* - 0 :: success
329+
* - 10 :: last find did not work
330+
* - 11 :: grb2_inq did not request reading metadata
331+
* - 12 :: grib format error
332+
* - 13 :: size of metadata is too big
333+
*
334+
* See documentation for wgrib2_get_mem_buffer() for additional error codes.
274335
*
275336
* @note Metadata stored in register 18.
276337
*
@@ -282,21 +343,21 @@ int grb2_get_meta(unsigned char *meta, int nbytes) {
282343

283344
if (good == 0) {
284345
fprintf(stderr,"grb2_get_meta: last find did not work.\n");
285-
return 1;
346+
return 10;
286347
}
287348
if ((last_options & META) == 0) {
288349
fprintf(stderr,"grb2_get_meta: grb2_inq did not request reading metadata.\n");
289-
return 1;
350+
return 11;
290351
}
291352

292353
size = wgrib2_get_mem_buffer_size(18);
293354
if (size == 0) {
294355
fprintf(stderr,"grb2_get_meta: size = 0, grib format error\n");
295-
return 1;
356+
return 12;
296357
}
297358
if (size > INT_MAX || size+1 > (size_t) nbytes) {
298359
fprintf(stderr,"grb2_get_meta: size of metadata is too big.\n");
299-
return 1;
360+
return 13;
300361
}
301362
err = wgrib2_get_mem_buffer(meta, size, 18);
302363
if (err == 0) meta[size] = 0; /* end the string */
@@ -306,7 +367,16 @@ int grb2_get_meta(unsigned char *meta, int nbytes) {
306367
/**
307368
* Get the size of the memory buffer for grid metadata stored in RPN register.
308369
*
309-
* @return Size of the memory buffer for grid metadata, or 0 if an error occurred.
370+
* ### Program History Log
371+
* Date | Programmer | Comments
372+
* -----|------------|---------
373+
* 3/2018 | W. Ebisuzaki | Initial
374+
* 7/2026 | A. Stahl | New error codes for testing purposes + replaced META w/ GRIDMETA
375+
*
376+
* @return Size of the memory buffer for grid metadata on success, error code otherwise
377+
* - 0 :: Failure in wgrib2_get_mem_buffer_size()
378+
* - -1 :: last find did not work
379+
* - -2 :: grb2_inq did not request reading grid metadata
310380
*
311381
* @note Grid metadata stored in register 17.
312382
*
@@ -317,11 +387,11 @@ int grb2_size_gridmeta(void) {
317387

318388
if (good == 0) {
319389
fprintf(stderr,"grb2_size_gridmeta: last find did not work.\n");
320-
return 0;
390+
return -1;
321391
}
322-
if ((last_options & META) == 0) {
392+
if ((last_options & GRIDMETA) == 0) {
323393
fprintf(stderr,"grb2_size_gridmeta: grb2_inq did not request reading gridmetadata\n");
324-
return 0;
394+
return -2;
325395
}
326396
size = (unsigned int) wgrib2_get_mem_buffer_size(17);
327397
if (size == 0) return 0;
@@ -331,10 +401,21 @@ int grb2_size_gridmeta(void) {
331401
/**
332402
* Get memory-copy of grid metadata from RPN register.
333403
*
404+
* ### Program History Log
405+
* Date | Programmer | Comments
406+
* -----|------------|---------
407+
* 3/2018 | W. Ebisuzaki | Initial
408+
* 7/2026 | A. Stahl | New error codes for testing purposes
409+
*
334410
* @param meta Pointer to the grid metadata array.
335411
* @param nbytes Size of the grid metadata buffer.
336412
*
337-
* @return 0 on success, error code otherwise.
413+
* @return
414+
* - 0 :: success
415+
* - 10 :: last find did not work
416+
* - 11 :: grb2_inq did not request reading grid metadata
417+
* - 12 :: size of grid metadata = 0, grid problem?
418+
* - 13 :: size of metadata is too big
338419
*
339420
* @note Grid metadata stored in register 17.
340421
*
@@ -346,23 +427,39 @@ int grb2_get_gridmeta(unsigned char *meta, int nbytes) {
346427

347428
if (good == 0) {
348429
fprintf(stderr,"grb2_get_gridmeta: last find did not work.\n");
349-
return 1;
430+
return 10;
350431
}
351432
if ((last_options & GRIDMETA) == 0) {
352433
fprintf(stderr,"grb2_get_gridmeta: grb2_inq did not request reading metadata.\n");
353-
return 1;
434+
return 11;
354435
}
355436

356437
size = wgrib2_get_mem_buffer_size(17);
357438
if (size == 0) {
358439
fprintf(stderr,"grb2_get_gridmeta: size of gridmeta = 0, grid problem?.\n");
359-
return 1;
440+
return 12;
360441
}
361442
if (size > INT_MAX || size+1 > (size_t) nbytes) {
362443
fprintf(stderr,"grb2_get_gridmeta: size of metadata is too big.\n");
363-
return 1;
444+
return 13;
364445
}
365446
err = wgrib2_get_mem_buffer(meta, size, 17);
366447
if (err == 0) meta[size] = 0; /* end the string */
367448
return err;
368449
}
450+
451+
/**
452+
* For testing purposes only. Set the state of the static variables last_options, good, and npnts.
453+
*
454+
* @param last_options_val The value to set for last_options. Bitwise OR of option flags used in last inquiry.
455+
* options = SEQUENTIAL | DATA | LATLON | WENS | RAW_ORDER | META | GRIDMETA | REGEX
456+
* @param good_val The value to set for good. It is a flag indicating if last inquiry was successful. (0 = failure, otherwise success)
457+
* @param npnts_val The value to set for npnts. It represents the number of data points in the last inquiry.
458+
*
459+
* @author Alyson Stahl @date 7/2026
460+
*/
461+
void grb2_inq_set_state(int last_options_val, int good_val, int npnts_val) {
462+
last_options = last_options_val;
463+
good = good_val;
464+
npnts = npnts_val;
465+
}

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ if (BUILD_LIB)
241241
build_c_api_test(test_grb2_cmd)
242242
endif()
243243
build_c_api_test(test_grb2_mk_inv)
244+
build_c_api_test(test_grb2_inq)
244245
build_test(test_string2time_unit)
245246
build_test(test_read_latlon)
246247
build_test(test_pdt_len)

0 commit comments

Comments
 (0)