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.euclidean.internal;
    18
    19  import org.apache.commons.geometry.core.GeometryTestUtils;
    20  import org.apache.commons.geometry.euclidean.oned.Vector1D;
    21  import org.apache.commons.geometry.euclidean.threed.Vector3D;
    22  import org.junit.jupiter.api.Assertions;
    23  import org.junit.jupiter.api.Test;
    24
    25  class VectorsTest {
    26
    27      private static final double EPS = Math.ulp(1d);
    28
    29      @Test
    30      void testIsRealNonZero() {
    31          // act/assert
    32          Assertions.assertTrue(Vectors.isRealNonZero(1e-20));
    33          Assertions.assertTrue(Vectors.isRealNonZero(1e20));
    34          Assertions.assertTrue(Vectors.isRealNonZero(-1e-20));
    35          Assertions.assertTrue(Vectors.isRealNonZero(-1e20));
    36
    37          Assertions.assertFalse(Vectors.isRealNonZero(0.0));
    38          Assertions.assertFalse(Vectors.isRealNonZero(-0.0));
    39          Assertions.assertFalse(Vectors.isRealNonZero(Double.NaN));
    40          Assertions.assertFalse(Vectors.isRealNonZero(Double.POSITIVE_INFINITY));
    41          Assertions.assertFalse(Vectors.isRealNonZero(Double.NEGATIVE_INFINITY));
    42      }
    43
    44      @Test
    45      void testCheckedNorm_normArg() {
    46          // act/assert
    47          Assertions.assertEquals(1.0, Vectors.checkedNorm(1.0), EPS);
    48          Assertions.assertEquals(23.12, Vectors.checkedNorm(23.12), EPS);
    49          Assertions.assertEquals(2e-12, Vectors.checkedNorm(2e-12), EPS);
    50
    51          Assertions.assertEquals(-1.0, Vectors.checkedNorm(-1.0), EPS);
    52          Assertions.assertEquals(-23.12, Vectors.checkedNorm(-23.12), EPS);
    53          Assertions.assertEquals(-2e-12, Vectors.checkedNorm(-2e-12), EPS);
    54
    55          GeometryTestUtils.assertThrowsWithMessage(() -> Vectors.checkedNorm(0.0),
    56                  IllegalArgumentException.class, "Illegal norm: 0.0");
    57          GeometryTestUtils.assertThrowsWithMessage(() -> Vectors.checkedNorm(Double.NaN),
    58                  IllegalArgumentException.class, "Illegal norm: NaN");
    59          GeometryTestUtils.assertThrowsWithMessage(() -> Vectors.checkedNorm(Double.POSITIVE_INFINITY),
    60                  IllegalArgumentException.class, "Illegal norm: Infinity");
    61          GeometryTestUtils.assertThrowsWithMessage(() -> Vectors.checkedNorm(Double.NEGATIVE_INFINITY),
    62                  IllegalArgumentException.class, "Illegal norm: -Infinity");
    63      }
    64
    65      @Test
    66      void testCheckedNorm_vectorArg() {
    67          // act/assert
    68          Assertions.assertEquals(1.0, Vectors.checkedNorm(Vector1D.of(1.0)), EPS);
    69          Assertions.assertEquals(23.12, Vectors.checkedNorm(Vector1D.of(23.12)), EPS);
    70          Assertions.assertEquals(2e-12, Vectors.checkedNorm(Vector1D.of(2e-12)), EPS);
    71
    72          Assertions.assertEquals(1.0, Vectors.checkedNorm(Vector1D.of(-1.0)), EPS);
    73          Assertions.assertEquals(23.12, Vectors.checkedNorm(Vector1D.of(-23.12)), EPS);
    74          Assertions.assertEquals(2e-12, Vectors.checkedNorm(Vector1D.of(-2e-12)), EPS);
    75
    76          GeometryTestUtils.assertThrowsWithMessage(() -> Vectors.checkedNorm(Vector3D.ZERO),
    77                  IllegalArgumentException.class, "Illegal norm: 0.0");
    78          GeometryTestUtils.assertThrowsWithMessage(() -> Vectors.checkedNorm(Vector3D.NaN),
    79                  IllegalArgumentException.class, "Illegal norm: NaN");
    80          GeometryTestUtils.assertThrowsWithMessage(() -> Vectors.checkedNorm(Vector3D.POSITIVE_INFINITY),
    81                  IllegalArgumentException.class, "Illegal norm: Infinity");
    82          GeometryTestUtils.assertThrowsWithMessage(() -> Vectors.checkedNorm(Vector3D.NEGATIVE_INFINITY),
    83                  IllegalArgumentException.class, "Illegal norm: Infinity");
    84      }
    85
    86      @Test
    87      void testNorm_oneD() {
    88          // act/assert
    89          Assertions.assertEquals(0.0, Vectors.norm(0.0), EPS);
    90
    91          Assertions.assertEquals(2.0, Vectors.norm(-2.0), EPS);
    92          Assertions.assertEquals(1.0, Vectors.norm(-1.0), EPS);
    93
    94          Assertions.assertEquals(1.0, Vectors.norm(1.0), EPS);
    95          Assertions.assertEquals(2.0, Vectors.norm(2.0), EPS);
    96      }
    97
    98      @Test
    99      void testNorm_twoD() {
    100         // act/assert
    101         Assertions.assertEquals(0.0, Vectors.norm(0.0, 0.0), EPS);
    102
    103         Assertions.assertEquals(Math.sqrt(5.0), Vectors.norm(1.0, 2.0), EPS);
    104         Assertions.assertEquals(5.0, Vectors.norm(3.0, -4.0), EPS);
    105         Assertions.assertEquals(Math.sqrt(61.0), Vectors.norm(-5.0, 6.0), EPS);
    106         Assertions.assertEquals(Math.sqrt(130.0), Vectors.norm(-7.0, -9.0), EPS);
    107     }
    108
    109     @Test
    110     void testNorm_threeD() {
    111         // act/assert
    112         Assertions.assertEquals(0.0, Vectors.norm(0.0, 0.0, 0.0), EPS);
    113
    114         Assertions.assertEquals(Math.sqrt(14.0), Vectors.norm(1.0, 2.0, 3.0), EPS);
    115         Assertions.assertEquals(Math.sqrt(77.0), Vectors.norm(4.0, 5.0, -6.0), EPS);
    116         Assertions.assertEquals(Math.sqrt(194.0), Vectors.norm(7.0, -8.0, 9.0), EPS);
    117         Assertions.assertEquals(Math.sqrt(365.0), Vectors.norm(10.0, -11.0, -12.0), EPS);
    118         Assertions.assertEquals(Math.sqrt(590.0), Vectors.norm(-13.0, 14.0, 15.0), EPS);
    119         Assertions.assertEquals(Math.sqrt(869.0), Vectors.norm(-16.0, 17.0, -18.0), EPS);
    120         Assertions.assertEquals(Math.sqrt(1202.0), Vectors.norm(-19.0, -20.0, 21.0), EPS);
    121         Assertions.assertEquals(Math.sqrt(1589.0), Vectors.norm(-22.0, -23.0, -24.0), EPS);
    122     }
    123
    124     @Test
    125     void testNormSq_oneD() {
    126         // act/assert
    127         Assertions.assertEquals(0.0, Vectors.normSq(0.0), EPS);
    128
    129         Assertions.assertEquals(9.0, Vectors.normSq(-3.0), EPS);
    130         Assertions.assertEquals(1.0, Vectors.normSq(-1.0), EPS);
    131
    132         Assertions.assertEquals(1.0, Vectors.normSq(1.0), EPS);
    133         Assertions.assertEquals(9.0, Vectors.normSq(3.0), EPS);
    134     }
    135
    136     @Test
    137     void testNormSq_twoD() {
    138         // act/assert
    139         Assertions.assertEquals(0.0, Vectors.normSq(0.0, 0.0), EPS);
    140
    141         Assertions.assertEquals(5.0, Vectors.normSq(1.0, 2.0), EPS);
    142         Assertions.assertEquals(25.0, Vectors.normSq(3.0, -4.0), EPS);
    143         Assertions.assertEquals(61.0, Vectors.normSq(-5.0, 6.0), EPS);
    144         Assertions.assertEquals(130.0, Vectors.normSq(-7.0, -9.0), EPS);
    145     }
    146
    147     @Test
    148     void testNormSq_threeD() {
    149         // act/assert
    150         Assertions.assertEquals(0.0, Vectors.normSq(0.0, 0.0, 0.0), EPS);
    151
    152         Assertions.assertEquals(14.0, Vectors.normSq(1.0, 2.0, 3.0), EPS);
    153         Assertions.assertEquals(77.0, Vectors.normSq(4.0, 5.0, -6.0), EPS);
    154         Assertions.assertEquals(194.0, Vectors.normSq(7.0, -8.0, 9.0), EPS);
    155         Assertions.assertEquals(365.0, Vectors.normSq(10.0, -11.0, -12.0), EPS);
    156         Assertions.assertEquals(590.0, Vectors.normSq(-13.0, 14.0, 15.0), EPS);
    157         Assertions.assertEquals(869.0, Vectors.normSq(-16.0, 17.0, -18.0), EPS);
    158         Assertions.assertEquals(1202.0, Vectors.normSq(-19.0, -20.0, 21.0), EPS);
    159         Assertions.assertEquals(1589.0, Vectors.normSq(-22.0, -23.0, -24.0), EPS);
    160     }
    161 }