summaryrefslogtreecommitdiffstats
path: root/toolkit/modules/tests/xpcshell/test_BinarySearch.js
blob: f48b0bccff2388d52194f4894e914610c6aa80a9 (plain)
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
/* 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;
}