Commit fac5b22b authored by Thomas Jahns's avatar Thomas Jahns 🤸
Browse files

Add code to test netCDF attribute handling.

* This exercises the code for which a bug was introduced in r3181 and
  fixed in r3200.
parent 2a9fdf39
......@@ -8,6 +8,9 @@
#include "cdi.h"
static void
printAtts(int vlistID);
int main(int argc, const char **argv)
{
const char *fname = "test.nc";
......@@ -30,11 +33,13 @@ int main(int argc, const char **argv)
size_t bufSize = 0;
size_t allNmissSum = 0;
printAtts(vlistID);
for (int tsID = 0; streamInqTimestep(streamID, tsID); ++tsID)
{
for (size_t varID = 0; varID < nVars; ++varID)
{
size_t memSize = (size_t)vlistInqVarSize(vlistID, varID)
size_t memSize = (size_t)vlistInqVarSize(vlistID, (int)varID)
* sizeof (double);
int nmiss;
if (memSize > bufSize)
......@@ -58,6 +63,111 @@ int main(int argc, const char **argv)
return EXIT_SUCCESS;
}
static void
printAtts(int vlistID)
{
size_t nVars = (size_t)vlistNvars(vlistID);
static const char globDesc[] = "global",
varDescPrefix[] = "variable ";
size_t digitsPerInt = 9;
size_t bufSize = digitsPerInt + sizeof (varDescPrefix);
void *restrict buf = malloc(bufSize);
if (!buf)
{
perror("attribute buffer resize failed");
exit(EXIT_FAILURE);
}
for (size_t varIdx = 0; varIdx <= nVars; ++varIdx)
{
int nAtts, attType, attLen;
char attName[CDI_MAX_NAME + 1];
int varID = (int)varIdx - 1;
vlistInqNatts(vlistID, varID, &nAtts);
for (size_t attIdx = 0; attIdx < (size_t)nAtts; attIdx++ )
{
int rc = vlistInqAtt(vlistID, varID, (int)attIdx,
attName, &attType, &attLen);
{
const char *varDesc = varIdx > 0
? (sprintf(buf, "%s%d", varDescPrefix,
vlistInqVarCode(vlistID, varID)), buf)
: globDesc;
printf("%s attribute \"%s\", value: ",
varDesc, attName);
}
if (attLen < 0)
goto attGetFail;
size_t elemSize = 0;
switch (attType)
{
case DATATYPE_TXT:
elemSize = 1;
break;
case DATATYPE_FLT:
elemSize = sizeof (double);
break;
case DATATYPE_INT:
elemSize = sizeof (int);
break;
}
size_t attSize = elemSize * ((size_t)attLen + 1);
if (attSize > bufSize)
{
if (!(buf = realloc(buf, attSize)))
{
perror("attribute buffer resize failed");
exit(EXIT_FAILURE);
}
}
switch (attType)
{
case DATATYPE_TXT:
rc = vlistInqAttTxt(vlistID, (int)varID, attName,
attLen, buf);
if (rc == CDI_NOERR)
printf("\"%.*s\"", attLen, (char *)buf);
break;
case DATATYPE_FLT:
rc = vlistInqAttFlt(vlistID, (int)varID, attName,
attLen + 1, buf);
if (rc == CDI_NOERR && attLen)
{
const double *restrict dp = buf;
printf("%10g", dp[0]);
for (size_t i = 1; i < (size_t)attLen; ++i)
printf(", %10g", dp[i]);
}
break;
case DATATYPE_INT:
rc = vlistInqAttInt(vlistID, (int)varID, attName,
attLen + 1, buf);
if (rc == CDI_NOERR && attLen)
{
const int *restrict ip = buf;
printf("%d", ip[0]);
for (size_t i = 1; i < (size_t)attLen; ++i)
printf(", %d", ip[i]);
}
break;
}
if (rc == CDI_NOERR)
{
putchar('\n');
}
else
{
attGetFail:
puts("error retrieving value");
}
}
}
free(buf);
}
/*
* Local Variables:
* c-file-style: "Java"
......
......@@ -18,6 +18,11 @@ if [ "$missValueCount" -ne 1 ]; then
echo "Mismatch in expected number of missing values!" >&2
exit 1
fi
if ! echo "$digest" | grep 'variable 1 attribute "CDI Text Attribute test, created by", value: "CDI test_cdf_write"' >/dev/null; then
echo "Expected attribute not found" >&2
exit 1
fi
#
# Local Variables:
# mode: sh
......
......@@ -27,7 +27,7 @@ int main(int argc, const char **argv)
{
/* todo: handle optional arguments here to increase test coverage */
const char *fname = "test.nc";
if (argc)
if (argc > 1)
fname = argv[1];
int streamID = streamOpenWrite(fname, FILETYPE_NC);
......@@ -73,6 +73,11 @@ int main(int argc, const char **argv)
int vlistID = vlistCreate();
int varID = vlistDefVar(vlistID, gridID, zaxisID, TSTEP_INSTANT);
vlistDefVarMissval(vlistID, varID, missValue);
{
static const char creatorText[] = "CDI test_cdf_write";
vlistDefAttTxt(vlistID, varID, "CDI Text Attribute test, created by",
sizeof (creatorText) - 1, creatorText);
}
int taxisID = taxisCreate(TAXIS_ABSOLUTE);
vlistDefTaxis(vlistID, taxisID);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment