From a9e65cd865cedf76557df237bfdb134533aece08 Mon Sep 17 00:00:00 2001 From: Kasun Thennakoon Date: Tue, 2 Aug 2022 18:53:41 +0530 Subject: [PATCH] Wrapping the example matrix with outer array (#1057) --- Graphs/NumberOfIslands.js | 7 ------- Graphs/test/NumberOfIslands.test.js | 31 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 Graphs/test/NumberOfIslands.test.js diff --git a/Graphs/NumberOfIslands.js b/Graphs/NumberOfIslands.js index 84b666d37..62891a481 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 000000000..3beb1da75 --- /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) + }) +})