-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.js
81 lines (70 loc) · 2.83 KB
/
tests.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
const test = require('tape');
const foo = require('./index');
// # Main tests
test('Method should return boolean value', (t) => {
t.equal(typeof foo([]), 'boolean', 'typeof result');
t.end();
});
test('Method should return right values in trivial cases', (t) => {
t.equal(foo([]), true, 'empty array');
t.equal(foo([1]), true, 'single item array');
t.equal(foo([2, 1]), true, 'two items array');
t.end();
});
test('Method should return true in certain cases without passed comparator', (t) => {
t.equal(foo([6, 2, 2]), true, '[6, 2, 2]');
t.equal(foo([2, 2, 2, 2]), true, '[2, 2, 2, 2]');
t.equal(foo([1, 5, 3, 7]), true, '[1, 5, 3, 7]');
t.equal(foo([1, 5, 3, 3, 7]), true, '[1, 5, 3, 3, 7]');
t.equal(foo([2, 2, 2, 5, 4]), true, '[2, 2, 2, 5, 4]');
t.equal(foo([1, 5, 3, 3, 2, 6]), true, '[1, 5, 3, 3, 2, 6]');
const array = [];
for (let i = 1; i <= 100000; i++) {
array.push(10);
}
array[0] = 1000;
t.equal(foo(array), true, 'over 99999');
t.end();
});
test('Method should return false in certain cases without passed comparator', (t) => {
t.equal(foo([5, 7, 3]), false, '[5, 7, 3]');
t.equal(foo([1, 5, 5, 3, 7]), false, '[1, 5, 5, 3, 7]');
t.equal(foo([1, 5, 3, 3, 4]), false, '[1, 5, 3, 3, 4]');
t.end();
});
test('Method should return right values when array should be sorted in DESCENDING order', (t) => {
t.equal(foo([], null, true), true, '[]');
t.equal(foo([1], null, true), true, '[1]');
t.equal(foo([5, 7], null, true), true, '[5, 7]');
t.equal(foo([7, 5], null, true), true, '[7, 5]');
t.equal(foo([3, 3, 3], null, true), true, '[3, 3, 3]');
t.equal(foo([7, 5, 3], null, true), true, '[7, 5, 3]');
t.equal(foo([7, 3, 5, 1], null, true), true, '[7, 3, 5, 1]');
t.equal(foo([7, 3, 3, 5, 1], null, true), false, '[7, 3, 3, 5, 1]');
t.end();
});
test('Method should work with custom comparator', (t) => {
const comp = (left, right) => {
if (left.length < right.length) return -1;
if (left.length > right.length) return 1;
return 0;
};
t.equal(foo(['ass', 'titties', 'ass n titties'], comp), true, `string length ['ass', 'titties', 'ass n titties']`);
t.equal(foo(['ass', 'ass n titties', 'titties'], comp), true, `string length ['ass', 'ass n titties', 'titties']`);
t.equal(foo(['ass', 'titties', 'ass', 'n', 'titties'], comp), false, `string length ['ass', 'titties', 'ass', 'n', 'titties']`);
t.end();
});
// # Default Comparator
test('defaultComparator', (t) => {
t.equals(foo.defaultComparator(5, 6), -1, 'place lower first');
t.equals(foo.defaultComparator(50, 10), 1, 'place bigger last');
t.equals(foo.defaultComparator(10, 10), 0, 'do not move equals');
t.end();
});
// # Swapper Function
test('swapArrayElements', (t) => {
const array = [];
t.equals(foo.swapArrayElements(array), array, 'return same array');
t.deepEqual(foo.swapArrayElements([1, 4, 0, 1], 2, 3), [1, 4, 1, 0], 'swap elements');
t.end();
});