Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

test: add IntroSort tests #1862

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
crhallberg wants to merge 3 commits into TheAlgorithms:master
base: master
Choose a base branch
Loading
from crhallberg:test-introsort
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 1 addition & 66 deletions Sorts/IntroSort.js
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
74 changes: 74 additions & 0 deletions Sorts/test/IntroSort.test.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { introsort } from '../IntroSort'

describe('introsort', () => {
it('should have a robust default comparator', () => {
// toString, null, undefined
const mixedData = [undefined, '2', 1, false, null, { a: 7 }]
introsort(mixedData)
expect(mixedData).toEqual([1, '2', { a: 7 }, false, null, undefined])

// Symbol
expect(() => introsort([Symbol(), Symbol()])).toThrowError()
})

it('fails gracefully', () => {
introsort('string')
introsort([])
introsort(['one len'])
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)
})
})

AltStyle によって変換されたページ (->オリジナル) /