forked from mgechev/javascript-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.testcase.js
More file actions
65 lines (54 loc) · 1.59 KB
/
sort.testcase.js
File metadata and controls
65 lines (54 loc) · 1.59 KB
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
61
62
63
64
65
module.exports = function (sort, algorithmName, options) {
'use strict';
options = options || {
integers: false,
reverse: true
};
describe(algorithmName, function () {
function createRandomArray(config) {
config = config || {};
var size = config.size || 100;
var precision = config.precision || 2;
var multiplier = config.multiplier || 100;
var result = [];
for (var i = size; i > 0; i -= 1) {
result.push(parseFloat((Math.random() *
multiplier).toFixed(precision)));
}
return result;
}
it('should work with empty array', function () {
expect(sort([])).toEqual([]);
});
it('should work with sorted arrays', function () {
expect(sort([1, 2, 3, 4])).toEqual([1, 2, 3, 4]);
});
it('should work with random non-sorted arrays', function () {
var array;
if (options.integers) {
array = createRandomArray();
} else {
array = createRandomArray({
precision: 0
});
}
sort(array);
for (var i = 0; i < array.length - 1; i += 1) {
expect(array[i] <= array[i + 1]).toBeTruthy();
}
});
if (options.reverse) {
it('should sort the numbers in descending order ' +
'when such comparator is provided', function () {
function comparator(a, b) {
return b - a;
}
var array = createRandomArray();
sort(array, comparator);
for (var i = 0; i < array.length - 1; i += 1) {
expect(array[i] >= array[i + 1]).toBeTruthy();
}
});
}
});
};