xref: /aosp_15_r20/external/perfetto/ui/src/base/geom_unittest.ts (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1// Copyright (C) 2024 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 {Vector2D, Rect2D, Bounds2D} from './geom';
16
17describe('Vector2D', () => {
18  test('add', () => {
19    const vector1 = new Vector2D({x: 1, y: 2});
20    const vector2 = new Vector2D({x: 3, y: 4});
21    const result = vector1.add(vector2);
22    expect(result.x).toBe(4);
23    expect(result.y).toBe(6);
24  });
25
26  test('sub', () => {
27    const vector1 = new Vector2D({x: 5, y: 7});
28    const vector2 = new Vector2D({x: 2, y: 3});
29    const result = vector1.sub(vector2);
30    expect(result.x).toBe(3);
31    expect(result.y).toBe(4);
32  });
33
34  test('scale', () => {
35    const vector = new Vector2D({x: 2, y: 3});
36    const result = vector.scale(2);
37    expect(result.x).toBe(4);
38    expect(result.y).toBe(6);
39  });
40});
41
42describe('Rect2D', () => {
43  test('intersect', () => {
44    const a = new Rect2D({left: 1, top: 1, right: 4, bottom: 4});
45    const b = {left: 2, top: 2, right: 5, bottom: 5};
46    const result = a.intersect(b);
47    expect(result).toMatchObject({left: 2, top: 2, right: 4, bottom: 4});
48    // Note: Non-overlapping rects are UB and thus not tested
49    // TODO(stevegolton): Work out what to do here.
50  });
51
52  test('expand', () => {
53    const rect = new Rect2D({left: 1, top: 1, right: 3, bottom: 3});
54    const result = rect.expand(1);
55    expect(result).toMatchObject({left: 0, top: 0, right: 4, bottom: 4});
56  });
57
58  test('reframe', () => {
59    const rect = new Rect2D({left: 2, top: 2, right: 5, bottom: 5});
60    const result = rect.reframe({x: 1, y: 1});
61    expect(result).toMatchObject({left: 1, top: 1, right: 4, bottom: 4});
62  });
63
64  test('size', () => {
65    const rect = new Rect2D({left: 1, top: 1, right: 4, bottom: 3});
66    expect(rect).toMatchObject({width: 3, height: 2});
67  });
68
69  it('translate', () => {
70    const rect = new Rect2D({left: 2, top: 2, right: 5, bottom: 5});
71    const result = rect.translate({x: 3, y: 4});
72    expect(result).toMatchObject({left: 5, top: 6, right: 8, bottom: 9});
73  });
74
75  it('contains', () => {
76    const outerRect = new Rect2D({left: 0, top: 0, right: 10, bottom: 10});
77    const innerRect: Bounds2D = {left: 2, top: 2, right: 8, bottom: 8};
78    expect(outerRect.contains(innerRect)).toBe(true);
79
80    const nonContainedRect: Bounds2D = {left: 2, top: 2, right: 12, bottom: 8};
81    expect(outerRect.contains(nonContainedRect)).toBe(false);
82  });
83});
84