forked from crizstian/data-structure-and-algorithms-with-ES6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
60 lines (46 loc) · 1.89 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const {sort} = require('./sort-module')
const nsTime = (hrtime) => hrtime[0] * 1e9 + hrtime[1]
function setData (numElements = 100) {
let dataStore = []
for (let i = 0; i < numElements; i++) {
dataStore[i] = Math.floor(Math.random() * (numElements + 1))
}
return dataStore
}
let basic = 50
test (`bubbleSort test with ${basic} elements`, assert => {
let start = process.hrtime()
let dataStore = setData(basic)
let sorted = sort(dataStore).bubbleSort()
let end = process.hrtime(start)
assert.equal(dataStore.sort(), sorted, `Elapsed time on ${basic} elements is: ${nsTime(end) / 1e9} seconds.`)
})
test (`selectionSort test with ${basic} elements`, assert => {
let start = process.hrtime()
let dataStore = setData(basic)
let sorted = sort(dataStore).selectionSort()
let end = process.hrtime(start)
assert.equal(dataStore.sort(), sorted, `Elapsed time on ${basic} elements is: ${nsTime(end) / 1e9} seconds.`)
})
test (`insertionSort test with ${basic} elements`, assert => {
let start = process.hrtime()
let dataStore = setData(basic)
let sorted = sort(dataStore).insertionSort()
let end = process.hrtime(start)
assert.equal(dataStore.sort(), sorted, `Elapsed time on ${basic} elements is: ${nsTime(end) / 1e9} seconds.`)
})
let advance = 1000
test (`mergeSort test with ${advance} elements`, assert => {
let start = process.hrtime()
let dataStore = setData(advance)
let sorted = sort(dataStore).mergeSort()
let end = process.hrtime(start)
assert.equal(dataStore.sort(), sorted, `Elapsed time on ${advance} elements is: ${nsTime(end) / 1e9} seconds.`)
})
test (`quickSort test with ${advance} elements`, assert => {
let start = process.hrtime()
let dataStore = setData(advance)
let sorted = sort(dataStore).quickSort()
let end = process.hrtime(start)
assert.equal(dataStore.sort(), sorted, `Elapsed time on ${advance} elements is: ${nsTime(end) / 1e9} seconds.`)
})