Skip to content

Commit

Permalink
add longer tests, identify bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Moodie committed Jun 9, 2020
1 parent 5e94e39 commit d4ede5e
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 45 deletions.
57 changes: 20 additions & 37 deletions tests/test_consistent.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,58 +29,41 @@ def test_bed_after_ten_updates(test_DeltaModel):
test_DeltaModel.update()

# slice is: test_DeltaModel.eta[:5, 4]
print(test_DeltaModel.eta[:5, 4])
# print(test_DeltaModel.eta[:5, 4])

# _exp = np.array([-1., -0.840265, -0.9976036, -1., -1.])
_exp = np.array([1.7, 0.83358884, -0.9256229, -1., -1.])
assert np.all(test_DeltaModel.eta[:5, 4] == pytest.approx(_exp))


@pytest.mark.xfail()
def test_segfault_error(tmp_path):
def test_long_multi_validation(tmp_path):
# IndexError on corner.

file_name = 'user_parameters.yaml'
p, f = utilities.create_temporary_file(tmp_path, file_name)
utilities.write_parameter_to_file(f, 'seed', 43)
utilities.write_parameter_to_file(f, 'Length', 30.)
utilities.write_parameter_to_file(f, 'Width', 60.)
utilities.write_parameter_to_file(f, 'dx', 1)
utilities.write_parameter_to_file(f, 'verbose', 1)
utilities.write_parameter_to_file(f, 'Np_water', 20)
utilities.write_parameter_to_file(f, 'Np_sed', 20)
utilities.write_parameter_to_file(f, 'f_bedload', 0.65)
utilities.write_parameter_to_file(f, 'seed', 42)
utilities.write_parameter_to_file(f, 'Length', 600.)
utilities.write_parameter_to_file(f, 'Width', 600.)
utilities.write_parameter_to_file(f, 'dx', 5)
utilities.write_parameter_to_file(f, 'Np_water', 10)
utilities.write_parameter_to_file(f, 'Np_sed', 10)
utilities.write_parameter_to_file(f, 'f_bedload', 0.05)
f.close()
delta = DeltaModel(input_file=p)

for _ in range(0, 2):
for _ in range(0, 3):
delta.update()

# slice is: test_DeltaModel.eta[:5, 4]
print(delta.eta[:5, 2])

_exp = np.array([1.7, 0.83358884, -0.9256229, -1., -1.])
assert np.all(delta.eta[:5, 2] == pytest.approx(_exp))
# slice is: test_DeltaModel.eta[:5, 62]
# print(delta.eta[:5, 62])

_exp1 = np.array([-4.971009, -3.722004, -4.973, -3.7240038, -3.7250037])
assert np.all(delta.eta[:5, 62] == pytest.approx(_exp1))

@pytest.mark.xfail()
def test_limit_inds_error(tmp_path):
file_name = 'user_parameters.yaml'
p, f = utilities.create_temporary_file(tmp_path, file_name)
utilities.write_parameter_to_file(f, 'seed', 42)
utilities.write_parameter_to_file(f, 'Length', 30.)
utilities.write_parameter_to_file(f, 'Width', 60.)
utilities.write_parameter_to_file(f, 'dx', 1)
utilities.write_parameter_to_file(f, 'verbose', 1)
utilities.write_parameter_to_file(f, 'Np_water', 20)
utilities.write_parameter_to_file(f, 'Np_sed', 20)
utilities.write_parameter_to_file(f, 'f_bedload', 0.65)
f.close()
delta = DeltaModel(input_file=p)

for _ in range(0, 2):
for _ in range(0, 30):
delta.update()

# slice is: test_DeltaModel.eta[:5, 4]
print(delta.eta[:5, 2])
print(delta.eta[:5, 62])

_exp = np.array([1.7, 0.83358884, -0.9256229, -1., -1.])
assert np.all(delta.eta[:5, 2] == pytest.approx(_exp))
_exp2 = np.array([-4.962428, -1.3612521, -2.2904062, -1.4572337, -0.864957])
assert np.all(delta.eta[:5, 62] == pytest.approx(_exp2))
109 changes: 101 additions & 8 deletions tests/test_shared_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,12 @@
import os
import numpy as np

# from pyDeltaRCM.deltaRCM_driver import pyDeltaRCM
from pyDeltaRCM.model import DeltaModel
from pyDeltaRCM import shared_tools
import utilities
from utilities import test_DeltaModel


# need to create a simple case of pydeltarcm object to test these functions
# delta = DeltaModel(input_file=os.path.join(os.getcwd(), 'tests', 'test.yaml'))

