Exemplo n.º 1
0
        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