} // make_table static void test_getting_info() { try { Console.Write("Testing getting table/field information"); // Open the file to check on the table. H5FileId fileId = H5F.open(FILE_NAME, H5F.OpenMode.ACC_RDWR); hssize_t nfields = 0, nrecords = 0; string[] field_names = { "c", "i", "l" }; // Get the table info. TableInfo table = H5TB.getTableInfo(fileId, TABLE_NAME); if (table.nFields != N_FIELDS) { Console.WriteLine("\ntest_getting_info: incorrect number of fields: read {0} - should be {1}", nfields, N_FIELDS); nerrors++; } if (table.nRecords != N_RECORDS) { Console.WriteLine("\ntest_getting_info: incorrect number of fields: read {0} - should be {1}", nrecords, N_RECORDS); nerrors++; } // Get field info. int [] sizes = new int[N_FIELDS]; int [] offsets = new int[N_FIELDS]; TableFieldInfo tablefield = H5TB.getFieldInfo(fileId, TABLE_NAME); int ii; for (ii = 0; ii < N_FIELDS; ii++) { if (tablefield.fieldName[ii] != field_names[ii]) { Console.WriteLine("\ntest_getting_info: field #{0} has incorrect name: read {0} - should be {1}", ii, field_names[ii], tablefield.fieldName[ii]); nerrors++; } } H5F.close(fileId); Console.WriteLine("\t\t\tPASSED"); } catch (HDFException anyHDF5E) { Console.WriteLine(anyHDF5E.Message); nerrors++; } catch (System.Exception sysE) { Console.WriteLine(sysE.TargetSite); Console.WriteLine(sysE.Message); nerrors++; } } // test_getting_info
static void test_group_basics() { try { Console.Write("Testing group basics"); // Create the file. H5FileId fileId = H5F.create(FILE_NAME, H5F.CreateMode.ACC_TRUNC); // Create a group. H5GroupId groupId = H5G.create(fileId, "/fromRoot"); // Create a dataspace for common use. hssize_t[] dims = { 1000, 20 }; H5DataSpaceId dspace = H5S.create_simple(RANK, dims); // Create a dataset using file as location with absolute path name. H5DataSetId dset1Id = H5D.create(fileId, "/fromRoot/intArray", H5T.H5Type.NATIVE_INT, dspace); // Create a dataset using group as location with absolute path name. H5DataSetId dset2Id = H5D.create(groupId, "/fromRoot/shortArray", H5T.H5Type.NATIVE_SHORT, dspace); // Create a dataset using group as location with relative path name. H5DataSetId dset3Id = H5D.create(groupId, "notfromRoot", H5T.H5Type.NATIVE_UCHAR, dspace); ObjectInfo info = H5G.getObjectInfo(fileId, "/fromRoot/intArray", true); if (info.nHardLinks != 1) { Console.WriteLine("\ntest_group_basics: number of hardlinks for /fromRoot/intArray should be = {0}", info.nHardLinks); } if (info.objectType != H5GType.DATASET) { Console.WriteLine("\ntest_group_basics: Object should be a dataset"); } // Close objects and files. H5D.close(dset1Id); H5D.close(dset2Id); H5D.close(dset3Id); H5S.close(dspace); H5G.close(groupId); // Check various number of objects. H5GroupId rootId = H5G.open(fileId, "/"); hssize_t num_objs = H5G.getNumObjects(rootId); if (num_objs != 1) { Console.WriteLine("\ntest_group_basics: incorrect num_objs = {0} for root group\n", num_objs); nerrors++; } groupId = H5G.open(fileId, "fromRoot"); num_objs = H5G.getNumObjects(groupId); if (num_objs != 3) { Console.WriteLine("\ntest_group_basics: incorrect num_objs = {0} for group \"fromRoot\"\n", num_objs); nerrors++; } H5G.close(rootId); H5G.close(groupId); H5F.close(fileId); if (nerrors == 0) { Console.WriteLine("\t\t\t\t\tPASSED"); } } catch (HDFException anyHDF5E) { Console.WriteLine(anyHDF5E.Message); nerrors++; } catch (System.Exception sysE) { Console.WriteLine(sysE.TargetSite); Console.WriteLine(sysE.Message); nerrors++; } } // test_group_basics
/* * test_group_iterate -- tests that group iterating works properly. * - opens the file created in the test_group_basics * - creates more groups and datasets * - iterates through root group and each sub group priting out name of each object */ static void test_group_iterate() { try { Console.Write("Testing group iteration"); // Open the file. H5FileId fileId = H5F.open(FILE_NAME, H5F.OpenMode.ACC_RDWR); // Create a group in the file. H5GroupId groupId = H5G.create(fileId, "/Data"); // Open first dataset. H5DataSetId dset1Id = H5D.open(fileId, "/fromRoot/intArray"); // Get dataspace of this dataset. H5DataSpaceId dspace = H5D.getSpace(dset1Id); // Create a dataset in the group using absolute name. H5DataSetId dataset = H5D.create(groupId, "/Data/IntData", H5T.H5Type.NATIVE_INT, dspace); // Close the first dataset. H5S.close(dspace); H5D.close(dataset); // Create the second dataset. hssize_t[] dims = { 500, 20 }; dspace = H5S.create_simple(RANK, dims); dataset = H5D.create(groupId, "/Data/FloatData", H5T.H5Type.NATIVE_FLOAT, dspace); // Close objects and file. H5D.close(dataset); H5G.close(groupId); H5F.close(fileId); // Now reopen the file and group in the file. fileId = H5F.open(FILE_NAME, H5F.OpenMode.ACC_RDWR); groupId = H5G.open(fileId, "/Data"); // Access "IntData" dataset in the group. dataset = H5D.open(groupId, "IntData"); // Create a dataset in the root group. dataset = H5D.create(fileId, "/singleDataset", H5T.H5Type.NATIVE_INT, dspace); // Various checks on number of objects groupId = H5G.open(fileId, "/"); hssize_t num_objs = H5G.getNumObjects(groupId); if (num_objs != 3) { Console.WriteLine("\ntest_group_iterate: / should have 3 objects: /fromRoot, /Data, and /singleDataset, but is {0}", num_objs); } H5G.close(groupId); groupId = H5G.open(fileId, "/fromRoot"); num_objs = H5G.getNumObjects(groupId); if (num_objs != 3) { Console.WriteLine("\ntest_group_iterate: /fromRoot should have 3 objects: intArray, shortArray, notfromRoot, but is {0}", num_objs); } // Use iterator to see the names of the objects in the root group and sub groups. H5GIterateCallback myCallback; myCallback = file_info; int x = 0; Console.WriteLine(); Console.WriteLine("\tRoot Group iterating:"); int index = H5G.iterate(fileId, "/", myCallback, "Object name:", ref x); Console.WriteLine(); Console.WriteLine("\t/fromRoot iterating:"); x = 0; index = H5G.iterate(fileId, "/fromRoot", myCallback, "Object name:", ref x); // Close objects and file. H5D.close(dataset); H5G.close(groupId); H5S.close(dspace); H5F.close(fileId); Console.WriteLine("\t\t\t\t\t\t\tPASSED"); } catch (HDFException anyHDF5E) { Console.WriteLine(anyHDF5E.Message); nerrors++; } catch (System.Exception sysE) { Console.WriteLine(sysE.TargetSite); Console.WriteLine(sysE.Message); nerrors++; } } // test_group_iterate
static void test_h5s_basic() { try { int rank; // Logical rank of dataspace hssize_t[] dims1 = { SPACE1_DIM1, SPACE1_DIM2, SPACE1_DIM3 }; hssize_t[] dims2 = { SPACE2_DIM1, SPACE2_DIM2, SPACE2_DIM3, SPACE2_DIM4 }; hssize_t[] max2 = { SPACE2_MAX1, SPACE2_MAX2, SPACE2_MAX3, SPACE2_MAX4 }; hssize_t[] tmax = new hssize_t[4]; // Output message about test being performed. Console.Write("Testing dataspace manipulation"); // Create a simple dataspace and check its rank. H5DataSpaceId sid1 = H5S.create_simple(SPACE1_RANK, dims1); rank = H5S.getSimpleExtentNDims(sid1); if (rank != SPACE1_RANK) { Console.WriteLine("\ntest_h5s_basic: Incorrect rank {0}, should be SPACE1_RANK({1})", rank, SPACE1_RANK); nerrors++; } // Check its dims. hssize_t[] tdims1 = new hssize_t[3]; tdims1 = H5S.getSimpleExtentDims(sid1); int i; for (i = 0; i < rank; i++) { if (tdims1[i] != dims1[i]) { Console.WriteLine("\ntest_h5s_basic: read tdims1[{0}] = {1} differs from dims1[{0}] = {2}", i, tdims1[i], dims1[i]); nerrors++; } } // Create another simple dataspace and check its rank, dims, and maxdims. H5DataSpaceId sid2 = H5S.create_simple(SPACE2_RANK, dims2, max2); rank = H5S.getSimpleExtentNDims(sid2); if (rank != SPACE2_RANK) { Console.WriteLine("\ntest_h5s_basic: Incorrect rank {0}, should be SPACE1_RANK({1})", rank, SPACE1_RANK); nerrors++; } hssize_t[] tdims2 = new hssize_t[3]; tdims2 = H5S.getSimpleExtentDims(sid2); tmax = H5S.getSimpleExtentMaxDims(sid2); for (i = 0; i < rank; i++) { if (tdims2[i] != dims2[i]) { Console.WriteLine("\ntest_h5s_basic: read tdims2[{0}] = {1} differs from dims2[{0}] = {2}", i, tdims2[i], dims2[i]); nerrors++; } } for (i = 0; i < rank; i++) { if (tmax[i] != max2[i]) { Console.WriteLine("\ntest_h5s_basic: read tmax[{0}] = {1} differs from max2[{0}] = {2}", i, tmax[i], max2[i]); nerrors++; } } // Close all dataspaces. H5S.close(sid1); H5S.close(sid2); /* * Try writing simple dataspaces without setting their extents. */ // Create the file H5FileId fid1 = H5F.create(BASICFILE, H5F.CreateMode.ACC_TRUNC); // Create dataspaces for testing. dims1[0] = SPACE1_DIM1; sid1 = H5S.create(H5S.H5SClass.SIMPLE); sid2 = H5S.create_simple(1, dims1, dims1); // This dataset's space has no extent; it should not be created try { H5DataSetId dset1 = H5D.create(fid1, BASICDATASET, H5T.H5Type.NATIVE_INT, sid1); // should fail, but didn't, print an error message. Console.WriteLine("\ntest_h5s_basic: Attempting to create a dataset whose space has no extent."); nerrors++; } catch (H5DcreateException) { } // does nothing, it should fail // Create dataset with good dataspace. H5DataSetId dataset = H5D.create(fid1, BASICDATASET2, H5T.H5Type.NATIVE_INT, sid2); // Try some writes with the bad dataspace (sid1) try { hssize_t nelems = 10; // Number of dataspace elements H5D.writeScalar(dataset, new H5DataTypeId(H5T.H5Type.NATIVE_INT), sid1, sid2, new H5PropertyListId(H5P.Template.DEFAULT), ref nelems); // should fail, but didn't, print an error message. Console.WriteLine("\ntest_h5s_basic: Attempting to write to a dataset with space that has no extent."); nerrors++; } catch (H5DwriteException) { } // does nothing, it should fail // Make sure that dataspace reads using the bad dataspace fail try { hssize_t n = 10; // Number of dataspace elements H5D.readScalar(dataset, new H5DataTypeId(H5T.H5Type.NATIVE_INT), sid1, sid2, new H5PropertyListId(H5P.Template.DEFAULT), ref n); // should fail, but didn't, print an error message. Console.WriteLine("\ntest_h5s_basic: Attempting to read a dataset with space that has no extent."); nerrors++; } catch (H5DreadException) { } // does nothing, it should fail // Close objects and file. H5D.close(dataset); H5S.close(sid1); H5S.close(sid2); H5F.close(fid1); Console.WriteLine("\t\t\t\tPASSED"); } // end of try catch (HDFException anyHDF5E) { Console.WriteLine(anyHDF5E.Message); nerrors++; } catch (System.Exception sysE) { Console.WriteLine(sysE.TargetSite); Console.WriteLine(sysE.Message); nerrors++; } } // test_h5s_basic
static void test_h5s_scalar() { try { hssize_t[] tdims = new hssize_t[3]; // Output message about test being performed. Console.Write("Testing dataspace during writing"); // Create the file. H5FileId fid1 = H5F.create(DATAFILE, H5F.CreateMode.ACC_TRUNC); // Create scalar dataspace. H5DataSpaceId sid1 = H5S.create_simple(SPACE3_RANK, null); // Get the logical rank of dataspace and verify it. int rank = H5S.getSimpleExtentNDims(sid1); if (rank != SPACE3_RANK) { Console.WriteLine("\ntest_h5s_scalar: incorrect rank {0}, should be SPACE3_RANK({1})", rank, SPACE3_RANK); nerrors++; } // Create and write the dataset. uint space3_data = 65; H5DataSetId dataset = H5D.create(fid1, "Dataset1", H5T.H5Type.NATIVE_UINT, sid1); H5D.writeScalar(dataset, new H5DataTypeId(H5T.H5Type.NATIVE_UINT), ref space3_data); // Close objects and file. H5D.close(dataset); H5S.close(sid1); H5F.close(fid1); /* Open the file and verify the dataspace. */ // Open the file. fid1 = H5F.open(DATAFILE, H5F.OpenMode.ACC_RDWR); // Create a dataset. dataset = H5D.open(fid1, "Dataset1"); // Get dataset's dataspace. sid1 = H5D.getSpace(dataset); rank = H5S.getSimpleExtentNDims(sid1); if (rank != SPACE3_RANK) { Console.WriteLine("\ntest_h5s_scalar: incorrect rank {0}", rank); } tdims = H5S.getSimpleExtentDims(sid1); //Console.WriteLine("tdims[0] = {0}, tdims[1] = {1}", tdims[0], tdims[1]); if (rank != 0) { Console.WriteLine("\ntest_h5s_scalar: incorrect rank {0}", rank); } // Read the dataset. uint rdata = 0; H5D.readScalar(dataset, new H5DataTypeId(H5T.H5Type.NATIVE_UINT), ref rdata); if (rdata != space3_data) { Console.WriteLine("\ntest_h5s_scalar: incorrect data {0}, should be {1}", rdata, space3_data); } // Close objects. H5D.close(dataset); H5S.close(sid1); H5F.close(fid1); Console.WriteLine("\t\t\tPASSED"); } // end of try catch (HDFException anyHDF5E) { Console.WriteLine(anyHDF5E.Message); nerrors++; } catch (System.Exception sysE) { Console.WriteLine(sysE.TargetSite); Console.WriteLine(sysE.Message); nerrors++; } } // test_h5s_scalar_write
static void test_attr_iterate() { try { Console.Write("Testing attribute iteration"); // Open the file. H5FileId fileId = H5F.open(FILE_NAME, H5F.OpenMode.ACC_RDWR); // Create a dataspace. H5DataSpaceId spaceId = H5S.create(H5S.H5SClass.SCALAR); // Create a new dataset using default properties. const string DSET2_NAME = ("Dataset2"); H5DataSetId dsetId = H5D.create(fileId, DSET2_NAME, H5T.H5Type.NATIVE_INT, spaceId); // Close dataspace. H5S.close(spaceId); // Verify the correct number of attributes. H5ObjectInfo oinfo = H5O.getInfo(dsetId); if (oinfo.nAttributes != 0) { Console.WriteLine("\ntest_attr_iterate: incorrect number of attributes: read {0} - should be {1}", oinfo.nAttributes, 1); nerrors++; } // Iterate through the attributes. hssize_t position = 0; H5AIterateCallback attributeCallback; attributeCallback = MyH5AFunction; H5ObjectInfo groupInfo = H5O.getInfo(dsetId); // While iterating, collect the names of all the attributes. ArrayList attributeNames = new ArrayList(); Console.WriteLine(); Console.WriteLine("\tIterating through attributes:"); H5A.iterate(dsetId, H5IndexType.CRT_ORDER, H5IterationOrder.INCREASING, ref position, attributeCallback, (object)attributeNames); // Close dataset. H5D.close(dsetId); // Open existing dataset with attributes. dsetId = H5D.open(fileId, DSET1_NAME); // Iterate through the attributes of dataset DSET1_NAME. position = 0; attributeCallback = MyH5AFunction; groupInfo = H5O.getInfo(dsetId); // While iterating, collect the names of all the attributes. attributeNames = new ArrayList(); H5A.iterate(dsetId, H5IndexType.CRT_ORDER, H5IterationOrder.INCREASING, ref position, attributeCallback, (object)attributeNames); // Verify the correct number of attributes. oinfo = H5O.getInfo(dsetId); if (oinfo.nAttributes != 2) { Console.WriteLine("\ntest_attr_iterate: incorrect number of attributes: read {0} - should be {1}", oinfo.nAttributes, 2); nerrors++; } // Close dataset and file. H5D.close(dsetId); H5F.close(fileId); Console.WriteLine("\tPASSED"); } catch (HDFException anyHDF5E) { Console.WriteLine(anyHDF5E.Message); nerrors++; } catch (System.Exception sysE) { Console.WriteLine(sysE.TargetSite); Console.WriteLine(sysE.Message); nerrors++; } }