|
2 | 2 |
|
3 | 3 | import copy |
4 | 4 | import pickle |
| 5 | +from collections.abc import Callable |
5 | 6 |
|
6 | 7 | import numpy as np |
7 | 8 | import pytest |
@@ -103,3 +104,20 @@ def test_noise_copy() -> None: |
103 | 104 |
|
104 | 105 | def test_noise_grid() -> None: |
105 | 106 | tcod.noise.grid((2, 2), scale=2) # Check int scale |
| 107 | + |
| 108 | + |
| 109 | +@pytest.mark.parametrize("algorithm", [tcod.noise.Algorithm.PERLIN, tcod.noise.Algorithm.SIMPLEX]) |
| 110 | +@pytest.mark.parametrize("rng_algorithm", [tcod.random.MERSENNE_TWISTER, tcod.random.MULTIPLY_WITH_CARRY]) |
| 111 | +@pytest.mark.parametrize("snapshot", [copy.copy, copy.deepcopy, lambda n: pickle.loads(pickle.dumps(n))]) |
| 112 | +def test_noise_snapshot_preserves_shared_random( |
| 113 | + algorithm: int, rng_algorithm: int, snapshot: Callable[[tcod.noise.Noise], tcod.noise.Noise] |
| 114 | +) -> None: |
| 115 | + rng = tcod.random.Random(rng_algorithm, seed=42) |
| 116 | + noise = tcod.noise.Noise(2, algorithm=algorithm, seed=rng) |
| 117 | + expected = copy.deepcopy(rng) |
| 118 | + noise_copy = snapshot(noise) |
| 119 | + assert [rng.randint(0, 100) for _ in range(8)] == [expected.randint(0, 100) for _ in range(8)] |
| 120 | + assert noise.algorithm == algorithm |
| 121 | + # The lazy wavelet tile remains synchronized if this algorithm is selected later. |
| 122 | + noise.algorithm = noise_copy.algorithm = tcod.noise.Algorithm.WAVELET |
| 123 | + np.testing.assert_array_equal(noise.sample_ogrid(np.ogrid[:3, :4]), noise_copy.sample_ogrid(np.ogrid[:3, :4])) |
0 commit comments