Skip to content

Commit

Permalink
Add initial test for alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
hmaarrfk committed Aug 28, 2022
1 parent 2c2acc2 commit e61d763
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions test/tst_alignment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from netCDF4 import set_alignment, get_alignment, Dataset
import netCDF4
import tempfile, unittest, os
import numpy as np

libversion= tuple(int(v) for v in netCDF4.__netcdf4libversion__.split('.'))
has_alignment = (libversion[0] > 4) or (
libversion[0] == 4 and (libversion[1] >= 9)
)

file_name = tempfile.NamedTemporaryFile(suffix='.nc', delete=False).name

def is_aligned(dataset, offset=4096):
# Here we check if the dataset is aligned
return dataset.id.get_offset() % offset == 0

class AlignmentTestCase(unittest.TestCase):
def setUp(self):
self.file = file_name

# This is a global variable in netcdf4, it must be set before File
# creation
if has_alignment:
set_alignment(1024, 4096)
assert get_alignment() == (1024, 4096)

f = Dataset(self.file, 'w')
f.createDimension('x', 4096)
# Create many datasets so that we decrease the chance of
# the dataset being randomly aligned
for i in range(10):
f.createVariable(f'data{i:02d}', np.float64, ('x',))
v = f.variables[f'data{i:02d}']
v[...] = 0
f.close()

def test_version_settings(self):
if has_alignment:
# One should always be able to set the alignment to 0, 0
set_alignment(0, 0)
assert get_alignment() == (0, 0)
else:
with self.assertRaises(RunetimeError):
set_alignment(0, 0)
with self.assertRaises(RunetimeError):
get_alignment(0, 0)


def test_setting_alignment(self):
# if we have no support for alignment, we have no guarantees on
# how the data can be aligned
if not has_alignment:
return

# TODO: ensure that the underlying alignment is set. but I'm not sure
# how to do this with netcdf
# https://github.com/h5py/h5py/pull/2040/files?diff=unified&w=0#diff-3166eca28ff7f5d816f07f37eaba428b4351077384d65a2630e7d85c1284698fR7
import h5py
with h5py.File(self.file, 'r') as h5file:
for i in range(10):
v = h5file[f'data{i:02d}']
assert is_aligned(v, offset=4096)


def tearDown(self):
# Remove the temporary files
os.remove(self.file)

if __name__ == '__main__':
unittest.main()

0 comments on commit e61d763

Please sign in to comment.