Skip to content

Commit a865b42

Browse files
tests: Pin the cells of a seed for r.random.cells (#8105)
Adds two tests pinning the cells r.random.cells fills for one seed on a region of 4 by 5 cells, so a change of the random number generator shows as a change of the result. The existing tests check counts and bounds only. Both the distance driven fill and the ncells limited fill are pinned, to catch a change in the order the cells are drawn.
1 parent b8c04e3 commit a865b42

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

‎raster/r.random.cells/tests/r_random_cells_test.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# SPDX-License-Identifier: GPL-2.0-or-later
44
import os
55

6+
import numpy as np
67
import pytest
78

89
import grass.script as gs
@@ -62,3 +63,41 @@ def test_fill_count(session_with_region):
6263
assert univar["null_cells"] == N_CELLS - count
6364
assert univar["min"] == 1
6465
assert univar["max"] == count
66+
67+
68+
@pytest.fixture
69+
def session_small_region(tmp_path):
70+
"""A session with a region of 4 rows and 5 columns."""
71+
project = tmp_path / "r_random_cells_small"
72+
gs.create_project(project)
73+
with gs.setup.init(project, env=os.environ.copy()) as session:
74+
Tools(session=session).g_region(n=4, s=0, e=5, w=0, res=1)
75+
yield session
76+
77+
78+
# The maps are integer, so unfilled cells come back from the array conversion
79+
# as 0 rather than as a null. Categories start at 1, so the two do not overlap.
80+
def test_cells_of_a_seed(session_small_region):
81+
"""The cells a seed fills are the pinned ones."""
82+
tools = Tools(session=session_small_region)
83+
result = tools.r_random_cells(output=np.array, distance=2, seed=100)
84+
expected = [
85+
[4, 0, 2, 0, 0],
86+
[0, 0, 0, 0, 5],
87+
[0, 0, 0, 0, 0],
88+
[3, 0, 0, 1, 0],
89+
]
90+
np.testing.assert_array_equal(result, expected)
91+
92+
93+
def test_cells_of_a_seed_with_count(session_small_region):
94+
"""ncells= keeps the first cells of the same seed."""
95+
tools = Tools(session=session_small_region)
96+
result = tools.r_random_cells(output=np.array, distance=2, seed=100, ncells=3)
97+
expected = [
98+
[0, 0, 2, 0, 0],
99+
[0, 0, 0, 0, 0],
100+
[0, 0, 0, 0, 0],
101+
[3, 0, 0, 1, 0],
102+
]
103+
np.testing.assert_array_equal(result, expected)

0 commit comments

Comments
 (0)