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    *      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  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  
23  package org.hipparchus.linear;
24  
25  import org.hipparchus.exception.LocalizedCoreFormats;
26  import org.hipparchus.exception.MathRuntimeException;
27  import org.hipparchus.linear.RealVector.Entry;
28  import org.junit.jupiter.api.Test;
29  
30  import java.util.Iterator;
31  
32  import static org.junit.jupiter.api.Assertions.assertEquals;
33  import static org.junit.jupiter.api.Assertions.assertThrows;
34  import static org.junit.jupiter.api.Assertions.fail;
35  
36  /**
37   * Tests for {@link RealVector}.
38   */
39  public class RealVectorTest extends RealVectorAbstractTest {
40  
41      @Override
42      public RealVector create(final double[] data) {
43          return new RealVectorTestImpl(data);
44      }
45  
46      @Test
47      @Override
48      public void testAppendVector() {
49          checkUnsupported(() -> super.testAppendVector());
50      }
51  
52      @Test
53      @Override
54      public void testAppendScalar() {
55          checkUnsupported(() -> super.testAppendScalar());
56      }
57  
58      @Test
59      @Override
60      public void testGetSubVector() {
61          checkUnsupported(() -> super.testGetSubVector());
62      }
63  
64      @Test
65      @Override
66      public void testGetSubVectorInvalidIndex1() {
67          assertThrows(UnsupportedOperationException.class, () -> {
68              final int n = 10;
69              create(new double[n]).getSubVector(-1, 2);
70          });
71      }
72  
73      @Test
74      @Override
75      public void testGetSubVectorInvalidIndex2() {
76          assertThrows(UnsupportedOperationException.class, () -> {
77              final int n = 10;
78              create(new double[n]).getSubVector(2, 2);
79          });
80      }
81  
82      @Test
83      @Override
84      public void testGetSubVectorInvalidIndex3() {
85          assertThrows(UnsupportedOperationException.class, () -> {
86              final int n = 10;
87              create(new double[n]).getSubVector(0, 2);
88          });
89      }
90  
91      @Test
92      @Override
93      public void testGetSubVectorInvalidIndex4() {
94          assertThrows(UnsupportedOperationException.class, () -> {
95              final int n = 10;
96              create(new double[n]).getSubVector(3, 2);
97          });
98      }
99  
100     @Test
101     @Override
102     public void testSetSubVectorSameType() {
103         checkUnsupported(() -> super.testSetSubVectorSameType());
104     }
105 
106     @Test
107     @Override
108     public void testSetSubVectorMixedType() {
109         checkUnsupported(() -> super.testSetSubVectorMixedType());
110     }
111 
112     @Test
113     @Override
114     public void testSetSubVectorInvalidIndex1() {
115         assertThrows(UnsupportedOperationException.class, () -> {
116             create(new double[10]).setSubVector(-1, create(new double[2]));
117         });
118     }
119 
120     @Test
121     @Override
122     public void testSetSubVectorInvalidIndex2() {
123         assertThrows(UnsupportedOperationException.class, () -> {
124             create(new double[10]).setSubVector(10, create(new double[2]));
125         });
126     }
127 
128     @Test
129     @Override
130     public void testSetSubVectorInvalidIndex3() {
131         assertThrows(UnsupportedOperationException.class, () -> {
132             create(new double[10]).setSubVector(9, create(new double[2]));
133         });
134     }
135 
136     @Test
137     @Override
138     public void testIsNaN() {
139         checkUnsupported(() -> super.testIsNaN());
140     }
141 
142     @Test
143     @Override
144     public void testIsInfinite() {
145         checkUnsupported(() -> super.testIsInfinite());
146     }
147 
148     @Test
149     @Override
150     public void testEbeMultiplySameType() {
151         checkUnsupported(() -> super.testEbeMultiplySameType());
152     }
153 
154     @Test
155     @Override
156     public void testEbeMultiplyMixedTypes() {
157         checkUnsupported(() -> super.testEbeMultiplyMixedTypes());
158     }
159 
160     @Test
161     @Override
162     public void testEbeMultiplyDimensionMismatch() {
163         assertThrows(UnsupportedOperationException.class, () -> {
164             doTestEbeBinaryOperationDimensionMismatch(BinaryOperation.MUL);
165         });
166     }
167 
168     @Test
169     @Override
170     public void testEbeDivideSameType() {
171         checkUnsupported(() -> super.testEbeDivideSameType());
172     }
173 
174     @Test
175     @Override
176     public void testEbeDivideMixedTypes() {
177         checkUnsupported(() -> super.testEbeDivideMixedTypes());
178     }
179 
180     @Test
181     @Override
182     public void testEbeDivideDimensionMismatch() {
183         assertThrows(UnsupportedOperationException.class, () -> {
184             doTestEbeBinaryOperationDimensionMismatch(BinaryOperation.DIV);
185         });
186     }
187 
188     @Test
189     void testSparseIterator() {
190         /*
191          * For non-default values, use x + 1, x + 2, etc... to make sure that
192          * these values are really different from x.
193          */
194         final double x = getPreferredEntryValue();
195         final double[] data = {
196             x, x + 1d, x, x, x + 2d, x + 3d, x + 4d, x, x, x, x + 5d, x + 6d, x
197         };
198 
199         RealVector v = create(data);
200         Entry e;
201         int i = 0;
202         final double[] nonDefault = {
203             x + 1d, x + 2d, x + 3d, x + 4d, x + 5d, x + 6d
204         };
205         for (Iterator<Entry> it = v.sparseIterator(); it.hasNext(); i++) {
206             e = it.next();
207             assertEquals(nonDefault[i], e.getValue(), 0);
208         }
209         double [] onlyOne = {x, x + 1d, x};
210         v = create(onlyOne);
211         for(Iterator<Entry> it = v.sparseIterator(); it.hasNext(); ) {
212             e = it.next();
213             assertEquals(onlyOne[1], e.getValue(), 0);
214         }
215     }
216 
217     @Test
218     @Override
219     public void testSerial() {
220         checkUnsupported(() -> super.testSerial());
221     }
222 
223     @Test
224     @Override
225     public void testEquals() {
226         checkUnsupported(() -> super.testEquals());
227     }
228 
229     interface Thunk {
230         void call();
231     }
232 
233     private void checkUnsupported(final Thunk t) {
234         try {
235             t.call();
236             fail("an exception should have been thrown");
237         } catch (MathRuntimeException mre) {
238             assertEquals(LocalizedCoreFormats.UNSUPPORTED_OPERATION, mre.getSpecifier());
239         } catch (UnsupportedOperationException uoe) {
240             // expected
241         }
242     }
243 
244 }