changeset 1864:d4b7f4d2e643

Modified test_speed
author bert <bert>
date Wed, 11 Aug 2004 20:48:50 +0000
parents f8c0ff2d4cdd
children 6e4673e73776
files testdir/Makefile.am testdir/test_speed.c
diffstat 2 files changed, 202 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- a/testdir/Makefile.am
+++ b/testdir/Makefile.am
@@ -21,7 +21,7 @@
 
 check_PROGRAMS = minc test_mconv minc_types icv icv_range \
 	icv_dim test_speed icv_dim1 icv_fillvalue \
-	test_xfm create_grid_xfm mincapi
+	test_xfm create_grid_xfm mincapi test_speed
 
 EXTRA_DIST = $(script_tests) $(expect_files) t1.xfm
 
--- a/testdir/test_speed.c
+++ b/testdir/test_speed.c
@@ -1,6 +1,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <minc.h>
+#include <float.h>
 
 #define TRUE 1
 #define FALSE 0
@@ -8,17 +9,196 @@
 #define NORMAL_STATUS 0
 #define ERROR_STATUS 1
 
-main(int argc, char *argv[])
+char *nctypename(nc_type datatype)
+{
+    switch (datatype) {
+    case NC_BYTE:
+        return "byte";
+    case NC_CHAR:
+        return "char";
+    case NC_SHORT:
+        return "short";
+    case NC_INT:
+        return "int";
+    case NC_FLOAT:
+        return "float";
+    case NC_DOUBLE:
+        return "double";
+    }
+}
+
+int
+test_icv_read(char *filename, int xsize, int ysize, double image_min,
+              double image_max, nc_type datatype, char *signtype)
 {
    int icv, cdfid, img, ndims;
-   int xsize, ysize;
-   double image_max, image_min;
    static long coord[MAX_VAR_DIMS];
    static long count[MAX_VAR_DIMS];
    int dim[MAX_VAR_DIMS];
    long dim_size;
-   unsigned char image[512*512];
-   int i, quit;
+   unsigned char *image;
+   int i;
+   double min, max;
+   int n;
+
+   min = DBL_MAX;
+   max = -DBL_MAX;
+
+   image = malloc(xsize * ysize * nctypelen(datatype));
+   if (image == NULL) {
+       return (ERROR_STATUS);
+   }
+
+   /* Create the icv */
+   icv=miicv_create();
+   (void) miicv_setint(icv, MI_ICV_XDIM_DIR, MI_ICV_POSITIVE);
+   (void) miicv_setint(icv, MI_ICV_YDIM_DIR, MI_ICV_POSITIVE);
+   (void) miicv_setint(icv, MI_ICV_ZDIM_DIR, MI_ICV_POSITIVE);
+   (void) miicv_setint(icv, MI_ICV_ADIM_SIZE, xsize);
+   (void) miicv_setint(icv, MI_ICV_BDIM_SIZE, ysize);
+   (void) miicv_setint(icv, MI_ICV_KEEP_ASPECT, FALSE);
+   (void) miicv_setint(icv, MI_ICV_DO_DIM_CONV, TRUE);
+   (void) miicv_setint(icv, MI_ICV_TYPE, datatype);
+   (void) miicv_setstr(icv, MI_ICV_SIGN, signtype);
+   (void) miicv_setdbl(icv, MI_ICV_VALID_MAX, image_max);
+   (void) miicv_setdbl(icv, MI_ICV_VALID_MIN, image_min);
+
+   /* Open the file, attach the image variable */
+   cdfid=miopen(filename, NC_NOWRITE);
+
+   /* Attach image variable */
+   img=ncvarid(cdfid, MIimage);
+   (void) miicv_attach(icv, cdfid, img);
+
+   /* Get the number of dimensions and modify count and coord */
+   (void) ncvarinq(cdfid, img, NULL, NULL, &ndims, dim, NULL);
+   if (ndims!=3) {
+      (void) fprintf(stderr, "File must have 3 dimensions\n");
+      return ERROR_STATUS;
+   }
+   (void) ncdiminq(cdfid, dim[0], NULL, &dim_size);
+   count[0]=1;
+   count[1]=ysize;
+   count[2]=xsize;
+   coord[1]=0;
+   coord[2]=0;
+
+   /* Get the data */
+   for (i=0; i<dim_size; i++) {
+      coord[0]=i;
+      (void) miicv_get(icv, coord, count, image);
+
+      switch (datatype) {
+      case NC_BYTE:
+          if (!strcmp(signtype, MI_UNSIGNED)) {
+              for (n = 0; n < xsize*ysize; n++) {
+                  unsigned char uc = *(((unsigned char *)image) + n);
+                  if (uc > max) {
+                      max = uc;
+                  }
+                  if (uc < min) {
+                      min = uc;
+                  }
+              }
+          }
+          else {
+              for (n = 0; n < xsize*ysize; n++) {
+                  signed char sc = *(((signed char *)image) + n);
+                  if (sc > max) {
+                      max = sc;
+                  }
+                  if (sc < min) {
+                      min = sc;
+                  }
+              }
+          }
+          break;
+      case NC_SHORT:
+          if (!strcmp(signtype, MI_UNSIGNED)) {
+              for (n = 0; n < xsize*ysize; n++) {
+                  unsigned short uc = *(((unsigned short *)image) + n);
+                  if (uc > max) {
+                      max = uc;
+                  }
+                  if (uc < min) {
+                      min = uc;
+                  }
+              }
+          }
+          else {
+              for (n = 0; n < xsize*ysize; n++) {
+                  signed short sc = *(((signed short *)image) + n);
+                  if (sc > max) {
+                      max = sc;
+                  }
+                  if (sc < min) {
+                      min = sc;
+                  }
+              }
+          }
+          break;
+      case NC_INT:
+          if (!strcmp(signtype, MI_UNSIGNED)) {
+              for (n = 0; n < xsize*ysize; n++) {
+                  unsigned int uc = *(((unsigned int *)image) + n);
+                  if (uc > max) {
+                      max = uc;
+                  }
+                  if (uc < min) {
+                      min = uc;
+                  }
+              }
+          }
+          else {
+              for (n = 0; n < xsize*ysize; n++) {
+                  signed int sc = *(((signed int *)image) + n);
+                  if (sc > max) {
+                      max = sc;
+                  }
+                  if (sc < min) {
+                      min = sc;
+                  }
+              }
+          }
+          break;
+      case NC_FLOAT:
+          for (n = 0; n < xsize*ysize; n++) {
+              float sc = *(((float *)image) + n);
+              if (sc > max) {
+                  max = sc;
+              }
+              if (sc < min) {
+                  min = sc;
+              }
+          }
+          break;
+      case NC_DOUBLE:
+          for (n = 0; n < xsize*ysize; n++) {
+              double sc = *(((double *)image) + n);
+              if (sc > max) {
+                  max = sc;
+              }
+              if (sc < min) {
+                  min = sc;
+              }
+          }
+          break;
+      }
+      printf("%d %s %s %f %f\n", i, signtype, nctypename(datatype), min, max);
+   }
+
+   /* Close the file and free the icv */
+   (void) miclose(cdfid);
+   (void) miicv_free(icv);
+
+   free(image);
+   return (NORMAL_STATUS);
+}
+
+main(int argc, char *argv[])
+{
+   int xsize, ysize;
+   double image_max, image_min;
    char *pname, *filename;
 
    /* Parse the command line */
@@ -34,49 +214,24 @@
    image_min=atof(argv[4]);
    image_max=atof(argv[5]);
 
-   /* Create the icv */
-   icv=miicv_create();
-   (void) miicv_setint(icv, MI_ICV_XDIM_DIR, MI_ICV_POSITIVE);
-   (void) miicv_setint(icv, MI_ICV_YDIM_DIR, MI_ICV_POSITIVE);
-   (void) miicv_setint(icv, MI_ICV_ZDIM_DIR, MI_ICV_POSITIVE);
-   (void) miicv_setint(icv, MI_ICV_ADIM_SIZE, xsize);
-   (void) miicv_setint(icv, MI_ICV_BDIM_SIZE, ysize);
-   (void) miicv_setint(icv, MI_ICV_KEEP_ASPECT, FALSE);
-   (void) miicv_setint(icv, MI_ICV_DO_DIM_CONV, TRUE);
-   (void) miicv_setint(icv, MI_ICV_TYPE, NC_BYTE);
-   (void) miicv_setstr(icv, MI_ICV_SIGN, MI_UNSIGNED);
-   (void) miicv_setdbl(icv, MI_ICV_VALID_MAX, image_max);
-   (void) miicv_setdbl(icv, MI_ICV_VALID_MIN, image_min);
-
-   /* Open the file, attach the image variable */
-   cdfid=ncopen(filename, NC_NOWRITE);
-
-   /* Attach image variable */
-   img=ncvarid(cdfid, MIimage);
-   (void) miicv_attach(icv, cdfid, img);
+   test_icv_read(filename, xsize, ysize, image_min, image_max, 
+                 NC_BYTE, MI_UNSIGNED);
+   test_icv_read(filename, xsize, ysize, image_min, image_max, 
+                 NC_SHORT, MI_UNSIGNED);
+   test_icv_read(filename, xsize, ysize, image_min, image_max, 
+                 NC_INT, MI_UNSIGNED);
 
-   /* Get the number of dimensions and modify count and coord */
-   (void) ncvarinq(cdfid, img, NULL, NULL, &ndims, dim, NULL);
-   if (ndims!=3) {
-      (void) fprintf(stderr, "%s: File must have 3 dimensions\n", pname);
-      return ERROR_STATUS;
-   }
-   (void) ncdiminq(cdfid, dim[0], NULL, &dim_size);
-   count[0]=1;
-   count[1]=ysize;
-   count[2]=xsize;
-   coord[1]=0;
-   coord[2]=0;
+   test_icv_read(filename, xsize, ysize, image_min, image_max, 
+                 NC_BYTE, MI_SIGNED);
+   test_icv_read(filename, xsize, ysize, image_min, image_max, 
+                 NC_SHORT, MI_SIGNED);
+   test_icv_read(filename, xsize, ysize, image_min, image_max, 
+                 NC_INT, MI_SIGNED);
 
-   /* Get the data */
-   for (i=0; i<dim_size; i++) {
-      coord[0]=i;
-      (void) miicv_get(icv, coord, count, image);
-   }
-
-   /* Close the file and free the icv */
-   (void) ncclose(cdfid);
-   (void) miicv_free(icv);
+   test_icv_read(filename, xsize, ysize, image_min, image_max, 
+                 NC_FLOAT, MI_SIGNED);
+   test_icv_read(filename, xsize, ysize, image_min, image_max, 
+                 NC_DOUBLE, MI_SIGNED);
 
    return NORMAL_STATUS;
 }