Testing: Add test for codes_get_message_copy

This commit is contained in:
Shahram Najm 2023-02-20 16:51:21 +00:00
parent 067a3692ea
commit f3fe51ab6a
3 changed files with 82 additions and 0 deletions

View File

@ -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

View File

@ -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 <stdio.h>
#undef NDEBUG
#include <assert.h>
#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;
}

23
tests/grib_copy_message.sh Executable file
View File

@ -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