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