1+ import unittest
2+ from collections import deque
3+ from typing import List
4+ from perimeter import Solution
5+
6+ class TestIslandPerimeter (unittest .TestCase ):
7+
8+ def setUp (self ):
9+ self .solution = Solution ()
10+
11+ def test_single_cell (self ):
12+ """Test with a single land cell"""
13+ grid = [[1 ]]
14+ self .assertEqual (self .solution .islandPerimeter (grid ), 4 )
15+
16+ def test_single_cell_with_water (self ):
17+ """Test single land cell surrounded by water"""
18+ grid = [
19+ [0 , 0 , 0 ],
20+ [0 , 1 , 0 ],
21+ [0 , 0 , 0 ]
22+ ]
23+ self .assertEqual (self .solution .islandPerimeter (grid ), 4 )
24+
25+ def test_two_cells_horizontal (self ):
26+ """Test two adjacent cells horizontally"""
27+ grid = [[1 , 1 ]]
28+ self .assertEqual (self .solution .islandPerimeter (grid ), 6 )
29+
30+ def test_two_cells_vertical (self ):
31+ """Test two adjacent cells vertically"""
32+ grid = [
33+ [1 ],
34+ [1 ]
35+ ]
36+ self .assertEqual (self .solution .islandPerimeter (grid ), 6 )
37+
38+ def test_example_1 (self ):
39+ """Test LeetCode Example 1"""
40+ grid = [
41+ [0 , 1 , 0 , 0 ],
42+ [1 , 1 , 1 , 0 ],
43+ [0 , 1 , 0 , 0 ],
44+ [1 , 1 , 0 , 0 ]
45+ ]
46+ self .assertEqual (self .solution .islandPerimeter (grid ), 16 )
47+
48+ def test_example_2 (self ):
49+ """Test LeetCode Example 2"""
50+ grid = [[1 ]]
51+ self .assertEqual (self .solution .islandPerimeter (grid ), 4 )
52+
53+ def test_example_3 (self ):
54+ """Test LeetCode Example 3"""
55+ grid = [[1 , 0 ]]
56+ self .assertEqual (self .solution .islandPerimeter (grid ), 4 )
57+
58+ def test_square_island (self ):
59+ """Test 2x2 square island"""
60+ grid = [
61+ [1 , 1 ],
62+ [1 , 1 ]
63+ ]
64+ self .assertEqual (self .solution .islandPerimeter (grid ), 8 )
65+
66+ def test_3x3_square_island (self ):
67+ """Test 3x3 square island"""
68+ grid = [
69+ [1 , 1 , 1 ],
70+ [1 , 1 , 1 ],
71+ [1 , 1 , 1 ]
72+ ]
73+ self .assertEqual (self .solution .islandPerimeter (grid ), 12 )
74+
75+ def test_l_shaped_island (self ):
76+ """Test L-shaped island"""
77+ grid = [
78+ [1 , 0 ],
79+ [1 , 0 ],
80+ [1 , 1 ]
81+ ]
82+ self .assertEqual (self .solution .islandPerimeter (grid ), 10 )
83+
84+ def test_island_at_corner (self ):
85+ """Test island starting at top-left corner"""
86+ grid = [
87+ [1 , 1 , 0 ],
88+ [1 , 0 , 0 ],
89+ [0 , 0 , 0 ]
90+ ]
91+ self .assertEqual (self .solution .islandPerimeter (grid ), 8 )
92+
93+ def test_island_at_bottom_right (self ):
94+ """Test island at bottom-right corner"""
95+ grid = [
96+ [0 , 0 , 0 ],
97+ [0 , 0 , 1 ],
98+ [0 , 1 , 1 ]
99+ ]
100+ self .assertEqual (self .solution .islandPerimeter (grid ), 8 )
101+
102+ def test_long_horizontal_line (self ):
103+ """Test long horizontal line of land"""
104+ grid = [[1 , 1 , 1 , 1 , 1 ]]
105+ self .assertEqual (self .solution .islandPerimeter (grid ), 12 )
106+
107+ def test_long_vertical_line (self ):
108+ """Test long vertical line of land"""
109+ grid = [
110+ [1 ],
111+ [1 ],
112+ [1 ],
113+ [1 ],
114+ [1 ]
115+ ]
116+ self .assertEqual (self .solution .islandPerimeter (grid ), 12 )
117+
118+ def test_plus_shaped_island (self ):
119+ """Test plus/cross shaped island"""
120+ grid = [
121+ [0 , 1 , 0 ],
122+ [1 , 1 , 1 ],
123+ [0 , 1 , 0 ]
124+ ]
125+ self .assertEqual (self .solution .islandPerimeter (grid ), 12 )
126+
127+ def test_complex_shape (self ):
128+ """Test complex irregular shape"""
129+ grid = [
130+ [1 , 1 , 0 , 0 ],
131+ [1 , 1 , 1 , 0 ],
132+ [0 , 0 , 1 , 0 ],
133+ [0 , 0 , 1 , 1 ]
134+ ]
135+ self .assertEqual (self .solution .islandPerimeter (grid ), 16 )
136+
137+ def test_snake_shape (self ):
138+ """Test snake-like shape"""
139+ grid = [
140+ [1 , 1 , 0 ],
141+ [0 , 1 , 0 ],
142+ [0 , 1 , 1 ]
143+ ]
144+ self .assertEqual (self .solution .islandPerimeter (grid ), 12 )
145+
146+ def test_all_water_except_one (self ):
147+ """Test large grid with single land cell"""
148+ grid = [
149+ [0 , 0 , 0 , 0 ],
150+ [0 , 0 , 0 , 0 ],
151+ [0 , 0 , 1 , 0 ],
152+ [0 , 0 , 0 , 0 ]
153+ ]
154+ self .assertEqual (self .solution .islandPerimeter (grid ), 4 )
155+
156+
157+ if __name__ == '__main__' :
158+ # Run tests with verbose output
159+ unittest .main (verbosity = 2 )
0 commit comments