'Tests that the mmap reference is closed/removed when there aren\'t any
HDU data references left.'

def test_mmap_closing(self):

    if (not _File._mmap_available):
        pytest.xfail('not    expected    to    work    on    platforms    without    mmap    support')
    with fits.open(self.data('test0.fits'), memmap=True) as hdul:
        assert (hdul._file._mmap is None)
        hdul[1].data
        assert (hdul._file._mmap is not None)
        del hdul[1].data
        assert (hdul._file._mmap is None)
        hdul[1].data
        hdul[2].data
        del hdul[1].data
        assert (hdul._file._mmap is not None)
        del hdul[2].data
        assert (hdul._file._mmap is None)
    assert (hdul._file._mmap is None)
    with fits.open(self.data('test0.fits'), memmap=True) as hdul:
        hdul[1].data
    assert (hdul._file._mmap is None)
    with fits.open(self.data('test0.fits'), memmap=True) as hdul:
        data = hdul[1].data
        data_copy = data.copy()
    del hdul
    assert np.all((data == data_copy))
