-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubblesort.spec.js
43 lines (34 loc) · 1.13 KB
/
bubblesort.spec.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
'use strict';
/* global bubbleSort inOrder swap */
var numerically = function (a, b) { return a - b; }
var generateArray = function (size, lower, upper) {
var randomArray = [];
while (size--) {
var randomNum = Math.floor(lower + Math.random() * upper);
randomArray.push(randomNum);
}
return randomArray;
}
describe('Bubble Sort', function(){
it('sorts an empty array', function(){
expect( bubbleSort([]) ).toEqual( [] );
});
it('sorts an array of one element', function(){
expect( bubbleSort([7]) ).toEqual( [7] );
});
// it('sorts an array of many elements', function(){
// expect( bubbleSort([5, 2, 7, 9, 3, 5, 4, 1, 0]) ).toEqual([0, 1, 2, 3, 4, 5, 5, 7, 9]);
// });
for (var i = 2; i < 103; i += 20) {
it('sorts an array of ' + i + ' random items', function(){
var arr = generateArray(i, 0, 100);
var sorted = arr.slice(0).sort(numerically);
expect( bubbleSort(arr) ).toEqual( sorted );
});
}
it('swaps the expected number of times', function(){
spyOn(window, 'swap').and.callThrough();
bubbleSort([4, 6, 5, 1]);
expect(swap.calls.count()).toEqual(4);
});
});