-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarySearch_tests.js
79 lines (57 loc) · 2.02 KB
/
binarySearch_tests.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
(function () {
function assert (condition, message) {
if (condition) {
throw new Error(message);
}
}
function assertEqual (reference, value) {
if (reference !== value) {
throw new Error(reference + ' !== ' + value);
}
}
function assertError(statement, errorMessage) {
try {
eval(statement);
} catch (error) {
if(error.toString().search(errorMessage) > -1) {
return;
}
else {
throw new Error('Error should be trown');
}
}
}
var unitTests = {
binarySearchTest: function () {
var arrSorted = [],
matchIndex;
for (var i = 1; i <= 1000000; i++) {
arrSorted.push(i);
}
matchIndex = binarySearch(arrSorted, 1);
assertEqual(arrSorted[matchIndex], 1);
matchIndex = binarySearch(arrSorted, 1000000);
assertEqual(arrSorted[matchIndex], 1000000);
matchIndex = binarySearch(arrSorted, 49);
assertEqual(arrSorted[matchIndex], 49);
matchIndex = binarySearch(arrSorted, 650598);
assertEqual(arrSorted[matchIndex], 650598);
matchIndex = binarySearch(arrSorted, -3);
assertEqual(matchIndex, -1);
matchIndex = binarySearch(arrSorted, 90000000000);
assertEqual(matchIndex, -1);
},
}
var unitTestsMethods = Object.keys(unitTests);
for (var i = 0; i < unitTestsMethods.length; i++) {
try {
unitTests[unitTestsMethods[i]]();
console.log(unitTestsMethods[i] + ' OK');
}
catch (error) {
if (error) {
console.log(unitTestsMethods[i] + ' failed.\n' + error);
}
}
};
} ());