From 73efc89d29250547ef5bf05575b00c11a514c55c Mon Sep 17 00:00:00 2001 From: Ankush263 <86042508+Ankush263@users.noreply.github.com> Date: Thu, 24 Mar 2022 21:50:39 +0530 Subject: [PATCH] merge: Fix GnomeSort algorithm and Add test case to it (#948) --- Sorts/GnomeSort.js | 1 + Sorts/test/GnomeSort.test.js | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 Sorts/test/GnomeSort.test.js diff --git a/Sorts/GnomeSort.js b/Sorts/GnomeSort.js index 59f68b33c..281b96afa 100644 --- a/Sorts/GnomeSort.js +++ b/Sorts/GnomeSort.js @@ -19,6 +19,7 @@ export function gnomeSort (items) { i = Math.max(1, i - 1) } } + return items } // Implementation of gnomeSort diff --git a/Sorts/test/GnomeSort.test.js b/Sorts/test/GnomeSort.test.js new file mode 100644 index 000000000..a769ebcc5 --- /dev/null +++ b/Sorts/test/GnomeSort.test.js @@ -0,0 +1,19 @@ +import { gnomeSort } from '../GnomeSort' + +test('The gnomeSort of the array [5, 4, 3, 2, 1] is [1, 2, 3, 4, 5]', () => { + const arr = [5, 4, 3, 2, 1] + const res = gnomeSort(arr) + expect(res).toEqual([1, 2, 3, 4, 5]) +}) + +test('The gnomeSort of the array [-5, 4, -3, 2, -1] is [-5, -3, -1, 2, 4]', () => { + const arr = [-5, 4, -3, 2, -1] + const res = gnomeSort(arr) + expect(res).toEqual([-5, -3, -1, 2, 4]) +}) + +test('The gnomeSort of the array [15, 4, -13, 2, -11] is [-13, -11, 2, 4, 15]', () => { + const arr = [15, 4, -13, 2, -11] + const res = gnomeSort(arr) + expect(res).toEqual([-13, -11, 2, 4, 15]) +})