From 2a8b4a4835f9371290a056532a6d5923f1417268 Mon Sep 17 00:00:00 2001 From: Chris Hallberg Date: Wed, 3 Dec 2025 17:51:46 -0500 Subject: [PATCH 1/3] test: add IntroSort tests --- Sorts/test/IntroSort.test.js | 73 ++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 Sorts/test/IntroSort.test.js diff --git a/Sorts/test/IntroSort.test.js b/Sorts/test/IntroSort.test.js new file mode 100644 index 0000000000..c531da6c84 --- /dev/null +++ b/Sorts/test/IntroSort.test.js @@ -0,0 +1,73 @@ +import { introsort } from '../IntroSort' + +describe('introsort', () => { + it('should have a robust default comparator', () => { + // toString, null, undefined + const mixedData = [undefined, '2', 1, false, null] + introsort(mixedData) + expect(mixedData).toEqual([1, '2', false, null, undefined]) + + // Symbol + expect(() => introsort([Symbol(), Symbol()])).toThrowError() + }) + + it('fails gracefully', () => { + introsort('string') + introsort([]) + introsort([1, 2, 3], 'string') + }) + + it('should sort randomly generated data', function demo1() { + // make array + const data = [] + const size = 10_000 + for (let i = 0; i < size; i++) { + const temp = Math.random() * Number.MAX_SAFE_INTEGER + data.push(temp) + } + + // custom comparator + const c = function (a, b) { + return a - b + } + introsort(data, c) + + // check that all numbers are smaller than the one after them + let faulty = false + for (let i = 1; i < size; i++) { + if (data[i - 1] > data[i]) { + faulty = true + break + } + } + + expect(faulty).toEqual(false) + }) + + it('should match the sorting of Array.sort()', function demo2() { + // make arrays + const data = [] + const data2 = [] + const size = 10_000 + for (let i = 0; i < size; i++) { + const temp = Math.random() * Number.MAX_SAFE_INTEGER + data.push(temp) + data2.push(temp) + } + + // sort + introsort(data) + data2.sort() + + // verify + let faulty = false + for (let i = 0; i < size; i++) { + if (data[i] !== data2[i]) { + faulty = true + break + } + } + + expect(faulty).toEqual(false) + }) +}) From 1cdd39f0e823dc7c36d3e5fc3b918504a1d6d7e6 Mon Sep 17 00:00:00 2001 From: Chris Hallberg Date: Wed, 3 Dec 2025 17:57:38 -0500 Subject: [PATCH 2/3] feat: move demo functions to tests --- Sorts/IntroSort.js | 67 +--------------------------------------------- 1 file changed, 1 insertion(+), 66 deletions(-) diff --git a/Sorts/IntroSort.js b/Sorts/IntroSort.js index 4874b44082..e54886d5c8 100644 --- a/Sorts/IntroSort.js +++ b/Sorts/IntroSort.js @@ -241,69 +241,4 @@ function introsort(array, compare) { })(array, compare) } -/** - * @example Demo run of the sort routine - * The data is randomly generated - * Returns 'RIGHT:)' if the sort routine worked as expected, - * 'WRONG!!' otherwise - */ -function demo1() { - const data = [] - const size = 1000000 - let i = 0 - let temp - const c = function (a, b) { - return a - b - } - for (i = 0; i < size; i++) { - temp = Math.random() * Number.MAX_SAFE_INTEGER - data.push(temp) - } - introsort(data, c) - let faulty = false - for (i = 1; i < size; i++) { - if (data[i] < data[i - 1]) { - faulty = true - break - } - } - if (faulty) { - return 'WRONG!!' - } else { - return 'RIGHT:)' - } -} - -/** - * @example Demo run of the sort routine - * using the default compare function and - * comparing the results with Array.sort - */ -function demo2() { - const data = [] - const data2 = [] - const size = 1000000 - let i = 0 - let temp - for (i = 0; i < size; i++) { - temp = Math.random() * Number.MAX_SAFE_INTEGER - data.push(temp) - data2.push(temp) - } - introsort(data) - data2.sort() - let faulty = false - for (i = 1; i < size; i++) { - if (data[i] !== data2[i]) { - faulty = true - break - } - } - if (faulty) { - return 'WRONG Implemented Comparator!!' - } else { - return 'Comparator Works Fine:)' - } -} - -export { introsort, demo1, demo2 } +export { introsort } From 109f32d3c31c1c0ac92dd1402477fc1024c9dd11 Mon Sep 17 00:00:00 2001 From: Chris Hallberg Date: Wed, 3 Dec 2025 18:08:54 -0500 Subject: [PATCH 3/3] test: cover missed code branches. --- Sorts/test/IntroSort.test.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Sorts/test/IntroSort.test.js b/Sorts/test/IntroSort.test.js index c531da6c84..28829ee0ce 100644 --- a/Sorts/test/IntroSort.test.js +++ b/Sorts/test/IntroSort.test.js @@ -3,9 +3,9 @@ import { introsort } from '../IntroSort' describe('introsort', () => { it('should have a robust default comparator', () => { // toString, null, undefined - const mixedData = [undefined, '2', 1, false, null] + const mixedData = [undefined, '2', 1, false, null, { a: 7 }] introsort(mixedData) - expect(mixedData).toEqual([1, '2', false, null, undefined]) + expect(mixedData).toEqual([1, '2', { a: 7 }, false, null, undefined]) // Symbol expect(() => introsort([Symbol(), Symbol()])).toThrowError() @@ -14,6 +14,7 @@ describe('introsort', () => { it('fails gracefully', () => { introsort('string') introsort([]) + introsort(['one len']) introsort([1, 2, 3], 'string') })