From c51fa655dfabba0d8b310fbb8c5ceaf7d6bc88eb Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Sun, 26 Jun 2022 16:55:50 +0100 Subject: [PATCH] Geoiterator: Fix the has_next function --- src/grib_iterator_class_gen.c | 4 ++- tests/grib_geo_iter.c | 58 +++++++++++++++++++++++++++++++++++ tests/grib_geo_iter.sh | 15 +++++++++ 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 tests/grib_geo_iter.c create mode 100755 tests/grib_geo_iter.sh diff --git a/src/grib_iterator_class_gen.c b/src/grib_iterator_class_gen.c index 0d5ab2c13..eaba5a623 100644 --- a/src/grib_iterator_class_gen.c +++ b/src/grib_iterator_class_gen.c @@ -228,5 +228,7 @@ static long has_next(grib_iterator* iter) { if (iter->data == NULL) return 0; - return iter->nv - iter->e; + if (iter->e >= (long)(iter->nv - 1)) + return 0; + return 1; } diff --git a/tests/grib_geo_iter.c b/tests/grib_geo_iter.c new file mode 100644 index 000000000..6fa2f7ebf --- /dev/null +++ b/tests/grib_geo_iter.c @@ -0,0 +1,58 @@ +/* + * (C) Copyright 2005- ECMWF. + * + * This software is licensed under the terms of the Apache Licence Version 2.0 + * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. + * + * In applying this licence, ECMWF does not waive the privileges and immunities granted to it by + * virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction. + */ + +#include +#include +#include + +#include "grib_api.h" + +static void usage(const char* prog) +{ + printf("Usage: %s grib_file\n", prog); + exit(1); +} + +int main(int argc, char** argv) +{ + FILE* in = NULL; + int err = 0; + double lat, lon, value; + int n = 0; + const int numberOfDataPoints = 13280; + const char* sample_filename = "gg_sfc_grib2"; + grib_handle* h = NULL; + grib_iterator* iter = NULL; + + h = grib_handle_new_from_samples(0, sample_filename); + assert(h); + + iter = grib_iterator_new(h, 0, &err); + assert(!err); + assert(iter); + + assert(grib_iterator_has_next(iter)); + n = 0; + + while (grib_iterator_next(iter, &lat, &lon, &value)) { + if (n < numberOfDataPoints - 1) + assert(grib_iterator_has_next(iter)); + n++; + } + assert(n == numberOfDataPoints); + + assert(grib_iterator_has_next(iter) == 0); + + grib_iterator_delete(iter); + + grib_handle_delete(h); + + return 0; +} diff --git a/tests/grib_geo_iter.sh b/tests/grib_geo_iter.sh new file mode 100755 index 000000000..4c3af0c86 --- /dev/null +++ b/tests/grib_geo_iter.sh @@ -0,0 +1,15 @@ +#!/bin/sh +# (C) Copyright 2005- ECMWF. +# +# This software is licensed under the terms of the Apache Licence Version 2.0 +# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. +# +# In applying this licence, ECMWF does not waive the privileges and immunities granted to it by +# virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction. +# + +. ./include.ctest.sh + +label="grib_geo_iter_test" + +$EXEC ${test_dir}/grib_geo_iter