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

Decompose cksum_read into independent functions.

parent 5476fc6f
......@@ -301,6 +301,8 @@ tests/cksum.c -text
tests/cksum.h -text
tests/cksum_read.c -text
tests/cksum_write.c -text
tests/ensure_array_size.c -text
tests/ensure_array_size.h -text
tests/pio_cksum_asynch.in -text
tests/pio_cksum_fpguard.in -text
tests/pio_cksum_mpinonb.in -text
......
......@@ -8,7 +8,9 @@ check_PROGRAMS = test_grib cksum_write cksum_read pio_write
#
test_grib_SOURCES = test_grib.c
cksum_write_SOURCES = cksum_write.c cksum.c cksum.h
cksum_read_SOURCES = cksum_read.c cksum.c cksum.h
cksum_read_SOURCES = cksum_read.c \
cksum.c cksum.h \
ensure_array_size.h ensure_array_size.c
pio_write_SOURCES = pio_write.c cksum.h cksum.c
#
LDADD = -L../src -lcdi -lm
......
......@@ -65,7 +65,8 @@ CONFIG_CLEAN_FILES = test_cksum_grib test_cksum_nc test_cksum_nc2 \
test_cksum_ieg pio_write_run pio_cksum_mpinonb \
pio_cksum_fpguard pio_cksum_asynch pio_cksum_writer
CONFIG_CLEAN_VPATH_FILES =
am_cksum_read_OBJECTS = cksum_read.$(OBJEXT) cksum.$(OBJEXT)
am_cksum_read_OBJECTS = cksum_read.$(OBJEXT) cksum.$(OBJEXT) \
ensure_array_size.$(OBJEXT)
cksum_read_OBJECTS = $(am_cksum_read_OBJECTS)
cksum_read_LDADD = $(LDADD)
cksum_read_DEPENDENCIES =
......@@ -288,7 +289,10 @@ TESTS = test_grib.sh test_cksum_grib test_cksum_nc test_cksum_extra \
#
test_grib_SOURCES = test_grib.c
cksum_write_SOURCES = cksum_write.c cksum.c cksum.h
cksum_read_SOURCES = cksum_read.c cksum.c cksum.h
cksum_read_SOURCES = cksum_read.c \
cksum.c cksum.h \
ensure_array_size.h ensure_array_size.c
pio_write_SOURCES = pio_write.c cksum.h cksum.c
#
LDADD = -L../src -lcdi -lm
......@@ -387,6 +391,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cksum.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cksum_read.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cksum_write.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ensure_array_size.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pio_write.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_grib.Po@am__quote@
......
......@@ -5,25 +5,105 @@
#include "cdi.h"
#include "cksum.h"
#include "ensure_array_size.h"
#include "pio_util.h"
int main(int argc, char *argv[])
struct cksum_table
{
int code;
uint32_t cksum;
};
struct varSize
{
int x, y, z;
size_t chars;
};
static struct cksum_table *
read_table(const char *table_fname, size_t *table_len)
{
struct cksum_table *table = NULL;
FILE *tablefp;
size_t table_size = 0, table_used = 0;
unsigned long cksum_temp;
int code;
if (!(tablefp = fopen(table_fname, "r")))
{
perror("failed to open table file");
*table_len = -1;
return NULL;
}
while (fscanf(tablefp, "%08lx %d\n", &cksum_temp, &code) == 2)
{
ENSURE_ARRAY_SIZE(table, table_size, table_used + 1);
table[table_used].code = code;
table[table_used].cksum = cksum_temp;
++table_used;
}
fclose(tablefp);
*table_len = table_used;
return table;
}
static int
compare_checksums(struct cksum_table a[], size_t a_size, const char *src_a,
struct cksum_table b[], size_t b_size, const char *src_b)
{
int checked_a[a_size], checked_b[b_size];
size_t i, j;
int retcode = EXIT_SUCCESS;
for (i = 0; i < a_size; ++i)
checked_a[i] = 0;
for (j = 0; j < b_size; ++j)
checked_b[j] = 0;
for (j = 0; j < b_size; ++j)
for (i = 0; i < a_size; ++i)
if (a[i].code == b[j].code)
{
if (a[i].cksum != b[j].cksum)
{
fprintf(stderr, "checksum error for varID %d, code %d!\n"
"%08lx != %08lx\n", i, a[i].code,
(unsigned long)a[i].cksum, b[j].cksum);
retcode = EXIT_FAILURE;
}
checked_a[i] = 1;
checked_b[j] = 1;
break;
}
for (i = 0; i < a_size; ++i)
if (!checked_a[i])
{
fprintf(stderr, "variable %d, code %d from %s not checked!\n",
i, a[i].code, src_a);
retcode = EXIT_FAILURE;
}
for (j = 0; j < b_size; ++j)
if (!checked_b[j])
{
fprintf(stderr, "variable %d, code %d from %s not checked!\n",
j, b[j].code, src_b);
retcode = EXIT_FAILURE;
}
return retcode;
}
static struct cksum_table *
cksum_stream(const char *fname, size_t *table_len)
{
int taxisID, vlistID, streamID, tsID;
int vdate, vtime, nvars, ngrids, nzaxis;
int i;
uint32_t *checksum_state;
struct {
int x, y, z;
size_t chars;
} *varSize;
struct varSize *varSize;
double **var;
char *fname = "example.grb", *table_fname = "example.cksum";
if (argc > 1)
fname = argv[1];
if (argc > 2)
table_fname = argv[2];
// Open the dataset
streamID = streamOpenRead(fname);
......@@ -31,23 +111,24 @@ int main(int argc, char *argv[])
{
fprintf(stderr, "Cannot open data input file %s: %s\n",
fname, cdiStringError(streamID));
return(1);
return NULL;
}
// Get the variable list of the dataset
vlistID = streamInqVlist(streamID);
nvars = vlistNvars(vlistID);
var = malloc(nvars * sizeof (var[0]));
checksum_state = calloc(nvars, sizeof (checksum_state[0]));
ngrids = vlistNgrids(vlistID);
nzaxis = vlistNzaxis(vlistID);
if (nzaxis < 0 || ngrids < 0)
{
fprintf(stderr, "Error in grid/zaxis count query %d:%d\n",
ngrids, nzaxis);
return NULL;
}
varSize = malloc(nvars * sizeof (varSize[0]));
var = xmalloc(nvars * sizeof (var[0]));
checksum_state = xcalloc(nvars, sizeof (checksum_state[0]));
varSize = xmalloc(nvars * sizeof (varSize[0]));
for (i = 0; i < nvars; ++i)
{
......@@ -84,7 +165,7 @@ int main(int argc, char *argv[])
if (varSize[i].y == 0) varSize[i].y = 1;
varSize[i].chars = (size_t)varSize[i].x * varSize[i].y * varSize[i].z
* sizeof (var[0][0]);
var[i] = malloc(varSize[i].chars);
var[i] = xmalloc(varSize[i].chars);
}
// Get the Time axis from the variable list
......@@ -109,52 +190,47 @@ int main(int argc, char *argv[])
++tsID;
}
struct cksum_table *file_vars = xmalloc(nvars * sizeof (file_vars[0]));
*table_len = nvars;
for (i = 0; i < nvars; ++i)
{
file_vars[i].code = vlistInqVarCode(vlistID, i);
file_vars[i].cksum = memcrc_finish(checksum_state + i,
(off_t)varSize[i].chars * tsID);
}
// Close the input stream
streamClose(streamID);
return file_vars;
}
int main(int argc, char *argv[])
{
char *fname = "example.grb", *table_fname = "example.cksum";
if (argc > 1)
fname = argv[1];
if (argc > 2)
table_fname = argv[2];
// compute checksums from data file
size_t nvars;
struct cksum_table *file_vars = cksum_stream(fname, &nvars);
if (!file_vars)
exit(EXIT_FAILURE);
// check checksums from table file
int retcode = EXIT_SUCCESS;
int retcode;
{
uint32_t cksum[nvars];
unsigned long cksum_temp;
int code, varCodes[nvars], checked[nvars];
FILE *tablefp;
for (i = 0; i < nvars; ++i)
{
cksum[i] = memcrc_finish(checksum_state + i,
(off_t)varSize[i].chars * tsID);
varCodes[i] = vlistInqVarCode(vlistID, i);
checked[i] = 0;
}
if (!(tablefp = fopen(table_fname, "r")))
{
perror("failed to open table file");
exit(EXIT_FAILURE);
}
while (fscanf(tablefp, "%08lx %d\n", &cksum_temp, &code) == 2)
{
for (i = 0; i < nvars; ++i)
if (varCodes[i] == code)
{
if (cksum[i] != cksum_temp)
{
fprintf(stderr, "checksum error for varID %d, code %d!\n"
"%08lx != %08lx\n", i, code,
(unsigned long)cksum[i], cksum_temp);
retcode = EXIT_FAILURE;
}
checked[i] = 1;
break;
}
}
fclose(tablefp);
for (i = 0; i < nvars; ++i)
if (!checked[i])
{
fprintf(stderr, "variable %d, code %d in file not checked!\n",
i, varCodes[i]);
exit(EXIT_FAILURE);
}
size_t num_ref_entries;
struct cksum_table *ref_var_table
= read_table(table_fname, &num_ref_entries);
if (num_ref_entries == -1)
exit(EXIT_FAILURE);
retcode = compare_checksums(file_vars, nvars, "file",
ref_var_table, num_ref_entries,
"reference table");
free(ref_var_table);
}
// Close the input stream
streamClose(streamID);
return retcode;
}
......
#include <stdlib.h>
#include "ensure_array_size.h"
#include "pio_util.h"
void
realloc_array(void **array, size_t elem_size, size_t *curr_array_size,
size_t requested_size)
{
const size_t array_inc_size = (1024 + elem_size - 1)/ elem_size;
*curr_array_size = array_inc_size
* ((requested_size + array_inc_size) / array_inc_size);
*array = realloc(*array, *curr_array_size * elem_size);
if (!*array)
xabort("reallocation failed");
}
#ifndef ENSURE_ARRAY_SIZE_H
#define ENSURE_ARRAY_SIZE_H
void
realloc_array(void **array, size_t elem_size, size_t *curr_array_size,
size_t requested_size);
#define ENSURE_ARRAY_SIZE(arrayp, curr_array_size, req_size) \
do { \
if ((req_size) > (curr_array_size)) \
{ \
size_t casize = (curr_array_size); \
\
realloc_array((void **)&(arrayp), sizeof(*(arrayp)), &casize, \
(req_size)); \
(curr_array_size) = casize; \
} \
} \
while(0)
#endif
Supports Markdown
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