1+ import unittest
2+ from typing import List
3+ from max import Solution
4+
5+ class TestMaxAreaOfIsland (unittest .TestCase ):
6+
7+ def setUp (self ):
8+ self .solution = Solution ()
9+
10+ def test_example_1 (self ):
11+ """Test with example 1 from problem statement"""
12+ grid = [
13+ [0 ,0 ,1 ,0 ,0 ,0 ,0 ,1 ,0 ,0 ,0 ,0 ,0 ],
14+ [0 ,0 ,0 ,0 ,0 ,0 ,0 ,1 ,1 ,1 ,0 ,0 ,0 ],
15+ [0 ,1 ,1 ,0 ,1 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ],
16+ [0 ,1 ,0 ,0 ,1 ,1 ,0 ,0 ,1 ,0 ,1 ,0 ,0 ],
17+ [0 ,1 ,0 ,0 ,1 ,1 ,0 ,0 ,1 ,1 ,1 ,0 ,0 ],
18+ [0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,1 ,0 ,0 ],
19+ [0 ,0 ,0 ,0 ,0 ,0 ,0 ,1 ,1 ,1 ,0 ,0 ,0 ],
20+ [0 ,0 ,0 ,0 ,0 ,0 ,0 ,1 ,1 ,0 ,0 ,0 ,0 ]
21+ ]
22+ self .assertEqual (self .solution .maxAreaOfIsland (grid ), 6 )
23+
24+ def test_example_2 (self ):
25+ """Test with all zeros (no islands)"""
26+ grid = [[0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ]]
27+ self .assertEqual (self .solution .maxAreaOfIsland (grid ), 0 )
28+
29+ def test_single_cell_island (self ):
30+ """Test with single cell island"""
31+ grid = [[1 ]]
32+ self .assertEqual (self .solution .maxAreaOfIsland (grid ), 1 )
33+
34+ def test_single_cell_water (self ):
35+ """Test with single cell water"""
36+ grid = [[0 ]]
37+ self .assertEqual (self .solution .maxAreaOfIsland (grid ), 0 )
38+
39+ def test_entire_grid_is_island (self ):
40+ """Test when entire grid is one island"""
41+ grid = [
42+ [1 , 1 , 1 ],
43+ [1 , 1 , 1 ],
44+ [1 , 1 , 1 ]
45+ ]
46+ self .assertEqual (self .solution .maxAreaOfIsland (grid ), 9 )
47+
48+ def test_multiple_islands_different_sizes (self ):
49+ """Test with multiple islands of different sizes"""
50+ grid = [
51+ [1 , 1 , 0 , 0 , 0 ],
52+ [1 , 1 , 0 , 0 , 0 ],
53+ [0 , 0 , 0 , 1 , 1 ],
54+ [0 , 0 , 0 , 1 , 1 ]
55+ ]
56+ self .assertEqual (self .solution .maxAreaOfIsland (grid ), 4 )
57+
58+ def test_diagonal_not_connected (self ):
59+ """Test that diagonal cells are not considered connected"""
60+ grid = [
61+ [1 , 0 , 1 ],
62+ [0 , 1 , 0 ],
63+ [1 , 0 , 1 ]
64+ ]
65+ self .assertEqual (self .solution .maxAreaOfIsland (grid ), 1 )
66+
67+ def test_l_shaped_island (self ):
68+ """Test L-shaped island"""
69+ grid = [
70+ [1 , 0 , 0 ],
71+ [1 , 0 , 0 ],
72+ [1 , 1 , 1 ]
73+ ]
74+ self .assertEqual (self .solution .maxAreaOfIsland (grid ), 5 )
75+
76+ def test_snake_shaped_island (self ):
77+ """Test snake-shaped island"""
78+ grid = [
79+ [1 , 1 , 1 , 0 ],
80+ [0 , 0 , 1 , 0 ],
81+ [0 , 0 , 1 , 0 ],
82+ [0 , 1 , 1 , 0 ]
83+ ]
84+ self .assertEqual (self .solution .maxAreaOfIsland (grid ), 7 )
85+
86+ def test_isolated_single_cells (self ):
87+ """Test multiple isolated single-cell islands"""
88+ grid = [
89+ [1 , 0 , 1 , 0 , 1 ],
90+ [0 , 0 , 0 , 0 , 0 ],
91+ [1 , 0 , 1 , 0 , 1 ]
92+ ]
93+ self .assertEqual (self .solution .maxAreaOfIsland (grid ), 1 )
94+
95+ def test_vertical_strip (self ):
96+ """Test vertical strip island"""
97+ grid = [
98+ [0 , 1 , 0 ],
99+ [0 , 1 , 0 ],
100+ [0 , 1 , 0 ],
101+ [0 , 1 , 0 ]
102+ ]
103+ self .assertEqual (self .solution .maxAreaOfIsland (grid ), 4 )
104+
105+ def test_horizontal_strip (self ):
106+ """Test horizontal strip island"""
107+ grid = [
108+ [0 , 0 , 0 , 0 ],
109+ [1 , 1 , 1 , 1 ],
110+ [0 , 0 , 0 , 0 ]
111+ ]
112+ self .assertEqual (self .solution .maxAreaOfIsland (grid ), 4 )
113+
114+ def test_complex_shape (self ):
115+ """Test complex island shape"""
116+ grid = [
117+ [1 , 1 , 0 , 1 , 1 ],
118+ [1 , 0 , 0 , 0 , 1 ],
119+ [1 , 0 , 1 , 0 , 1 ],
120+ [1 , 1 , 1 , 1 , 1 ]
121+ ]
122+ self .assertEqual (self .solution .maxAreaOfIsland (grid ), 14 )
123+
124+ def test_max_constraint_size (self ):
125+ """Test with larger grid approaching constraint limits"""
126+ grid = [[1 ] * 20 for _ in range (20 )]
127+ self .assertEqual (self .solution .maxAreaOfIsland (grid ), 400 )
128+
129+ def test_checkerboard_pattern (self ):
130+ """Test checkerboard pattern (no connected islands)"""
131+ grid = [
132+ [1 , 0 , 1 , 0 ],
133+ [0 , 1 , 0 , 1 ],
134+ [1 , 0 , 1 , 0 ],
135+ [0 , 1 , 0 , 1 ]
136+ ]
137+ self .assertEqual (self .solution .maxAreaOfIsland (grid ), 1 )
138+
139+
140+ if __name__ == '__main__' :
141+ unittest .main ()
0 commit comments