View Javadoc
1   /*
2    * Licensed to the Hipparchus project 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 Hipparchus project 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    *      https://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.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 }