1+ import unittest
2+ from typing import List
3+ from islands import Solution
4+
5+ class TestNumIslands (unittest .TestCase ):
6+
7+ def setUp (self ):
8+ self .solution = Solution ()
9+
10+ def test_basic_multiple_islands (self ):
11+ """Test case with multiple distinct islands"""
12+ grid = [
13+ ["1" ,"1" ,"1" ,"1" ,"0" ],
14+ ["1" ,"1" ,"0" ,"1" ,"0" ],
15+ ["1" ,"1" ,"0" ,"0" ,"0" ],
16+ ["0" ,"0" ,"0" ,"0" ,"0" ]
17+ ]
18+ self .assertEqual (self .solution .numIslands (grid ), 1 )
19+
20+ def test_separate_islands (self ):
21+ """Test case with clearly separated islands"""
22+ grid = [
23+ ["1" ,"1" ,"0" ,"0" ,"0" ],
24+ ["1" ,"1" ,"0" ,"0" ,"0" ],
25+ ["0" ,"0" ,"1" ,"0" ,"0" ],
26+ ["0" ,"0" ,"0" ,"1" ,"1" ]
27+ ]
28+ self .assertEqual (self .solution .numIslands (grid ), 3 )
29+
30+ def test_single_cell_island (self ):
31+ """Test with single cell island"""
32+ grid = [["1" ]]
33+ self .assertEqual (self .solution .numIslands (grid ), 1 )
34+
35+ def test_single_cell_water (self ):
36+ """Test with single cell of water"""
37+ grid = [["0" ]]
38+ self .assertEqual (self .solution .numIslands (grid ), 0 )
39+
40+ def test_all_water (self ):
41+ """Test grid with only water"""
42+ grid = [
43+ ["0" ,"0" ,"0" ],
44+ ["0" ,"0" ,"0" ],
45+ ["0" ,"0" ,"0" ]
46+ ]
47+ self .assertEqual (self .solution .numIslands (grid ), 0 )
48+
49+ def test_all_land (self ):
50+ """Test grid with only land (one big island)"""
51+ grid = [
52+ ["1" ,"1" ,"1" ],
53+ ["1" ,"1" ,"1" ],
54+ ["1" ,"1" ,"1" ]
55+ ]
56+ self .assertEqual (self .solution .numIslands (grid ), 1 )
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 .numIslands (grid ), 5 )
66+
67+ def test_complex_shape (self ):
68+ """Test with complex island shapes"""
69+ grid = [
70+ ["1" ,"1" ,"0" ,"0" ,"1" ],
71+ ["1" ,"0" ,"0" ,"1" ,"1" ],
72+ ["0" ,"0" ,"1" ,"0" ,"0" ],
73+ ["0" ,"0" ,"0" ,"1" ,"1" ]
74+ ]
75+ self .assertEqual (self .solution .numIslands (grid ), 4 )
76+
77+ def test_snake_island (self ):
78+ """Test with a snake-like connected island"""
79+ grid = [
80+ ["1" ,"0" ,"0" ,"0" ],
81+ ["1" ,"1" ,"0" ,"0" ],
82+ ["0" ,"1" ,"1" ,"0" ],
83+ ["0" ,"0" ,"1" ,"1" ]
84+ ]
85+ self .assertEqual (self .solution .numIslands (grid ), 1 )
86+
87+ def test_vertical_line (self ):
88+ """Test with vertical line island"""
89+ grid = [
90+ ["1" ],
91+ ["1" ],
92+ ["1" ],
93+ ["1" ]
94+ ]
95+ self .assertEqual (self .solution .numIslands (grid ), 1 )
96+
97+ def test_horizontal_line (self ):
98+ """Test with horizontal line island"""
99+ grid = [["1" ,"1" ,"1" ,"1" ]]
100+ self .assertEqual (self .solution .numIslands (grid ), 1 )
101+
102+ def test_checkerboard_pattern (self ):
103+ """Test with alternating pattern"""
104+ grid = [
105+ ["1" ,"0" ,"1" ,"0" ],
106+ ["0" ,"1" ,"0" ,"1" ],
107+ ["1" ,"0" ,"1" ,"0" ],
108+ ["0" ,"1" ,"0" ,"1" ]
109+ ]
110+ self .assertEqual (self .solution .numIslands (grid ), 8 )
111+
112+ def test_large_grid_one_island (self ):
113+ """Test with larger grid containing one island"""
114+ grid = [
115+ ["1" ,"1" ,"1" ,"1" ,"1" ],
116+ ["1" ,"0" ,"0" ,"0" ,"1" ],
117+ ["1" ,"0" ,"0" ,"0" ,"1" ],
118+ ["1" ,"0" ,"0" ,"0" ,"1" ],
119+ ["1" ,"1" ,"1" ,"1" ,"1" ]
120+ ]
121+ self .assertEqual (self .solution .numIslands (grid ), 1 )
122+
123+ def test_surrounded_water (self ):
124+ """Test island surrounded by water"""
125+ grid = [
126+ ["0" ,"0" ,"0" ,"0" ,"0" ],
127+ ["0" ,"1" ,"1" ,"1" ,"0" ],
128+ ["0" ,"1" ,"0" ,"1" ,"0" ],
129+ ["0" ,"1" ,"1" ,"1" ,"0" ],
130+ ["0" ,"0" ,"0" ,"0" ,"0" ]
131+ ]
132+ self .assertEqual (self .solution .numIslands (grid ), 1 )
133+
134+
135+ if __name__ == '__main__' :
136+ unittest .main (verbosity = 2 )
0 commit comments