diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 867b18a9a..589278ec8 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -44,6 +44,7 @@ list(APPEND test_c_bins grib_sh_ieee64 grib_ieee grib_set_bytes + grib_copy_message grib_packing_order grib_sh_imag grib_sh_spectral_complex @@ -239,6 +240,7 @@ if( HAVE_BUILD_TOOLS ) grib_grid_mercator grib_global grib_concept + grib_copy_message grib_decimalPrecision grib_bitsPerValue grib_get_fail diff --git a/tests/grib_copy_message.cc b/tests/grib_copy_message.cc new file mode 100644 index 000000000..0fd6bb021 --- /dev/null +++ b/tests/grib_copy_message.cc @@ -0,0 +1,57 @@ +/* + * (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 +#undef NDEBUG +#include +#include "eccodes.h" + +int main(int argc, char* argv[]) +{ + FILE* in = NULL; + codes_handle* source_handle = NULL; + int err = 0; + size_t totalLength = 0, size = 0; + unsigned char* buffer = NULL; + codes_handle* new_handle = NULL; + + assert (argc == 3); + + in = fopen(argv[1], "rb"); + assert(in); + + source_handle = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err); + assert(source_handle); + + // How big is the input GRIB message? + CODES_CHECK(codes_get_message_size(source_handle, &totalLength), 0); + + // Allocate a buffer large enough to hold the message + buffer = (unsigned char*)malloc(totalLength); + size = totalLength; + + // Take a copy of the message into buffer. Now we own it + CODES_CHECK(codes_get_message_copy(source_handle, buffer, &size), 0); + assert(size == totalLength); + codes_handle_delete(source_handle); + + // Now buffer contains a copy of the message + new_handle = codes_handle_new_from_message(0, buffer, totalLength); + assert(new_handle); + + // Change something and write it out + CODES_CHECK(codes_set_long(new_handle, "hour", 18), 0); + CODES_CHECK(codes_write_message(new_handle, argv[2], "w"), 0); + codes_handle_delete(new_handle); + free(buffer); + + fclose(in); + + return 0; +} diff --git a/tests/grib_copy_message.sh b/tests/grib_copy_message.sh new file mode 100755 index 000000000..4d0786340 --- /dev/null +++ b/tests/grib_copy_message.sh @@ -0,0 +1,23 @@ +#!/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_copy_message_test" +temp=temp.$label.grib + +infile=$data_dir/sample.grib2 +$EXEC ${test_dir}/grib_copy_message $infile $temp + +# The hour key was changed but nothing else +${tools_dir}/grib_compare -b hour $infile $temp + +# Clean up test outputs +rm -f $temp