View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  package org.apache.commons.geometry.core.partitioning;
    18
    19  import java.util.ArrayList;
    20  import java.util.Arrays;
    21  import java.util.Collections;
    22  import java.util.List;
    23  import java.util.stream.Collectors;
    24
    25  import org.apache.commons.geometry.core.partitioning.test.TestLineSegment;
    26  import org.apache.commons.geometry.core.partitioning.test.TestPoint2D;
    27  import org.junit.jupiter.api.Assertions;
    28  import org.junit.jupiter.api.Test;
    29
    30  class BoundaryListTest {
    31
    32      @Test
    33      void testBoundaries() {
    34          // arrange
    35          final List<TestLineSegment> boundaries = new ArrayList<>();
    36          boundaries.add(new TestLineSegment(0, 0, 1, 1));
    37          boundaries.add(new TestLineSegment(1, 1, 0, 2));
    38
    39          // act
    40          final BoundaryList<TestPoint2D, TestLineSegment> list = new BoundaryList<>(boundaries);
    41
    42          // assert
    43          Assertions.assertNotSame(boundaries, list.getBoundaries());
    44          Assertions.assertEquals(boundaries, list.getBoundaries());
    45          Assertions.assertEquals(boundaries, list.boundaryStream().collect(Collectors.toList()));
    46      }
    47
    48      @Test
    49      void testGetBoundaries_listCannotBeModified() {
    50          // arrange
    51          final List<TestLineSegment> boundaries = new ArrayList<>();
    52          boundaries.add(new TestLineSegment(0, 0, 1, 1));
    53
    54          final BoundaryList<TestPoint2D, TestLineSegment> list = new BoundaryList<>(boundaries);
    55          final List<TestLineSegment> items = list.getBoundaries();
    56          final TestLineSegment segment = new TestLineSegment(1, 1, 0, 2);
    57
    58          // act/assert
    59          Assertions.assertThrows(UnsupportedOperationException.class, () -> items.add(segment));
    60      }
    61
    62      @Test
    63      void testCount() {
    64          // act/assert
    65          Assertions.assertEquals(0, new BoundaryList<>(Collections.emptyList()).count());
    66          Assertions.assertEquals(1, new BoundaryList<>(Collections.singletonList(
    67                  new TestLineSegment(0, 0, 1, 1)
    68          )).count());
    69          Assertions.assertEquals(2, new BoundaryList<>(Arrays.asList(
    70                  new TestLineSegment(0, 0, 1, 1),
    71                  new TestLineSegment(1, 1, 0, 2)
    72              )).count());
    73      }
    74
    75      @Test
    76      void testToString() {
    77          // arrange
    78          final BoundaryList<TestPoint2D, TestLineSegment> empty = new BoundaryList<>(Collections.emptyList());
    79          final BoundaryList<TestPoint2D, TestLineSegment> single = new BoundaryList<>(Collections.singletonList(
    80                  new TestLineSegment(0, 0, 1, 1)
    81          ));
    82
    83          // act
    84          Assertions.assertEquals("BoundaryList[count= 0]", empty.toString());
    85          Assertions.assertEquals("BoundaryList[count= 1]", single.toString());
    86      }
    87  }