forked from rocicorp/mono
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset-util.test.ts
More file actions
105 lines (98 loc) · 2.48 KB
/
set-util.test.ts
File metadata and controls
105 lines (98 loc) · 2.48 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
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
import {expect, test} from 'vitest';
import {
difference,
equals,
intersection,
symmetricDifference,
union,
} from './set-utils.js';
test('equals', () => {
const t = <T>(a: Iterable<T>, b: Iterable<T>, expected: boolean) => {
expect(equals(new Set(a), new Set(b))).toBe(expected);
expect(equals(new Set(b), new Set(a))).toBe(expected);
};
t('', '', true);
t('', 'a', false);
t('', 'ab', false);
t('a', 'a', true);
t('a', 'b', false);
t('ab', 'a', false);
t('ab', 'b', false);
t('ab', 'ab', true);
t('ab', 'ba', true);
t('abc', 'abcd', false);
});
test('union', () => {
const t = <T>(...sets: [Iterable<T>, ...Iterable<T>[]]) => {
expect(sets.length).toBeGreaterThan(0);
const expected = new Set(sets.at(-1));
expect(union(...sets.slice(0, -1).map(s => new Set(s)))).toEqual(expected);
};
t('');
t('', '');
t('', '', '', '');
t('a', 'a');
t('ab', 'ab');
t('a', 'b', 'c', 'abc');
t('ab', 'bc', 'cd', 'abcd');
});
test('intersection', () => {
const t = <T>(a: Iterable<T>, b: Iterable<T>, expected: Iterable<T>) => {
expect(intersection(new Set(a), new Set(b))).toEqual(new Set(expected));
};
t('', '', '');
t('a', '', '');
t('', 'a', '');
t('a', 'a', 'a');
t('a', 'b', '');
t('a', 'ab', 'a');
t('ab', 'b', 'b');
t('abc', 'cb', 'bc');
t('ab', 'bc', 'b');
t('abc', 'abc', 'cba');
});
test('difference', () => {
const t = <T>(a: Iterable<T>, b: Iterable<T>, expected: Iterable<T>) => {
expect(difference(new Set(a), new Set(b))).toEqual(new Set(expected));
};
t('', '', '');
t('', 'a', '');
t('', 'ab', '');
t('a', '', 'a');
t('a', 'a', '');
t('a', 'b', 'a');
t('ab', '', 'ab');
t('ab', 'a', 'b');
t('ab', 'b', 'a');
t('ab', 'ab', '');
t('abc', '', 'abc');
t('abc', 'a', 'bc');
t('abc', 'b', 'ac');
t('abc', 'c', 'ab');
t('abc', 'ab', 'c');
t('abc', 'bc', 'a');
t('abc', 'ac', 'b');
t('abc', 'abc', '');
t('abc', 'abcd', '');
});
test('symmetricDifference', () => {
const t = <T>(a: Iterable<T>, b: Iterable<T>, expected: Iterable<T>) => {
expect(symmetricDifference(new Set(a), new Set(b))).toEqual(
new Set(expected),
);
expect(symmetricDifference(new Set(b), new Set(a))).toEqual(
new Set(expected),
);
};
t('', '', '');
t('', 'a', 'a');
t('abc', '', 'abc');
t('abc', 'a', 'bc');
t('abc', 'b', 'ac');
t('abc', 'c', 'ab');
t('abc', 'ab', 'c');
t('abc', 'bc', 'a');
t('abc', 'ac', 'b');
t('abc', 'abc', '');
t('abc', 'abcd', 'd');
});