-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIslandTest.py
More file actions
107 lines (94 loc) · 2.48 KB
/
IslandTest.py
File metadata and controls
107 lines (94 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import unittest
import Island as IslandClass
class TestNumIslands(unittest.TestCase):
island = IslandClass.Island()
# Test an empty grid
def test_empty_grid(self):
grid = []
self.assertEqual(self.island.numIslands(grid), 0)
# Test grid with single island
def test_single_island(self):
grid = [[1]]
self.assertEqual(self.island.numIslands(grid), 1)
# Test grid with multiple islands
def test_multiple_islands(self):
grid = [
[1, 0, 1],
[0, 1, 0],
[1, 0, 1]
]
self.assertEqual(self.island.numIslands(grid), 1)
# Test grid with no islands
def test_no_islands(self):
grid = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
]
self.assertEqual(self.island.numIslands(grid), 0)
# Test grid with all cells set to 1
def test_all_ones(self):
grid = [
[1, 1, 1],
[1, 1, 1],
[1, 1, 1]
]
self.assertEqual(self.island.numIslands(grid), 1)
# Test grid with a single row
def test_single_row(self):
grid = [
[1, 1, 1]
]
self.assertEqual(self.island.numIslands(grid), 1)
# Test grid with a single column
def test_single_column(self):
grid = [
[1],
[1],
[1]
]
self.assertEqual(self.island.numIslands(grid), 1)
# Test grid with islands connected diagonally
def test_diagonal_islands(self):
grid = [
[1, 0, 0, 0, 1],
[0, 1, 0, 1, 0],
[0, 0, 1, 0, 1],
[1, 0, 0, 0, 0],
[0, 1, 0, 0, 0]
]
self.assertEqual(self.island.numIslands(grid), 2)
# Test a mixed grid
def test_mixed_grid(self):
grid = [
[1, 1, 0, 0, 1],
[0, 1, 1, 0, 0],
[0, 0, 0, 1, 1],
[1, 0, 1, 0, 1],
[1, 0, 1, 1, 1]
]
self.assertEqual(self.island.numIslands(grid), 3)
# Test grid with islands that are adjacent horizontally, vertically and diagonally
def test_adjacent_islands(self):
grid = [
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 0, 0, 0, 1, 0, 0],
[1, 1, 0, 0, 0, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 1, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 0, 0]
]
self.assertEqual(self.island.numIslands(grid), 4)
# Test single island surrounded by water
def test_island_surrounded_by_water(self):
grid = [
[0,0,0,0,0],
[0,1,1,1,0],
[0,1,1,1,0],
[0,0,0,0,0]
]
self.assertEqual(self.island.numIslands(grid), 1)
if __name__ == "__main__":
unittest.main()