/*------------------------------------------------------------------------- * Function: test_read_slice * * Purpose: Reads just certain parts of an array. We only test this for * the 3d_int case. * * Return: Success: 0 * * Failure: number of errors * * Programmer: Robb Matzke * Wednesday, February 10, 1999 * * Modifications: * Robb Matzke, 2000-01-12 * Changed hyphens to underscores in object names because silo * now fails when underscores are present in the name. *------------------------------------------------------------------------- */ static int test_read_slice(DBfile *dbfile) { int dims[3], i, j, n, nerrors=0; int idata_in[12*30]; int offset[3], length[3], stride[3]; puts("DBReadSlice()"); puts(" 3d_int"); dims[0] = 12; dims[1] = 30; dims[2] = TEST_NELMTS/(dims[0]*dims[1]); /* Read elements (I,J,0) for I in {5,7,9}, J in {9,18,27} */ offset[0] = 5; offset[1] = 9; offset[2] = 0; length[0] = (9-5)+1; /*one method*/ length[1] = 9*3; /*another method*/ length[2] = 1; stride[0] = 2; stride[1] = 9; stride[2] = 1; /*don't care -- only one value being read */ if (DBReadVarSlice(dbfile, "3d_int", offset, length, stride, 3, idata_in)<0) { puts(" failed"); nerrors++; } /* Check */ for (i=n=0; i<3; i++) { for (j=0; j<3; j++, n++) { int v = idata_g[(offset[0]+stride[0]*i)*dims[1]*dims[2] + (offset[1]+stride[1]*j)*dims[2]]; if (v!=idata_in[n]) { printf(" failed at i=%d: out=%d, in=%d\n", n, v, idata_in[n]); nerrors++; } } } return nerrors; }
/*------------------------------------------------------------------------- * Function: main * * Purpose: * * Return: 0 * * Programmer: * * Modifications: * Robb Matzke, 1999-04-09 * Added argument parsing to control the driver which is used. * * Mark C. Miller, Mon Sep 21 15:20:30 PDT 2009 * Added code to test long long type. * * Mark C. Miller, Wed Sep 23 11:57:24 PDT 2009 * Added logic to test DBInqFile. * * Mark C. Miller, Fri Nov 13 15:40:35 PST 2009 * Test long long on PDB driver too. *------------------------------------------------------------------------- */ int main(int argc, char *argv[]) { int i, j, k; int ndims, dims[3]; float val[NX * NY * NZ]; long long lval[NX * NY * NZ]; int offset[3], length[3], stride[3]; float val2[NX * NY * NZ]; long long *lval2 = 0; int cnt, driver=DB_PDB; char *filename="simple.pdb"; int k1, k2; int err = 0; int inqval; DBfile *dbfile; int show_all_errors = FALSE; /* Parse command-line */ for (i=1; i<argc; i++) { if (!strncmp(argv[i], "DB_PDB", 6)) { driver = StringToDriver(argv[i]); filename = "simple.pdb"; } else if (!strncmp(argv[i], "DB_HDF5", 7)) { driver = StringToDriver(argv[i]); filename = "simple.h5"; } else if (!strcmp(argv[i], "show-all-errors")) { show_all_errors = 1; } else if (argv[i][0] != '\0') { fprintf(stderr, "%s: ignored argument `%s'\n", argv[0], argv[i]); } } DBShowErrors(show_all_errors?DB_ALL_AND_DRVR:DB_TOP, NULL); DBForceSingle(1); /* * Build a simple variables. */ ndims = 3; dims[0] = NZ; dims[1] = NY; dims[2] = NX; for (k = 0; k < NZ; k++) { for (j = 0; j < NY; j++) { for (i = 0; i < NX; i++) { val[i + j * NX + k * NX * NY] = i + j * NX + k * NX * NY; lval[i + j * NX + k * NX * NY] = ((long long) 1 << 35) + i + j * NX + k * NX * NY; } } } /* Test InqFile on a PDB (but not Silo) file */ if (driver == DB_PDB) inqval = DBInqFile("not_a_silo_file.pdb"); else inqval = DBInqFile("not_a_silo_file.h5"); if (inqval < 0) { fprintf(stderr, "Error in InqFile attempting to identify not_a_silo_file"); err = 1; } else if (inqval > 0) { fprintf(stderr, "InqFile incorrectly identified not_a_silo_file"); err = 1; } /* Create empty silo file to test InqFile */ dbfile = DBCreate(filename, 0, DB_LOCAL, "Empty Silo File", driver); DBClose(dbfile); if (DBInqFile(filename) <= 0) { fprintf(stderr, "InqFile says file created via DBCreate is NOT a silo file"); err = 1; } unlink(filename); /* * Create a file that contains a simple variables. */ printf("Creating file: `%s'\n", filename); dbfile = DBCreate(filename, 0, DB_LOCAL, "Simple Test", driver); DBWrite(dbfile, "simple", val, dims, ndims, DB_FLOAT); DBWrite(dbfile, "longlong", lval, dims, ndims, DB_LONG_LONG); DBClose(dbfile); /* * Now try opening the file again and reading the simple * variable. */ printf("Reopening `%s'\n", filename); dbfile = DBOpen(filename, driver, DB_READ); offset[0] = 0; offset[1] = 0; offset[2] = 0; length[0] = NZ2; length[1] = NY2; length[2] = NX2; stride[0] = 1; stride[1] = 1; stride[2] = 1; for (i = 0; i < NX * NY * NZ; i++) val2[i] = 0; DBReadVarSlice(dbfile, "simple", offset, length, stride, ndims, val2); lval2 = DBGetVar(dbfile, "longlong"); DBClose(dbfile); /* * Check the data. */ cnt = 0; for (k = 0; k < NZ2; k++) { for (j = 0; j < NY2; j++) { for (i = 0; i < NX2; i++) { if (val2[i + j * NX2 + k * NX2 * NY2] != val[i + j * NX + k * NX * NY]) cnt++; } } } err += cnt; printf("%d values don't match\n", cnt); cnt = 0; k1 = NX2 * NY2 * NZ2; k2 = NX * NY * NZ; for (i = k1; i < k2; i++) if (val2[i] != 0) cnt++; printf("%d values were overwritten\n", cnt); cnt = 0; for (k = 0; k < NZ && lval2; k++) { for (j = 0; j < NY; j++) { for (i = 0; i < NX; i++) { if (lval2[i + j * NX + k * NX * NY] != lval[i + j * NX + k * NX * NY]) cnt++; } } } err += cnt; printf("%d long long values don't match\n", cnt); if (lval2) free(lval2); CleanupDriverStuff(); return err; }