summaryrefslogtreecommitdiffstats
path: root/toolkit/modules/tests/xpcshell/test_BinarySearch.js
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commit5f8de423f190bbb79a62f804151bc24824fa32d8 (patch)
tree10027f336435511475e392454359edea8e25895d /toolkit/modules/tests/xpcshell/test_BinarySearch.js
parent49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff)
downloadUXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip
Add m-esr52 at 52.6.0
Diffstat (limited to 'toolkit/modules/tests/xpcshell/test_BinarySearch.js')
-rw-r--r--toolkit/modules/tests/xpcshell/test_BinarySearch.js81
1 files changed, 81 insertions, 0 deletions
diff --git a/toolkit/modules/tests/xpcshell/test_BinarySearch.js b/toolkit/modules/tests/xpcshell/test_BinarySearch.js
new file mode 100644
index 000000000..f48b0bccf
--- /dev/null
+++ b/toolkit/modules/tests/xpcshell/test_BinarySearch.js
@@ -0,0 +1,81 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+Components.utils.import("resource://gre/modules/BinarySearch.jsm");
+
+function run_test() {
+ // empty array
+ ok([], 1, false, 0);
+
+ // one-element array
+ ok([2], 2, true, 0);
+ ok([2], 1, false, 0);
+ ok([2], 3, false, 1);
+
+ // two-element array
+ ok([2, 4], 2, true, 0);
+ ok([2, 4], 4, true, 1);
+ ok([2, 4], 1, false, 0);
+ ok([2, 4], 3, false, 1);
+ ok([2, 4], 5, false, 2);
+
+ // three-element array
+ ok([2, 4, 6], 2, true, 0);
+ ok([2, 4, 6], 4, true, 1);
+ ok([2, 4, 6], 6, true, 2);
+ ok([2, 4, 6], 1, false, 0);
+ ok([2, 4, 6], 3, false, 1);
+ ok([2, 4, 6], 5, false, 2);
+ ok([2, 4, 6], 7, false, 3);
+
+ // duplicates
+ ok([2, 2], 2, true, 0);
+ ok([2, 2], 1, false, 0);
+ ok([2, 2], 3, false, 2);
+
+ // duplicates on the left
+ ok([2, 2, 4], 2, true, 1);
+ ok([2, 2, 4], 4, true, 2);
+ ok([2, 2, 4], 1, false, 0);
+ ok([2, 2, 4], 3, false, 2);
+ ok([2, 2, 4], 5, false, 3);
+
+ // duplicates on the right
+ ok([2, 4, 4], 2, true, 0);
+ ok([2, 4, 4], 4, true, 1);
+ ok([2, 4, 4], 1, false, 0);
+ ok([2, 4, 4], 3, false, 1);
+ ok([2, 4, 4], 5, false, 3);
+
+ // duplicates in the middle
+ ok([2, 4, 4, 6], 2, true, 0);
+ ok([2, 4, 4, 6], 4, true, 1);
+ ok([2, 4, 4, 6], 6, true, 3);
+ ok([2, 4, 4, 6], 1, false, 0);
+ ok([2, 4, 4, 6], 3, false, 1);
+ ok([2, 4, 4, 6], 5, false, 3);
+ ok([2, 4, 4, 6], 7, false, 4);
+
+ // duplicates all around
+ ok([2, 2, 4, 4, 6, 6], 2, true, 0);
+ ok([2, 2, 4, 4, 6, 6], 4, true, 2);
+ ok([2, 2, 4, 4, 6, 6], 6, true, 4);
+ ok([2, 2, 4, 4, 6, 6], 1, false, 0);
+ ok([2, 2, 4, 4, 6, 6], 3, false, 2);
+ ok([2, 2, 4, 4, 6, 6], 5, false, 4);
+ ok([2, 2, 4, 4, 6, 6], 7, false, 6);
+}
+
+function ok(array, target, expectedFound, expectedIdx) {
+ let [found, idx] = BinarySearch.search(cmp, array, target);
+ do_check_eq(found, expectedFound);
+ do_check_eq(idx, expectedIdx);
+
+ idx = expectedFound ? expectedIdx : -1;
+ do_check_eq(BinarySearch.indexOf(cmp, array, target), idx);
+ do_check_eq(BinarySearch.insertionIndexOf(cmp, array, target), expectedIdx);
+}
+
+function cmp(num1, num2) {
+ return num1 - num2;
+}