Testing: transient darray

This commit is contained in:
Shahram Najm 2023-12-27 15:09:34 +00:00
parent cf66422eee
commit 075760afc8
3 changed files with 54 additions and 62 deletions

View File

@ -19,7 +19,7 @@
IMPLEMENTS = unpack_double;pack_double
IMPLEMENTS = unpack_long;pack_long;destroy
IMPLEMENTS = init;dump;value_count
IMPLEMENTS = compare;get_native_type
IMPLEMENTS = get_native_type
MEMBERS=grib_darray* arr
MEMBERS=int type;
END_CLASS_DEF
@ -45,7 +45,6 @@ static int value_count(grib_accessor*, long*);
static void destroy(grib_context*, grib_accessor*);
static void dump(grib_accessor*, grib_dumper*);
static void init(grib_accessor*, const long, grib_arguments*);
static int compare(grib_accessor*, grib_accessor*);
typedef struct grib_accessor_transient_darray
{
@ -96,7 +95,7 @@ static grib_accessor_class _grib_accessor_class_transient_darray = {
0, /* resize */
0, /* nearest_smaller_value */
0, /* next accessor */
&compare, /* compare vs. another accessor */
0, /* compare vs. another accessor */
0, /* unpack only ith value (double) */
0, /* unpack only ith value (float) */
0, /* unpack a given set of elements (double) */
@ -119,23 +118,20 @@ static void init(grib_accessor* a, const long length, grib_arguments* args)
a->length = 0;
}
static void dump(grib_accessor* a, grib_dumper* dumper)
{
/* grib_accessor_transient_darray *self = (grib_accessor_transient_darray*)a; */
grib_dump_double(dumper, a, NULL);
}
static int pack_double(grib_accessor* a, const double* val, size_t* len)
{
grib_accessor_transient_darray* self = (grib_accessor_transient_darray*)a;
size_t i;
if (self->arr)
grib_darray_delete(a->context, self->arr);
self->arr = grib_darray_new(a->context, *len, 10);
for (i = 0; i < *len; i++)
for (size_t i = 0; i < *len; i++)
grib_darray_push(a->context, self->arr, val[i]);
return GRIB_SUCCESS;
@ -144,13 +140,12 @@ static int pack_double(grib_accessor* a, const double* val, size_t* len)
static int pack_long(grib_accessor* a, const long* val, size_t* len)
{
grib_accessor_transient_darray* self = (grib_accessor_transient_darray*)a;
size_t i;
if (self->arr)
grib_darray_delete(a->context, self->arr);
self->arr = grib_darray_new(a->context, *len, 10);
for (i = 0; i < *len; i++)
for (size_t i = 0; i < *len; i++)
grib_darray_push(a->context, self->arr, (double)val[i]);
return GRIB_SUCCESS;
@ -159,8 +154,7 @@ static int pack_long(grib_accessor* a, const long* val, size_t* len)
static int unpack_double(grib_accessor* a, double* val, size_t* len)
{
grib_accessor_transient_darray* self = (grib_accessor_transient_darray*)a;
long count = 0;
size_t i;
long count = 0;
value_count(a, &count);
@ -170,17 +164,16 @@ static int unpack_double(grib_accessor* a, double* val, size_t* len)
}
*len = count;
for (i = 0; i < *len; i++)
for (size_t i = 0; i < *len; i++)
val[i] = self->arr->v[i];
return GRIB_SUCCESS;
}
static int unpack_long(grib_accessor* a, long* val, size_t* len)
{
grib_accessor_transient_darray* self = (grib_accessor_transient_darray*)a;
long count = 0;
size_t i;
long count = 0;
value_count(a, &count);
@ -190,14 +183,12 @@ static int unpack_long(grib_accessor* a, long* val, size_t* len)
}
*len = count;
for (i = 0; i < *len; i++)
for (size_t i = 0; i < *len; i++)
val[i] = (long)self->arr->v[i];
return GRIB_SUCCESS;
}
static void destroy(grib_context* c, grib_accessor* a)
{
grib_accessor_transient_darray* self = (grib_accessor_transient_darray*)a;
@ -216,51 +207,8 @@ static int value_count(grib_accessor* a, long* count)
return 0;
}
static int compare(grib_accessor* a, grib_accessor* b)
{
int retval = 0;
double* aval = 0;
double* bval = 0;
size_t alen = 0;
size_t blen = 0;
int err = 0;
long count = 0;
err = grib_value_count(a, &count);
if (err)
return err;
alen = count;
err = grib_value_count(b, &count);
if (err)
return err;
blen = count;
if (alen != blen)
return GRIB_COUNT_MISMATCH;
aval = (double*)grib_context_malloc(a->context, alen * sizeof(double));
bval = (double*)grib_context_malloc(b->context, blen * sizeof(double));
grib_unpack_double(a, aval, &alen);
grib_unpack_double(b, bval, &blen);
retval = GRIB_SUCCESS;
while (alen != 0) {
if (*bval != *aval)
retval = GRIB_DOUBLE_VALUE_MISMATCH;
alen--;
}
grib_context_free(a->context, aval);
grib_context_free(b->context, bval);
return retval;
}
static int get_native_type(grib_accessor* a)
{
grib_accessor_transient_darray* self = (grib_accessor_transient_darray*)a;
const grib_accessor_transient_darray* self = (grib_accessor_transient_darray*)a;
return self->type;
}

View File

@ -96,6 +96,7 @@ if( HAVE_BUILD_TOOLS )
grib_ifsParam
grib_packing_order
filter_substr
filter_transient_darray
grib_uerra
grib_ecpoint
grib_s2s

View File

@ -0,0 +1,43 @@
#!/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="filter_transient_darray_test"
tempOut=temp.$label.out
tempRef=temp.$label.ref
tempFilt=temp.$label.filt
sample=$ECCODES_SAMPLES_PATH/GRIB2.tmpl
cat > $tempFilt <<EOF
transient dar = { 6, -1.6 };
print "dar as int=[dar:i]";
print "dar as dbl=[dar:d]";
set dar = {-9.99, 14.76, 15, 16, 18};
print "dar now =[dar]";
meta elem1 element(dar, 1);
print "elem1 = [elem1:d]";
EOF
${tools_dir}/grib_filter $tempFilt $sample > $tempOut
cat > $tempRef <<EOF
dar as int=6 -1
dar as dbl=6 -1.6
dar now =-9.99 14.76 15 16 18
elem1 = 14.76
EOF
diff $tempRef $tempOut
# Clean up
rm -f $tempRef $tempOut $tempFilt