1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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 }