diff --git a/pfio/FileMetadata.F90 b/pfio/FileMetadata.F90 index 73e33927885d..2d3cae3c5590 100644 --- a/pfio/FileMetadata.F90 +++ b/pfio/FileMetadata.F90 @@ -123,11 +123,20 @@ subroutine add_dimension(this, dim_name, extent, unusable, rc) class (FileMetadata), target, intent(inout) :: this character(len=*), intent(in) :: dim_name integer, intent(in) :: extent - class (KeywordEnforcer), optional, intent(in) :: unusable integer, optional, intent(out) :: rc - call this%dimensions%insert(dim_name, extent) + integer :: existing_extent + + if (.not. this%has_dimension(dim_name)) then + call this%dimensions%insert(dim_name, extent) + _RETURN(_SUCCESS) + end if + + ! Otherwise verify consistency + existing_extent = this%get_dimension(dim_name) + _ASSERT(extent == existing_extent,'FileMetadata::add_dimension() - dimension already exists with different extent.') + _RETURN(_SUCCESS) _UNUSED_DUMMY(unusable) end subroutine add_dimension diff --git a/pfio/tests/Test_FileMetadata.pf b/pfio/tests/Test_FileMetadata.pf index 4b324c8d8850..284f4683bcb5 100644 --- a/pfio/tests/Test_FileMetadata.pf +++ b/pfio/tests/Test_FileMetadata.pf @@ -49,6 +49,26 @@ contains end subroutine test_get_dimension + @test + subroutine test_fail_add_existing_dim_with_mismatch() + type (FileMetadata) :: cf + + call cf%add_dimension('x', 10) + call cf%add_dimension('x', 11) + + @assertExceptionRaised('FileMetadata::add_dimension() - dimension already exists with different extent.') + + end subroutine test_fail_add_existing_dim_with_mismatch + + @test + subroutine test_add_duplicate_dimension() + type (FileMetadata) :: cf + + call cf%add_dimension('x', 10) + call cf%add_dimension('x', 10) + + end subroutine test_add_duplicate_dimension + @test subroutine test_get_dimensions() type (FileMetadata), target :: cf