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.numbers.angle;
18  
19  import org.junit.jupiter.api.Assertions;
20  import org.junit.jupiter.api.Test;
21  
22  /**
23   * Tests for examples contained in the user guide.
24   */
25  class UserGuideTest {
26  
27      @Test
28      void testAngle1() {
29          Angle.Deg a = Angle.Turn.of(0.5).toDeg();
30          Angle.Rad b = a.toRad();
31          Angle.Turn c = b.toTurn();
32          Assertions.assertEquals(0.5, c.getAsDouble());
33      }
34  
35      @Test
36      void testAngleNormalizer1() {
37          double angle = Angle.Deg.normalizer(-180).applyAsDouble(270);
38          Assertions.assertEquals(-90.0, angle);
39      }
40  
41      @Test
42      void testReduce1() {
43          Reduce reduce = new Reduce(0, 24);
44          double hours1 = reduce.applyAsDouble(173.5);
45          double hours2 = reduce.applyAsDouble(23.5);
46          double hours3 = reduce.applyAsDouble(-10);
47          Assertions.assertEquals(5.5, hours1);
48          Assertions.assertEquals(23.5, hours2);
49          Assertions.assertEquals(14.0, hours3);
50      }
51  
52      @Test
53      void testCosAngle1() {
54          double[] v1 = {1, 0};
55          double[] v2 = {0, 1};
56          double[] v3 = {7, 7};
57          double cosAngle1 = CosAngle.value(v1, v1);
58          double cosAngle2 = CosAngle.value(v1, v2);
59          double cosAngle3 = CosAngle.value(v1, v3);
60          Assertions.assertEquals(1, cosAngle1);
61          Assertions.assertEquals(0, cosAngle2);
62          Assertions.assertEquals(0.7071067811865476, cosAngle3);
63          Assertions.assertEquals(45.0, Math.toDegrees(Math.acos(cosAngle3)));
64      }
65  }