1+ import pytest
2+ utils = pytest .importorskip ("example_transcoding.utils" )
3+ image_u8 = utils .image_u8
4+ color_quad_u8 = utils .color_quad_u8
5+ pixel_coord = utils .pixel_coord
6+
7+ class TestUtils :
8+ def test_flood_fill_stack_overflow_protection (self ):
9+ # Create a large image
10+ img = image_u8 (1000 , 1000 )
11+
12+ # Fill the entire image with a background color
13+ bg_color = color_quad_u8 (0 , 0 , 0 , 255 )
14+ img .set_all (bg_color )
15+
16+ # Try to flood fill with a small region that would cause stack overflow
17+ # This should not cause a crash or infinite loop
18+ fill_color = color_quad_u8 (255 , 255 , 255 , 255 )
19+ b_color = color_quad_u8 (0 , 0 , 0 , 255 )
20+
21+ # Test with a point that would trigger large stack growth
22+ result = img .flood_fill (500 , 500 , fill_color , b_color , None )
23+
24+ # Flood fill a large region; this should complete without stack overflow
25+ # and should not cause a crash or infinite loop
26+ fill_color = color_quad_u8 (255 , 255 , 255 , 255 )
27+ b_color = color_quad_u8 (0 , 0 , 0 , 255 )
28+
29+ # Test with a point that triggers flood fill of the large background region
30+ result = img .flood_fill (500 , 500 , fill_color , b_color , None )
31+
32+ # Should return a positive number indicating pixels filled
33+ assert result > 0
34+
35+ def test_flood_fill_normal_operation (self ):
36+ # Create a small image
37+ img = image_u8 (10 , 10 )
38+
39+ # Fill with background
40+ bg_color = color_quad_u8 (0 , 0 , 0 , 255 )
41+ for y in range (10 ):
42+ for x in range (10 ):
43+ img .set_pixel_clipped (x , y , bg_color )
44+
45+ # Fill a small region
46+ fill_color = color_quad_u8 (255 , 255 , 255 , 255 )
47+ b_color = color_quad_u8 (0 , 0 , 0 , 255 )
48+
49+ # Test normal flood fill operation
50+ result = img .flood_fill (5 , 5 , fill_color , b_color , None )
51+
52+ # Should return a positive number
53+ assert result > 0
54+
55+ def test_flood_fill_edge_case_outside_bounds (self ):
56+ # Create a small image
57+ img = image_u8 (10 , 10 )
58+
59+ # Fill with background
60+ bg_color = color_quad_u8 (0 , 0 , 0 , 255 )
61+ for y in range (10 ):
62+ for x in range (10 ):
63+ img .set_pixel_clipped (x , y , bg_color )
64+
65+ # Try to flood fill outside image bounds
66+ fill_color = color_quad_u8 (255 , 255 , 255 , 255 )
67+ b_color = color_quad_u8 (0 , 0 , 0 , 255 )
68+
69+ # Should return 0 for out of bounds
70+ result = img .flood_fill (- 1 , 5 , fill_color , b_color , None )
71+ assert result == 0
72+
73+ result = img .flood_fill (5 , - 1 , fill_color , b_color , None )
74+ assert result == 0
75+
76+ result = img .flood_fill (15 , 5 , fill_color , b_color , None )
77+ assert result == 0
78+
79+ result = img .flood_fill (5 , 15 , fill_color , b_color , None )
80+ assert result == 0
0 commit comments