View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://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   */
15  package org.htmlunit.util.geometry;
16  
17  import static org.junit.jupiter.api.Assertions.assertEquals;
18  import static org.junit.jupiter.api.Assertions.assertFalse;
19  import static org.junit.jupiter.api.Assertions.assertTrue;
20  
21  import org.junit.jupiter.api.Test;
22  
23  /**
24   * Tests for {@link Rectangle2D}.
25   *
26   * @author Ronald Brill
27   */
28  public class Rectangle2DTest {
29  
30      /**
31       * @throws Exception if the test fails
32       */
33      @Test
34      public void props() throws Exception {
35          assertEquals(4, new Rectangle2D(4, 7, 5, 9).getLeft(), 0.00001);
36          assertEquals(7, new Rectangle2D(4, 7, 5, 9).getBottom(), 0.00001);
37          assertEquals(4, new Rectangle2D(5, 7, 4, 9).getLeft(), 0.00001);
38          assertEquals(7, new Rectangle2D(5, 7, 4, 9).getBottom(), 0.00001);
39          assertEquals(7, new Rectangle2D(5, 9, 4, 7).getBottom(), 0.00001);
40      }
41  
42      /**
43       * @throws Exception if the test fails
44       */
45      @Test
46      public void contains() throws Exception {
47          assertTrue(new Rectangle2D(4, 7, 5, 9).contains(4, 7));
48          assertTrue(new Rectangle2D(4, 7, 5, 9).contains(4.5, 7));
49          assertTrue(new Rectangle2D(4, 7, 5, 9).contains(4.5, 8));
50          assertTrue(new Rectangle2D(4, 7, 5, 9).contains(4.5, 9));
51  
52          assertTrue(new Rectangle2D(4, 7, 5, 9).contains(5, 7));
53  
54          assertFalse(new Rectangle2D(4, 7, 5, 9).contains(5.001, 7));
55          assertFalse(new Rectangle2D(4, 7, 5, 9).contains(5, 9.001));
56          assertFalse(new Rectangle2D(4, 7, 5, 9).contains(3.999, 6));
57          assertFalse(new Rectangle2D(4, 7, 5, 9).contains(4.5, 6.999));
58      }
59  
60      /**
61       * @throws Exception if the test fails
62       */
63      @Test
64      public void extend() throws Exception {
65          final Rectangle2D testRect = new Rectangle2D(4, 7, 5, 9);
66          assertEquals(4, testRect.getLeft(), 0.00001);
67          assertEquals(7, testRect.getBottom(), 0.00001);
68  
69          testRect.extend(20, 17);
70          assertEquals(4, testRect.getLeft(), 0.00001);
71          assertEquals(7, testRect.getBottom(), 0.00001);
72  
73          testRect.extend(-2, 4);
74          assertEquals(-2, testRect.getLeft(), 0.00001);
75          assertEquals(4, testRect.getBottom(), 0.00001);
76  
77          testRect.extend(4.5, 13.2);
78          assertEquals(-2, testRect.getLeft(), 0.00001);
79          assertEquals(4, testRect.getBottom(), 0.00001);
80  
81          testRect.extend(-2, 4);
82          assertEquals(-2, testRect.getLeft(), 0.00001);
83          assertEquals(4, testRect.getBottom(), 0.00001);
84      }
85  }