Skip to content

Commit 6491896

Browse files
AlysonStahl-NOAAalyson
andauthored
Unit tests for smallest_grib and smallest_4 (#556)
* add some data files * another data file * replace data file * another ref file * complete test script * remove the print statement * Test for smallest_4 * fix error --------- Co-authored-by: alyson <alyson@ubuntu.myguest.virtualbox.org>
1 parent 63564d4 commit 6491896

11 files changed

Lines changed: 415 additions & 3 deletions

aux_progs/smallest_4.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,12 @@
4343
* @param argc Number of command line arguments
4444
* @param argv Array of command line arguments
4545
*
46-
* @return 0 on success, non-zero on error
47-
*
46+
* @return
47+
* - 0 :: Success
48+
* - 1 :: First 4 bytes not "GRIB"
49+
* - 4 :: Read error occurred
50+
* - 8 :: Exact error is written to stderr
51+
*
4852
* @author Wesley Ebisuzaki @date 02/2011
4953
*/
5054
int main(int argc, char **argv) {

aux_progs/smallest_grib2.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@
4343
* @param argc Number of command line arguments
4444
* @param argv Array of command line arguments
4545
*
46-
* @return 0 on success, non-zero on error
46+
* @return
47+
* - 0 :: Success
48+
* - 1 :: First 4 bytes not "GRIB"
49+
* - 4 :: Read error occurred
50+
* - 8 :: Exact error is written to stderr
4751
*
4852
* @author Wesley Ebisuzaki @date 2010
4953
*/

tests/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ copy_test_data(png_4bits.png)
125125
copy_test_data(large_png.grb2)
126126
copy_test_data(ref_c3_overflow.grib2)
127127
copy_test_data(delayed_error.grib2)
128+
copy_test_data(ref_smallest_grib.grb2)
129+
copy_test_data(bad_sec0_1.grb2)
130+
copy_test_data(bad_sec0_2.grb2)
131+
copy_test_data(bad_sec0_3.grb2)
132+
copy_test_data(bad_sec0_4.grb2)
133+
copy_test_data(bad_sec0_size.grb2)
128134

129135
# Run these shell tests.
130136
shell_test(run_wgrib2_tests)
@@ -134,6 +140,8 @@ shell_test(run_wgrib2_update_table_tests)
134140
shell_test(run_wgrib2_png_tests)
135141
shell_test(run_gmerge_tests)
136142
shell_test(run_wgrib2_delayed_err_tests)
143+
shell_test(run_smallest_grib_tests)
144+
shell_test(run_smallest_4_tests)
137145

138146
if (USE_NETCDF)
139147
shell_test(run_wgrib2_netcdf4_tests)

tests/data/bad_sec0_1.grb2

5.94 MB
Binary file not shown.

tests/data/bad_sec0_2.grb2

5.94 MB
Binary file not shown.

tests/data/bad_sec0_3.grb2

5.94 MB
Binary file not shown.

tests/data/bad_sec0_4.grb2

5.94 MB
Binary file not shown.

tests/data/bad_sec0_size.grb2

175 KB
Binary file not shown.

tests/data/ref_smallest_grib.grb2

176 KB
Binary file not shown.

tests/run_smallest_4_tests.sh

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
#!/bin/sh
2+
# This script runs a series of tests of the smallest_4 program.
3+
#
4+
# Alyson Stahl 7/2026
5+
6+
set -e
7+
8+
echo ""
9+
echo "*** Running smallest_4 tests"
10+
11+
ref_file=data/ref_smallest_grib.grb2
12+
output=smallest_grib.grb2
13+
file1=data/png_4bits.png
14+
file2=data/large_png.grb2
15+
file3=data/ref_c3_overflow.grib2
16+
file4=data/LARGECAT220250305_12_1443copy.grib2
17+
18+
../aux_progs/smallest_4 && exit 1
19+
if [ $? -ne 8 ]; then
20+
echo "Error: Test failed for invalid number of arguments."
21+
exit 1
22+
fi
23+
24+
../aux_progs/smallest_4 /bad_directory/"$output" "$file1" "$file2" "$file3" "$file4" && exit 1
25+
if [ $? -ne 8 ]; then
26+
echo "Error: Test failed for bad output file."
27+
exit 1
28+
fi
29+
30+
../aux_progs/smallest_4 "$output" badfile.grb2 "$file2" "$file3" "$file4" && exit 1
31+
if [ $? -ne 8 ]; then
32+
echo "Error: Test failed for bad input file 1."
33+
exit 1
34+
fi
35+
36+
../aux_progs/smallest_4 "$output" "$file1" badfile.grb2 "$file3" "$file4" && exit 1
37+
if [ $? -ne 8 ]; then
38+
echo "Error: Test failed for bad input file 2."
39+
exit 1
40+
fi
41+
42+
../aux_progs/smallest_4 "$output" "$file1" "$file2" badfile.grb2 "$file4" && exit 1
43+
if [ $? -ne 8 ]; then
44+
echo "Error: Test failed for bad input file 3."
45+
exit 1
46+
fi
47+
48+
../aux_progs/smallest_4 "$output" "$file1" "$file2" "$file3" badfile.grb2 && exit 1
49+
if [ $? -ne 8 ]; then
50+
echo "Error: Test failed for bad input file 4."
51+
exit 1
52+
fi
53+
54+
# Message 1 Errors
55+
56+
# First 4 bytes does not start with GRIB
57+
../aux_progs/smallest_4 "$output" data/bad_sec0_1.grb2 "$file2" "$file3" "$file4" && exit 1
58+
if [ $? -ne 1 ]; then
59+
echo "Error: Test failed for bad sec0 message at byte 1 in file 1."
60+
exit 1
61+
fi
62+
63+
../aux_progs/smallest_4 "$output" data/bad_sec0_2.grb2 "$file2" "$file3" "$file4" && exit 1
64+
if [ $? -ne 1 ]; then
65+
echo "Error: Test failed for bad sec0 message at byte 2 in file 1."
66+
exit 1
67+
fi
68+
69+
../aux_progs/smallest_4 "$output" data/bad_sec0_3.grb2 "$file2" "$file3" "$file4" && exit 1
70+
if [ $? -ne 1 ]; then
71+
echo "Error: Test failed for bad sec0 message at byte 3 in file 1."
72+
exit 1
73+
fi
74+
75+
../aux_progs/smallest_4 "$output" data/bad_sec0_4.grb2 "$file2" "$file3" "$file4" && exit 1
76+
if [ $? -ne 1 ]; then
77+
echo "Error: Test failed for bad sec0 message at byte 4 in file 1."
78+
exit 1
79+
fi
80+
81+
../aux_progs/smallest_4 "$output" data/bad_sec0_size.grb2 "$file2" "$file3" "$file4" && exit 1
82+
if [ $? -ne 4 ]; then
83+
echo "Error: Test failed for read error in file 1."
84+
exit 1
85+
fi
86+
87+
# Message 2 Errors
88+
89+
# First 4 bytes does not start with GRIB
90+
../aux_progs/smallest_4 "$output" "$file1" data/bad_sec0_1.grb2 "$file3" "$file4" && exit 1
91+
if [ $? -ne 1 ]; then
92+
echo "Error: Test failed for bad sec0 message at byte 1 in file 2."
93+
exit 1
94+
fi
95+
96+
../aux_progs/smallest_4 "$output" "$file1" data/bad_sec0_2.grb2 "$file3" "$file4" && exit 1
97+
if [ $? -ne 1 ]; then
98+
echo "Error: Test failed for bad sec0 message at byte 2 in file 2."
99+
exit 1
100+
fi
101+
102+
../aux_progs/smallest_4 "$output" "$file1" data/bad_sec0_3.grb2 "$file3" "$file4" && exit 1
103+
if [ $? -ne 1 ]; then
104+
echo "Error: Test failed for bad sec0 message at byte 3 in file 2."
105+
exit 1
106+
fi
107+
108+
../aux_progs/smallest_4 "$output" "$file1" data/bad_sec0_4.grb2 "$file3" "$file4" && exit 1
109+
if [ $? -ne 1 ]; then
110+
echo "Error: Test failed for bad sec0 message at byte 4 in file 2."
111+
exit 1
112+
fi
113+
114+
../aux_progs/smallest_4 "$output" "$file1" data/bad_sec0_size.grb2 "$file3" "$file4" && exit 1
115+
if [ $? -ne 4 ]; then
116+
echo "Error: Test failed for read error in file 2."
117+
exit 1
118+
fi
119+
120+
# Message 3 Errors
121+
122+
# First 4 bytes does not start with GRIB
123+
../aux_progs/smallest_4 "$output" "$file1" "$file2" data/bad_sec0_1.grb2 "$file4" && exit 1
124+
if [ $? -ne 1 ]; then
125+
echo "Error: Test failed for bad sec0 message at byte 1 in file 3."
126+
exit 1
127+
fi
128+
129+
../aux_progs/smallest_4 "$output" "$file1" "$file2" data/bad_sec0_2.grb2 "$file4" && exit 1
130+
if [ $? -ne 1 ]; then
131+
echo "Error: Test failed for bad sec0 message at byte 2 in file 3."
132+
exit 1
133+
fi
134+
135+
../aux_progs/smallest_4 "$output" "$file1" "$file2" data/bad_sec0_3.grb2 "$file4" && exit 1
136+
if [ $? -ne 1 ]; then
137+
echo "Error: Test failed for bad sec0 message at byte 3 in file 3."
138+
exit 1
139+
fi
140+
141+
../aux_progs/smallest_4 "$output" "$file1" "$file2" data/bad_sec0_4.grb2 "$file4" && exit 1
142+
if [ $? -ne 1 ]; then
143+
echo "Error: Test failed for bad sec0 message at byte 4 in file 3."
144+
exit 1
145+
fi
146+
147+
../aux_progs/smallest_4 "$output" "$file1" "$file2" data/bad_sec0_size.grb2 "$file4" && exit 1
148+
if [ $? -ne 4 ]; then
149+
echo "Error: Test failed for read error in file 3."
150+
exit 1
151+
fi
152+
153+
# Message 4 Errors
154+
155+
# First 4 bytes does not start with GRIB
156+
../aux_progs/smallest_4 "$output" "$file1" "$file2" "$file3" data/bad_sec0_1.grb2 && exit 1
157+
if [ $? -ne 1 ]; then
158+
echo "Error: Test failed for bad sec0 message at byte 1 in file 4."
159+
exit 1
160+
fi
161+
162+
../aux_progs/smallest_4 "$output" "$file1" "$file2" "$file3" data/bad_sec0_2.grb2 && exit 1
163+
if [ $? -ne 1 ]; then
164+
echo "Error: Test failed for bad sec0 message at byte 2 in file 4."
165+
exit 1
166+
fi
167+
168+
../aux_progs/smallest_4 "$output" "$file1" "$file2" "$file3" data/bad_sec0_3.grb2 && exit 1
169+
if [ $? -ne 1 ]; then
170+
echo "Error: Test failed for bad sec0 message at byte 3 in file 4."
171+
exit 1
172+
fi
173+
174+
../aux_progs/smallest_4 "$output" "$file1" "$file2" "$file3" data/bad_sec0_4.grb2 && exit 1
175+
if [ $? -ne 1 ]; then
176+
echo "Error: Test failed for bad sec0 message at byte 4 in file 4."
177+
exit 1
178+
fi
179+
180+
../aux_progs/smallest_4 "$output" "$file1" "$file2" "$file3" data/bad_sec0_size.grb2 && exit 1
181+
if [ $? -ne 4 ]; then
182+
echo "Error: Test failed for read error in file 4."
183+
exit 1
184+
fi
185+
186+
# Smallest = 1
187+
../aux_progs/smallest_4 "$output" "$file1" "$file2" "$file3" "$file4"
188+
ret=$(../src/wgrib2 "$output")
189+
expected=$(../src/wgrib2 "$ref_file")
190+
if [ "$ret" != "$expected" ]; then
191+
echo "Error: Smallest grib test failed where smallest is file 1."
192+
exit 1
193+
fi
194+
195+
# Smallest = 2
196+
../aux_progs/smallest_4 "$output" "$file2" "$file1" "$file3" "$file4"
197+
ret=$(../src/wgrib2 "$output")
198+
expected=$(../src/wgrib2 "$ref_file")
199+
if [ "$ret" != "$expected" ]; then
200+
echo "Error: Smallest grib test failed where smallest is file 2."
201+
exit 1
202+
fi
203+
204+
# Smallest = 3
205+
../aux_progs/smallest_4 "$output" "$file3" "$file2" "$file1" "$file4"
206+
ret=$(../src/wgrib2 "$output")
207+
expected=$(../src/wgrib2 "$ref_file")
208+
if [ "$ret" != "$expected" ]; then
209+
echo "Error: Smallest grib test failed where smallest is file 3."
210+
exit 1
211+
fi
212+
213+
# Smallest = 4
214+
../aux_progs/smallest_4 "$output" "$file4" "$file2" "$file3" "$file1"
215+
ret=$(../src/wgrib2 "$output")
216+
expected=$(../src/wgrib2 "$ref_file")
217+
if [ "$ret" != "$expected" ]; then
218+
echo "Error: Smallest grib test failed where smallest is file 4."
219+
exit 1
220+
fi
221+
222+
echo "*** SUCCESS!"
223+
exit 0

0 commit comments

Comments
 (0)