forked from trekhleb/javascript-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComparator.test.js
More file actions
50 lines (43 loc) · 1.98 KB
/
Comparator.test.js
File metadata and controls
50 lines (43 loc) · 1.98 KB
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
import Comparator from '../Comparator';
describe('Comparator', () => {
it('should compare with default comparator function', () => {
const comparator = new Comparator();
expect(comparator.equal(0, 0)).toBe(true);
expect(comparator.equal(0, 1)).toBe(false);
expect(comparator.equal('a', 'a')).toBe(true);
expect(comparator.lessThan(1, 2)).toBe(true);
expect(comparator.lessThan(-1, 2)).toBe(true);
expect(comparator.lessThan('a', 'b')).toBe(true);
expect(comparator.lessThan('a', 'ab')).toBe(true);
expect(comparator.lessThan(10, 2)).toBe(false);
expect(comparator.lessThanOrEqual(10, 2)).toBe(false);
expect(comparator.lessThanOrEqual(1, 1)).toBe(true);
expect(comparator.lessThanOrEqual(0, 0)).toBe(true);
expect(comparator.greaterThan(0, 0)).toBe(false);
expect(comparator.greaterThan(10, 0)).toBe(true);
expect(comparator.greaterThanOrEqual(10, 0)).toBe(true);
expect(comparator.greaterThanOrEqual(10, 10)).toBe(true);
expect(comparator.greaterThanOrEqual(0, 10)).toBe(false);
});
it('should compare with custom comparator function', () => {
const comparator = new Comparator((a, b) => {
if (a.length === b.length) {
return 0;
}
return a.length < b.length ? -1 : 1;
});
expect(comparator.equal('a', 'b')).toBe(true);
expect(comparator.equal('a', '')).toBe(false);
expect(comparator.lessThan('b', 'aa')).toBe(true);
expect(comparator.greaterThanOrEqual('a', 'aa')).toBe(false);
expect(comparator.greaterThanOrEqual('aa', 'a')).toBe(true);
expect(comparator.greaterThanOrEqual('a', 'a')).toBe(true);
comparator.reverse();
expect(comparator.equal('a', 'b')).toBe(true);
expect(comparator.equal('a', '')).toBe(false);
expect(comparator.lessThan('b', 'aa')).toBe(false);
expect(comparator.greaterThanOrEqual('a', 'aa')).toBe(true);
expect(comparator.greaterThanOrEqual('aa', 'a')).toBe(false);
expect(comparator.greaterThanOrEqual('a', 'a')).toBe(true);
});
});