diff --git a/Graphs/NumberOfIslands.js b/Graphs/NumberOfIslands.js index 84b666d373..62891a4814 100644 --- a/Graphs/NumberOfIslands.js +++ b/Graphs/NumberOfIslands.js @@ -79,10 +79,3 @@ const islands = (matrixGrid) => { } export { islands } - -// islands( -// ['1', '1', '0', '0', '0'], -// ['1', '1', '0', '0', '0'], -// ['0', '0', '1', '0', '0'], -// ['0', '0', '0', '1', '1'] -// ) diff --git a/Graphs/test/NumberOfIslands.test.js b/Graphs/test/NumberOfIslands.test.js new file mode 100644 index 0000000000..3beb1da75a --- /dev/null +++ b/Graphs/test/NumberOfIslands.test.js @@ -0,0 +1,31 @@ +import { islands } from '../NumberOfIslands' + +describe('Number of Islands', () => { + test('Graph with three islands', () => { + const graph = [ + ['1', '1', '0', '0', '0'], + ['1', '1', '0', '0', '0'], + ['0', '0', '1', '0', '0'], + ['0', '0', '0', '1', '1'] + ] + expect(islands(graph)).toBe(3) + }) + + test('Graph with only one island', () => { + const graph = [ + ['1', '1'], + ['1', '1'], + ['0', '0'], + ['0', '0'] + ] + expect(islands(graph)).toBe(1) + }) + + test('No islands', () => { + const graph = [ + ['0', '0'], + ['0', '0'] + ] + expect(islands(graph)).toBe(0) + }) +})