1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.hipparchus.analysis.interpolation;
18
19 import org.hipparchus.exception.LocalizedCoreFormats;
20 import org.hipparchus.exception.MathIllegalArgumentException;
21 import org.junit.jupiter.api.Assertions;
22 import org.junit.jupiter.api.Test;
23
24 public class RegularIndexerTest {
25
26 @Test
27 public void testGetters() {
28 final RegularIndexer sampling = new RegularIndexer(3.0, 5.0, 5);
29 Assertions.assertEquals(3.0, sampling.getMin(), 1.0e-15);
30 Assertions.assertEquals(5.0, sampling.getMax(), 1.0e-15);
31 Assertions.assertEquals(5, sampling.getN());
32 Assertions.assertEquals(0.5, sampling.getStep(), 1.0e-15);
33 }
34
35 @Test
36 public void testIndexAtNodes() {
37 final RegularIndexer sampling = new RegularIndexer(3.0, 5.0, 5);
38 Assertions.assertEquals(0, sampling.index(3.0));
39 Assertions.assertEquals(1, sampling.index(3.5));
40 Assertions.assertEquals(2, sampling.index(4.0));
41 Assertions.assertEquals(3, sampling.index(4.5));
42 Assertions.assertEquals(4, sampling.index(5.0));
43 }
44
45 @Test
46 public void testIndexAwayFromNodes() {
47 final RegularIndexer sampling = new RegularIndexer(3.0, 5.0, 5);
48 Assertions.assertEquals(0, sampling.index(2.750000001));
49 Assertions.assertEquals(0, sampling.index(3.249999999));
50 Assertions.assertEquals(1, sampling.index(3.250000001));
51 Assertions.assertEquals(1, sampling.index(3.749999999));
52 Assertions.assertEquals(2, sampling.index(3.750000001));
53 Assertions.assertEquals(2, sampling.index(4.249999999));
54 Assertions.assertEquals(3, sampling.index(4.250000001));
55 Assertions.assertEquals(3, sampling.index(4.749999999));
56 Assertions.assertEquals(4, sampling.index(4.750000001));
57 Assertions.assertEquals(4, sampling.index(5.249999999));
58 }
59
60 @Test
61 public void testIndexTooLow() {
62 final RegularIndexer sampling = new RegularIndexer(300.0, 500.0, 5);
63 try {
64 sampling.index(274.9999999);
65 Assertions.fail("an exception should have been thrown");
66 } catch (MathIllegalArgumentException miae) {
67 Assertions.assertEquals(LocalizedCoreFormats.OUT_OF_RANGE_SIMPLE, miae.getSpecifier());
68 Assertions.assertEquals(-1L, miae.getParts()[0]);
69 Assertions.assertEquals( 0L, miae.getParts()[1]);
70 Assertions.assertEquals( 4L, miae.getParts()[2]);
71 }
72 }
73
74 @Test
75 public void testIndexTooHigh() {
76 final RegularIndexer sampling = new RegularIndexer(300.0, 500.0, 5);
77 try {
78 sampling.index(525.0000001);
79 Assertions.fail("an exception should have been thrown");
80 } catch (MathIllegalArgumentException miae) {
81 Assertions.assertEquals(LocalizedCoreFormats.OUT_OF_RANGE_SIMPLE, miae.getSpecifier());
82 Assertions.assertEquals( 5L, miae.getParts()[0]);
83 Assertions.assertEquals( 0L, miae.getParts()[1]);
84 Assertions.assertEquals( 4L, miae.getParts()[2]);
85 }
86 }
87
88 @Test
89 public void testCoordinateInRange() {
90 final RegularIndexer sampling = new RegularIndexer(300.0, 500.0, 5);
91 Assertions.assertEquals(300.0, sampling.coordinate(0), 1.0e-15);
92 Assertions.assertEquals(350.0, sampling.coordinate(1), 1.0e-15);
93 Assertions.assertEquals(400.0, sampling.coordinate(2), 1.0e-15);
94 Assertions.assertEquals(450.0, sampling.coordinate(3), 1.0e-15);
95 Assertions.assertEquals(500.0, sampling.coordinate(4), 1.0e-15);
96 }
97
98 @Test
99 public void testCoordinateTooLow() {
100 final RegularIndexer sampling = new RegularIndexer(300.0, 500.0, 5);
101 try {
102 sampling.coordinate(-1);
103 Assertions.fail("an exception should have been thrown");
104 } catch (MathIllegalArgumentException miae) {
105 Assertions.assertEquals(LocalizedCoreFormats.OUT_OF_RANGE_SIMPLE, miae.getSpecifier());
106 Assertions.assertEquals(-1L, miae.getParts()[0]);
107 Assertions.assertEquals( 0L, miae.getParts()[1]);
108 Assertions.assertEquals( 4L, miae.getParts()[2]);
109 }
110 }
111
112 @Test
113 public void testCoordinateTooHigh() {
114 final RegularIndexer sampling = new RegularIndexer(300.0, 500.0, 5);
115 try {
116 sampling.coordinate(5);
117 Assertions.fail("an exception should have been thrown");
118 } catch (MathIllegalArgumentException miae) {
119 Assertions.assertEquals(LocalizedCoreFormats.OUT_OF_RANGE_SIMPLE, miae.getSpecifier());
120 Assertions.assertEquals( 5L, miae.getParts()[0]);
121 Assertions.assertEquals( 0L, miae.getParts()[1]);
122 Assertions.assertEquals( 4L, miae.getParts()[2]);
123 }
124 }
125
126 }