# now that it is initiated can access the shared_tools via the inherited object
# delta._delta.**shared_tools_function**


def test_set_random_assignments(test_DeltaModel):
"""
Test for function shared_tools.get_random_uniform and
Expand Down Expand Up @@ -232,3 +226,102 @@ def test_version_is_valid():
assert type(v) is str
dots = [i for i, c in enumerate(v) if c == '.']
assert len(dots) == 2


@pytest.mark.xfail(raises=IndexError, strict=True)
def test_limit_inds_error(tmp_path):
"""IndexError on corner.
This test throws an error by trying to index cell 1800 of a 30x60 array.
This exceeds the limit of the array. I suspect this is a bug with the
unravel in shared_tools.
The xfail should be removed when the bug is fixed.
"""

file_name = 'user_parameters.yaml'
p, f = utilities.create_temporary_file(tmp_path, file_name)
utilities.write_parameter_to_file(f, 'seed', 42)
utilities.write_parameter_to_file(f, 'Length', 30.)
utilities.write_parameter_to_file(f, 'Width', 60.)
utilities.write_parameter_to_file(f, 'dx', 1)
utilities.write_parameter_to_file(f, 'verbose', 1)
utilities.write_parameter_to_file(f, 'Np_water', 20)
utilities.write_parameter_to_file(f, 'Np_sed', 20)
utilities.write_parameter_to_file(f, 'f_bedload', 0.65)
f.close()
delta = DeltaModel(input_file=p)

for _ in range(0, 2):
delta.update()

# slice is: test_DeltaModel.eta[:5, 4]
print(delta.eta[:5, 2])

_exp = np.array([1.7, 0.83358884, -0.9256229, -1., -1.])
assert np.all(delta.eta[:5, 2] == pytest.approx(_exp))


"""
This test cannot be enabled because it causes a segfault. For some reason,
this configuration results in an index error (like the below test) if you run
the config as a normal model run, but produces a segfault inside the test.
@pytest.mark.xfail(raises=IndexError, strict=True)
def test_limits_inds_error_segfault_error(tmp_path):
file_name = 'user_parameters.yaml'
p, f = utilities.create_temporary_file(tmp_path, file_name)
utilities.write_parameter_to_file(f, 'seed', 43)
utilities.write_parameter_to_file(f, 'Length', 30.)
utilities.write_parameter_to_file(f, 'Width', 60.)
utilities.write_parameter_to_file(f, 'dx', 1)
utilities.write_parameter_to_file(f, 'verbose', 1)
utilities.write_parameter_to_file(f, 'Np_water', 20)
utilities.write_parameter_to_file(f, 'Np_sed', 20)
utilities.write_parameter_to_file(f, 'f_bedload', 0.65)
f.close()
delta = DeltaModel(input_file=p)
for _ in range(0, 2):
delta.update()
# slice is: test_DeltaModel.eta[:5, 4]
print(delta.eta[:5, 2])
_exp = np.array([1.7, 0.83358884, -0.9256229, -1., -1.])
assert np.all(delta.eta[:5, 2] == pytest.approx(_exp))
@pytest.mark.xfail(raises=IndexError, strict=True)
def test_limit_inds_error(tmp_path):
# IndexError on corner.
# This test throws an error by trying to index cell 1800 of a 30x60 array.
# This exceeds the limit of the array. I suspect this is a bug with the
# unravel in shared_tools.
# The xfail should be removed when the bug is fixed.
file_name = 'user_parameters.yaml'
p, f = utilities.create_temporary_file(tmp_path, file_name)
utilities.write_parameter_to_file(f, 'seed', 42)
utilities.write_parameter_to_file(f, 'Length', 20.)
utilities.write_parameter_to_file(f, 'Width', 10.)
utilities.write_parameter_to_file(f, 'dx', 2)
utilities.write_parameter_to_file(f, 'verbose', 1)
utilities.write_parameter_to_file(f, 'Np_water', 20)
utilities.write_parameter_to_file(f, 'Np_sed', 20)
utilities.write_parameter_to_file(f, 'f_bedload', 0.65)
f.close()
delta = DeltaModel(input_file=p)
for _ in range(0, 2):
delta.update()
# slice is: test_DeltaModel.eta[:5, 4]
print(delta.eta[:5, 2])
_exp = np.array([1.7, 0.83358884, -0.9256229, -1., -1.])
assert np.all(delta.eta[:5, 2] == pytest.approx(_exp))
"""

0 comments on commit d4ede5e

Please sign in to comment.