This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Comparator.test.js
50 lines (43 loc) · 2.02 KB
/
Comparator.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
import Comparator from '../Comparator';
describe('Comparator', () => {
it('should compare with default comparator function', () => {
const comparator = new Comparator();
expect(comparator.equal(0, 0)).toBeTruthy();
expect(comparator.equal(0, 1)).toBeFalsy();
expect(comparator.equal('a', 'a')).toBeTruthy();
expect(comparator.lessThan(1, 2)).toBeTruthy();
expect(comparator.lessThan(-1, 2)).toBeTruthy();
expect(comparator.lessThan('a', 'b')).toBeTruthy();
expect(comparator.lessThan('a', 'ab')).toBeTruthy();
expect(comparator.lessThan(10, 2)).toBeFalsy();
expect(comparator.lessThanOrEqual(10, 2)).toBeFalsy();
expect(comparator.lessThanOrEqual(1, 1)).toBeTruthy();
expect(comparator.lessThanOrEqual(0, 0)).toBeTruthy();
expect(comparator.greaterThan(0, 0)).toBeFalsy();
expect(comparator.greaterThan(10, 0)).toBeTruthy();
expect(comparator.greaterThanOrEqual(10, 0)).toBeTruthy();
expect(comparator.greaterThanOrEqual(10, 10)).toBeTruthy();
expect(comparator.greaterThanOrEqual(0, 10)).toBeFalsy();
});
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')).toBeTruthy();
expect(comparator.equal('a', '')).toBeFalsy();
expect(comparator.lessThan('b', 'aa')).toBeTruthy();
expect(comparator.greaterThanOrEqual('a', 'aa')).toBeFalsy();
expect(comparator.greaterThanOrEqual('aa', 'a')).toBeTruthy();
expect(comparator.greaterThanOrEqual('a', 'a')).toBeTruthy();
comparator.reverse();
expect(comparator.equal('a', 'b')).toBeTruthy();
expect(comparator.equal('a', '')).toBeFalsy();
expect(comparator.lessThan('b', 'aa')).toBeFalsy();
expect(comparator.greaterThanOrEqual('a', 'aa')).toBeTruthy();
expect(comparator.greaterThanOrEqual('aa', 'a')).toBeFalsy();
expect(comparator.greaterThanOrEqual('a', 'a')).toBeTruthy();
});
});