Skip to content

Commit dc8a3ba

Browse files
committed
Cover pixelunshuffle()'s indivisible-dimension error path
pixelunshuffle() raises ValueError when a spatial dimension is not divisible by the scale factor. Nothing exercised that branch: the five existing tests all use shapes that divide cleanly, so the guard clause and its message were never executed by the suite. That is why the malformed message corrected in the preceding commit survived from March 2025 without anyone noticing. This test covers that branch, and is written so it would have caught that specific defect. The wording of the assertion is load-bearing, not incidental, because the malformed code raises ValueError too: assertRaises(ValueError) alone passes on the broken message match "divisible by factor" passes on the broken message match "factor 2, spatial" fails on the broken message Only a pattern spanning the point where the two literals were joined can tell the two apart, so the assertion has to reach across it. Against the unfixed source it reports: AssertionError: "divisible by factor 2, spatial shape is: \[7, 8\]" does not match "All spatial dimensions must be divisible by factor 2. , spatial shape is: [7, 8]" The trade-off is that the test is coupled to the message text and will need updating if the message is reworded. That is the cost of pinning the defect; a looser assertion would pass either way and prove nothing. Kept as a separate commit so the text corrections can be reviewed, or reverted, independently of the new coverage. Signed-off-by: Hans Johnson <hans-johnson@uiowa.edu>
1 parent 4941405 commit dc8a3ba

1 file changed

Lines changed: 5 additions & 0 deletions

File tree

tests/networks/utils/test_pixelunshuffle.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ def test_different_scale_factor(self):
4040
out = pixelunshuffle(x, spatial_dims=2, scale_factor=3)
4141
torch.testing.assert_close(out, torch.pixel_unshuffle(x, 3))
4242

43+
def test_indivisible_spatial_dims(self):
44+
x = torch.randn(1, 2, 7, 8)
45+
with self.assertRaisesRegex(ValueError, r"divisible by factor 2, spatial shape is: \[7, 8\]"):
46+
pixelunshuffle(x, spatial_dims=2, scale_factor=2)
47+
4348
def test_inverse_operation(self):
4449
x = torch.arange(4096).reshape(1, 8, 8, 8, 8)
4550
shuffled = pixelshuffle(x, spatial_dims=3, scale_factor=2)

0 commit comments

Comments
 (0)