-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary-search-iterative.test.js
82 lines (66 loc) · 2.7 KB
/
binary-search-iterative.test.js
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
// npm test -- binary-search-iterative.test
const { binarySearchIterative } = require("./binary-search-iterative");
test("binarySearchIterative() : Has been found", () => {
const array = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90];
const x = 80;
const input = binarySearchIterative(array, x, []);
const output = [40, 70, 80]; // 80 has been found
expect(input).toEqual(output);
expect(output[output.length - 1] === x ).toEqual(true) // true result
});
test("binarySearchIterative() : Has not been found", () => {
const array = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90];
const x = 85;
const input = binarySearchIterative(array, x, []);
const output = [40, 70, 80, 90]; // 85 has not been found
expect(input).toEqual(output);
expect(output[output.length - 1] === x ).toEqual(false) // true false
});
test("binarySearchIterative() : Empty array", () => {
const array = [];
const x = 999;
const input = binarySearchIterative(array, x, []);
const output = []; // 999 has not been found
expect(input).toEqual(output);
expect(output[output.length - 1] === x ).toEqual(false) // false result
});
test("binarySearchIterative() : One item", () => {
const array = [1];
const x = 999;
const input = binarySearchIterative(array, x, []);
const output = [1]; // 999 has not been found
expect(input).toEqual(output);
expect(output[output.length - 1] === x ).toEqual(false) // false result
});
test("binarySearchIterative() : One item - Found", () => {
const array = [1];
const x = 1;
const input = binarySearchIterative(array, x, []);
const output = [1]; // 1 has been found
expect(input).toEqual(output);
expect(output[output.length - 1] === x ).toEqual(true) // true result
});
test("binarySearchIterative() : Two items", () => {
const array = [1, 2];
const x = 999;
const input = binarySearchIterative(array, x, []);
const output = [1, 2]; // 999 has not been found
expect(input).toEqual(output);
expect(output[output.length - 1] === x ).toEqual(false) // false result
});
test("binarySearchIterative() : Two items - found item #1", () => {
const array = [1, 2];
const x = 1;
const input = binarySearchIterative(array, x, []);
const output = [1]; // 1 has been found
expect(input).toEqual(output);
expect(output[output.length - 1] === x ).toEqual(true) // true result
});
test("binarySearchIterative() : Two items - found item #2", () => {
const array = [1, 2];
const x = 2;
const input = binarySearchIterative(array, x, []);
const output = [1, 2]; // 1 has been found
expect(input).toEqual(output);
expect(output[output.length - 1] === x ).toEqual(true) // true result
});