forked from mozilla/source-map
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-binary-search.js
More file actions
144 lines (120 loc) · 3.11 KB
/
Copy pathtest-binary-search.js
File metadata and controls
144 lines (120 loc) · 3.11 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* -*- Mode: js; js-indent-level: 2; -*- */
/*
* Copyright 2011 Mozilla Foundation and contributors
* Licensed under the New BSD license. See LICENSE or:
* http://opensource.org/licenses/BSD-3-Clause
*/
const binarySearch = require("../lib/binary-search");
function numberCompare(a, b) {
return a - b;
}
exports["test too high with default (glb) bias"] = function (assert) {
const needle = 30;
const haystack = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20];
assert.doesNotThrow(function () {
binarySearch.search(needle, haystack, numberCompare);
});
assert.equal(
haystack[binarySearch.search(needle, haystack, numberCompare)],
20
);
};
exports["test too low with default (glb) bias"] = function (assert) {
const needle = 1;
const haystack = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20];
assert.doesNotThrow(function () {
binarySearch.search(needle, haystack, numberCompare);
});
assert.equal(binarySearch.search(needle, haystack, numberCompare), -1);
};
exports["test too high with lub bias"] = function (assert) {
const needle = 30;
const haystack = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20];
assert.doesNotThrow(function () {
binarySearch.search(needle, haystack, numberCompare);
});
assert.equal(
binarySearch.search(
needle,
haystack,
numberCompare,
binarySearch.LEAST_UPPER_BOUND
),
-1
);
};
exports["test too low with lub bias"] = function (assert) {
const needle = 1;
const haystack = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20];
assert.doesNotThrow(function () {
binarySearch.search(needle, haystack, numberCompare);
});
assert.equal(
haystack[
binarySearch.search(
needle,
haystack,
numberCompare,
binarySearch.LEAST_UPPER_BOUND
)
],
2
);
};
exports["test exact search"] = function (assert) {
const needle = 4;
const haystack = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20];
assert.equal(
haystack[binarySearch.search(needle, haystack, numberCompare)],
4
);
};
exports["test fuzzy search with default (glb) bias"] = function (assert) {
const needle = 19;
const haystack = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20];
assert.equal(
haystack[binarySearch.search(needle, haystack, numberCompare)],
18
);
};
exports["test fuzzy search with lub bias"] = function (assert) {
const needle = 19;
const haystack = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20];
assert.equal(
haystack[
binarySearch.search(
needle,
haystack,
numberCompare,
binarySearch.LEAST_UPPER_BOUND
)
],
20
);
};
exports["test multiple matches"] = function (assert) {
const needle = 5;
const haystack = [1, 1, 2, 5, 5, 5, 13, 21];
assert.equal(
binarySearch.search(
needle,
haystack,
numberCompare,
binarySearch.LEAST_UPPER_BOUND
),
3
);
};
exports["test multiple matches at the beginning"] = function (assert) {
const needle = 1;
const haystack = [1, 1, 2, 5, 5, 5, 13, 21];
assert.equal(
binarySearch.search(
needle,
haystack,
numberCompare,
binarySearch.LEAST_UPPER_BOUND
),
0
);
};