xref: /aosp_15_r20/external/perfetto/ui/src/base/set_utils_unittest.ts (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1// Copyright (C) 2023 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import {intersect, isSetEqual, union} from './set_utils';
16
17// Just to make the tests easier to read:
18function s<T>(xs: T[]): Set<T> {
19  return new Set<T>(xs);
20}
21
22test('union', () => {
23  expect(union(s([]), s([]))).toEqual(s([]));
24  expect(union(s([1]), s([2]))).toEqual(s([1, 2]));
25  expect(union(s([1]), s([]))).toEqual(s([1]));
26  expect(union(s([]), s([2]))).toEqual(s([2]));
27  expect(union(s([1]), s([1]))).toEqual(s([1]));
28  expect(union(s([1, 2]), s([2, 3]))).toEqual(s([1, 2, 3]));
29});
30
31test('intersect', () => {
32  expect(intersect(s([]), s([]))).toEqual(s([]));
33  expect(intersect(s([1]), s([2]))).toEqual(s([]));
34  expect(intersect(s([1]), s([]))).toEqual(s([]));
35  expect(intersect(s([]), s([2]))).toEqual(s([]));
36  expect(intersect(s([1]), s([1]))).toEqual(s([1]));
37  expect(intersect(s([1, 2]), s([2, 3]))).toEqual(s([2]));
38});
39
40test('isSetEqual', () => {
41  expect(isSetEqual(s([]), s([]))).toEqual(true);
42  expect(isSetEqual(s([1]), s([2]))).toEqual(false);
43  expect(isSetEqual(s([1]), s([]))).toEqual(false);
44  expect(isSetEqual(s([]), s([2]))).toEqual(false);
45  expect(isSetEqual(s([1]), s([1]))).toEqual(true);
46  expect(isSetEqual(s([1, 2]), s([2, 3]))).toEqual(false);
47});
48