Skip to content

Commit

Permalink
r.grow: added test file for r.grow module (OSGeo#4691)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shreshth-Malik authored Nov 12, 2024
1 parent 8472773 commit 660e8f8
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions scripts/r.grow/testsuite/test_grow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
from grass.gunittest.case import TestCase
from grass.gunittest.main import test
from grass.gunittest.gmodules import SimpleModule


class TestRGrow(TestCase):

@classmethod
def setUpClass(cls):
"""Set up a small region and test map."""
cls.output = "test_grow"
cls.runModule("g.region", n=10, s=0, e=10, w=0, res=1)

# Create a test map with a centered 3x3 block of 1s
cls.runModule(
"r.mapcalc",
expression="test_map = if(row() > 3 && row() < 7 && col() > 3 && col() < 7, 1, null())",
overwrite=True,
)

@classmethod
def tearDownClass(cls):
"""Clean up test maps after all tests."""
cls.runModule("g.remove", type="raster", name="test_map,test_grow", flags="f")

def tearDown(self):
"""Remove output map after each test to prevent conflicts."""
self.runModule("g.remove", type="raster", name=self.output, flags="f")

def test_default_growth(self):
"""Test default growth using Euclidean metric."""
module = SimpleModule(
"r.grow", input="test_map", output=self.output, overwrite=True
)
self.assertModule(module)

self.assertRasterFitsUnivar(raster=self.output, reference="n=21")

def test_grow_with_manhattan_metric(self):
"""Test growth with Manhattan metric and float radius of 2"""
module = SimpleModule(
"r.grow",
input="test_map",
output=self.output,
radius=2,
metric="manhattan",
overwrite=True,
)
self.assertModule(module)

self.assertRasterFitsUnivar(raster=self.output, reference="n=21")

def test_grow_with_maximum_metric(self):
"""Test growth with Maximum metric."""
module = SimpleModule(
"r.grow",
input="test_map",
output=self.output,
metric="maximum",
overwrite=True,
)
self.assertModule(module)

self.assertRasterFitsUnivar(raster=self.output, reference="n=25")

def test_shrink_with_negative_radius(self):
"""Test shrinking with a negative radius of -2 to reduce area size."""
module = SimpleModule(
"r.grow", input="test_map", radius="-2", output=self.output, overwrite=True
)
self.assertModule(module)

self.assertRasterFitsUnivar(raster=self.output, reference="n=1")

def test_old_value_replacement(self):
"""Test replacing the original cells with -1 and new ones with 2."""
module = SimpleModule(
"r.grow",
input="test_map",
output=self.output,
old=-1,
new="2",
overwrite=True,
)
self.assertModule(module)

expected_values = {"n": 21, "sum": 15, "min": -1, "max": 2}
self.assertRasterFitsUnivar(raster=self.output, reference=expected_values)


if __name__ == "__main__":
test()

0 comments on commit 660e8f8

Please sign in to comment.