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.math4.transform;
18  
19  import java.util.function.DoubleUnaryOperator;
20  
21  import org.junit.Assert;
22  import org.junit.Test;
23  
24  import org.apache.commons.math3.analysis.function.Sin;
25  
26  import static org.junit.jupiter.api.Assertions.assertThrows;
27  
28  /**
29   * Tests for {@link TransformUtils}.
30   */
31  public class TransformUtilsTest {
32  
33      private static final Sin SIN_FUNCTION = new Sin();
34      private static final DoubleUnaryOperator SIN = x -> SIN_FUNCTION.value(x);
35  
36      @Test
37      public void testSampleWrongBounds() {
38          assertThrows(TransformException.class, () ->
39                  TransformUtils.sample(SIN, Math.PI, 0.0, 10));
40      }
41  
42      @Test
43      public void testSampleNegativeNumberOfPoints() {
44          assertThrows(TransformException.class, () ->
45                  TransformUtils.sample(SIN, 0.0, Math.PI, -1));
46      }
47  
48      @Test
49      public void testSampleNullNumberOfPoints() {
50          assertThrows(TransformException.class, () ->
51                  TransformUtils.sample(SIN, 0.0, Math.PI, 0));
52      }
53  
54      @Test
55      public void testSample() {
56          final int n = 11;
57          final double min = 0.0;
58          final double max = Math.PI;
59          final double[] actual = TransformUtils.sample(SIN, min, max, n);
60          for (int i = 0; i < n; i++) {
61              final double x = min + (max - min) / n * i;
62              Assert.assertEquals("x = " + x, Math.sin(x), actual[i], 1e-15);
63          }
64      }
65  
66  }