-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
125 lines (97 loc) · 2.87 KB
/
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
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
const test = require('ava');
const {
comparatorAscending,
comparatorDescending,
pickMiddleValue
} = require('./utils');
const qs = require('.');
test('Should sort an array of even length', t => {
const value = [7, 1, 10, 6, 3, 2, 8, 5, 9, 4];
const expected = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
t.deepEqual(qs(value), expected);
});
test('Should sort an array of odd length', t => {
const value = [5, 1, 7, 8, 2, 3, 6, 9, 4];
const expected = [1, 2, 3, 4, 5, 6, 7, 8, 9];
t.deepEqual(qs(value), expected);
});
test('Should work with an already sorted array', t => {
const value = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const expected = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
t.deepEqual(qs(value), expected);
});
test('Should work with if the array values are all the same', t => {
const value = [4, 4, 4, 4, 4, 4, 4, 4];
const expected = [4, 4, 4, 4, 4, 4, 4, 4];
t.deepEqual(qs(value), expected);
});
test('Should work with an array of one value', t => {
const value = [4];
const expected = [4];
t.deepEqual(qs(value), expected);
});
test('Should work with an empty array', t => {
const value = [];
const expected = [];
t.deepEqual(qs(value), expected);
});
test('Should not sort the array in place', t => {
const value = [1, 2, 3];
t.false(qs(value) === value);
});
test('Should work with a custom comparator', t => {
const value = [
{order: 3},
{order: 1},
{order: 4},
{order: 2}
];
const expected = [
{order: 1},
{order: 2},
{order: 3},
{order: 4}
];
const comparator = function (a, b) {
if (a.order < b.order) {
return -1;
}
if (a.order > b.order) {
return 1;
}
return 0;
};
t.deepEqual(qs(value, comparator), expected);
});
test('Should work with a custom pivot function', t => {
const value = [5, 2, 4, 1, 4];
const expected = [1, 2, 4, 4, 5];
const getPivot = function (array) {
return array[0];
};
t.deepEqual(qs(value, undefined, getPivot), expected);
});
test('Ascending comparator should return -1 when first value is smaller', t => {
t.true(comparatorAscending(1, 2) === -1);
});
test('Ascending comparator should return 1 when first value is greater', t => {
t.true(comparatorAscending(2, 1) === 1);
});
test('Ascending comparator should return 0 when values are equal', t => {
t.true(comparatorAscending(1, 1) === 0);
});
test('Descending comparator should return 1 when first value is smaller', t => {
t.true(comparatorDescending(2, 1) === -1);
});
test('Descending comparator should return -1 when first value is greater', t => {
t.true(comparatorDescending(1, 2) === 1);
});
test('Descending comparator should return 0 when values are equal', t => {
t.true(comparatorDescending(1, 1) === 0);
});
test('Should return the middle value for odd arrays', t => {
t.true(pickMiddleValue([1, 2, 3, 4, 5]) === 3);
});
test('Should return the middle-right value for even arrays', t => {
t.true(pickMiddleValue([1, 2, 3, 4, 5, 6]) === 4);
});