Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80620 views
1
/* -*- Mode: js; js-indent-level: 2; -*- */
2
/*
3
* Copyright 2011 Mozilla Foundation and contributors
4
* Licensed under the New BSD license. See LICENSE or:
5
* http://opensource.org/licenses/BSD-3-Clause
6
*/
7
if (typeof define !== 'function') {
8
var define = require('amdefine')(module, require);
9
}
10
define(function (require, exports, module) {
11
12
var binarySearch = require('../../lib/source-map/binary-search');
13
14
function numberCompare(a, b) {
15
return a - b;
16
}
17
18
exports['test too high with lub bias'] = function (assert, util) {
19
var needle = 30;
20
var haystack = [2,4,6,8,10,12,14,16,18,20];
21
22
assert.doesNotThrow(function () {
23
binarySearch.search(needle, haystack, numberCompare);
24
});
25
26
assert.equal(binarySearch.search(needle, haystack, numberCompare), -1);
27
};
28
29
exports['test too low with lub bias'] = function (assert, util) {
30
var needle = 1;
31
var haystack = [2,4,6,8,10,12,14,16,18,20];
32
33
assert.doesNotThrow(function () {
34
binarySearch.search(needle, haystack, numberCompare, true);
35
});
36
37
assert.equal(haystack[binarySearch.search(needle, haystack, numberCompare)], 2);
38
};
39
40
exports['test exact search with lub bias'] = function (assert, util) {
41
var needle = 4;
42
var haystack = [2,4,6,8,10,12,14,16,18,20];
43
44
assert.equal(haystack[binarySearch.search(needle, haystack, numberCompare)], 4);
45
};
46
47
exports['test fuzzy search with lub bias'] = function (assert, util) {
48
var needle = 19;
49
var haystack = [2,4,6,8,10,12,14,16,18,20];
50
51
assert.equal(haystack[binarySearch.search(needle, haystack, numberCompare)], 20);
52
};
53
54
exports['test too high with glb bias'] = function (assert, util) {
55
var needle = 30;
56
var haystack = [2,4,6,8,10,12,14,16,18,20];
57
58
assert.doesNotThrow(function () {
59
binarySearch.search(needle, haystack, numberCompare);
60
});
61
62
assert.equal(haystack[binarySearch.search(needle, haystack, numberCompare,
63
binarySearch.GREATEST_LOWER_BOUND)], 20);
64
};
65
66
exports['test too low with glb bias'] = function (assert, util) {
67
var needle = 1;
68
var haystack = [2,4,6,8,10,12,14,16,18,20];
69
70
assert.doesNotThrow(function () {
71
binarySearch.search(needle, haystack, numberCompare,
72
binarySearch.GREATEST_LOWER_BOUND);
73
});
74
75
assert.equal(binarySearch.search(needle, haystack, numberCompare,
76
binarySearch.GREATEST_LOWER_BOUND), -1);
77
};
78
79
exports['test exact search with glb bias'] = function (assert, util) {
80
var needle = 4;
81
var haystack = [2,4,6,8,10,12,14,16,18,20];
82
83
assert.equal(haystack[binarySearch.search(needle, haystack, numberCompare,
84
binarySearch.GREATEST_LOWER_BOUND)], 4);
85
};
86
87
exports['test fuzzy search with glb bias'] = function (assert, util) {
88
var needle = 19;
89
var haystack = [2,4,6,8,10,12,14,16,18,20];
90
91
assert.equal(haystack[binarySearch.search(needle, haystack, numberCompare,
92
binarySearch.GREATEST_LOWER_BOUND)], 18);
93
};
94
});
95
96