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.util;
24  
25  import org.hipparchus.exception.LocalizedCoreFormats;
26  import org.hipparchus.exception.MathIllegalArgumentException;
27  import org.junit.jupiter.api.Assertions;
28  import org.junit.jupiter.api.Test;
29  
30  import java.util.NoSuchElementException;
31  
32  import static org.junit.jupiter.api.Assertions.assertEquals;
33  import static org.junit.jupiter.api.Assertions.assertFalse;
34  import static org.junit.jupiter.api.Assertions.assertThrows;
35  import static org.junit.jupiter.api.Assertions.assertTrue;
36  import static org.junit.jupiter.api.Assertions.fail;
37  
38  /**
39   * Test cases for the {@link MultidimensionalCounter} class.
40   */
41  class MultidimensionalCounterTest {
42      @Test
43      void testPreconditions() {
44          MultidimensionalCounter c;
45  
46          try {
47              new MultidimensionalCounter(0, 1);
48              fail("MathIllegalArgumentException expected");
49          } catch (MathIllegalArgumentException e) {
50              // Expected.
51          }
52          try {
53              new MultidimensionalCounter(2, 0);
54              fail("MathIllegalArgumentException expected");
55          } catch (MathIllegalArgumentException e) {
56              // Expected.
57          }
58          try {
59              new MultidimensionalCounter(-1, 1);
60              fail("MathIllegalArgumentException expected");
61          } catch (MathIllegalArgumentException e) {
62              // Expected.
63          }
64  
65          c = new MultidimensionalCounter(2, 3);
66          try {
67              c.getCount(1, 1, 1);
68              fail("MathIllegalArgumentException expected");
69          } catch (MathIllegalArgumentException e) {
70              // Expected.
71          }
72          try {
73              c.getCount(3, 1);
74              fail("MathIllegalArgumentException expected");
75          } catch (MathIllegalArgumentException e) {
76              // Expected.
77          }
78          try {
79              c.getCount(0, -1);
80              fail("MathIllegalArgumentException expected");
81          } catch (MathIllegalArgumentException e) {
82              // Expected.
83          }
84          try {
85              c.getCounts(-1);
86              fail("MathIllegalArgumentException expected");
87          } catch (MathIllegalArgumentException e) {
88              // Expected.
89          }
90          try {
91              c.getCounts(6);
92              fail("MathIllegalArgumentException expected");
93          } catch (MathIllegalArgumentException e) {
94              // Expected.
95          }
96      }
97  
98      @Test
99      void testIteratorPreconditions() {
100         MultidimensionalCounter.Iterator iter = (new MultidimensionalCounter(2, 3)).iterator();
101         try {
102             iter.getCount(-1);
103             fail("IndexOutOfBoundsException expected");
104         } catch (IndexOutOfBoundsException e) {
105             // Expected.
106         }
107         try {
108             iter.getCount(2);
109             fail("IndexOutOfBoundsException expected");
110         } catch (IndexOutOfBoundsException e) {
111             // Expected.
112         }
113     }
114 
115     @Test
116     void testIterator() {
117         final int dim1 = 3;
118         final int dim2 = 4;
119 
120         final MultidimensionalCounter.Iterator iter
121             = new MultidimensionalCounter(dim1, dim2).iterator();
122 
123         final int max = dim1 * dim2;
124         for (int i = 0; i < max; i++) {
125             assertTrue(iter.hasNext());
126 
127             // Should not throw.
128             iter.next();
129         }
130 
131         assertFalse(iter.hasNext());
132     }
133 
134     @Test
135     void testIteratorNoMoreElements() {
136         assertThrows(NoSuchElementException.class, () -> {
137             final MultidimensionalCounter.Iterator iter
138                 = new MultidimensionalCounter(4, 2).iterator();
139 
140             while (iter.hasNext()) {
141                 iter.next();
142             }
143 
144             // No more elements: should throw.
145             iter.next();
146         });
147     }
148 
149     @Test
150     void testMulti2UniConversion() {
151         final MultidimensionalCounter c = new MultidimensionalCounter(2, 4, 5);
152         assertEquals(33, c.getCount(1, 2, 3));
153     }
154 
155     @Test
156     void testAccessors() {
157         final int[] originalSize = new int[] {2, 6, 5};
158         final MultidimensionalCounter c = new MultidimensionalCounter(originalSize);
159         final int nDim = c.getDimension();
160         assertEquals(nDim, originalSize.length);
161 
162         final int[] size = c.getSizes();
163         for (int i = 0; i < nDim; i++) {
164             assertEquals(originalSize[i], size[i]);
165         }
166     }
167 
168     @Test
169     void testIterationConsistency() {
170         final MultidimensionalCounter c = new MultidimensionalCounter(2, 3, 4);
171         final int[][] expected = new int[][] {
172             { 0, 0, 0 },
173             { 0, 0, 1 },
174             { 0, 0, 2 },
175             { 0, 0, 3 },
176             { 0, 1, 0 },
177             { 0, 1, 1 },
178             { 0, 1, 2 },
179             { 0, 1, 3 },
180             { 0, 2, 0 },
181             { 0, 2, 1 },
182             { 0, 2, 2 },
183             { 0, 2, 3 },
184             { 1, 0, 0 },
185             { 1, 0, 1 },
186             { 1, 0, 2 },
187             { 1, 0, 3 },
188             { 1, 1, 0 },
189             { 1, 1, 1 },
190             { 1, 1, 2 },
191             { 1, 1, 3 },
192             { 1, 2, 0 },
193             { 1, 2, 1 },
194             { 1, 2, 2 },
195             { 1, 2, 3 }
196         };
197 
198         final int totalSize = c.getSize();
199         assertEquals(expected.length, totalSize);
200 
201         final int nDim = c.getDimension();
202         final MultidimensionalCounter.Iterator iter = c.iterator();
203         for (int i = 0; i < totalSize; i++) {
204             if (!iter.hasNext()) {
205                 fail("Too short");
206             }
207             final int uniDimIndex = iter.next();
208             assertEquals(i, uniDimIndex, "Wrong iteration at " + i);
209 
210             for (int dimIndex = 0; dimIndex < nDim; dimIndex++) {
211                 assertEquals(expected[i][dimIndex], iter.getCount(dimIndex), "Wrong multidimensional index for [" + i + "][" + dimIndex + "]");
212             }
213 
214             assertEquals(c.getCount(expected[i]), uniDimIndex, "Wrong unidimensional index for [" + i + "]");
215 
216             final int[] indices = c.getCounts(uniDimIndex);
217             for (int dimIndex = 0; dimIndex < nDim; dimIndex++) {
218                 assertEquals(expected[i][dimIndex], indices[dimIndex], "Wrong multidimensional index for [" + i + "][" + dimIndex + "]");
219             }
220         }
221 
222         if (iter.hasNext()) {
223             fail("Too long");
224         }
225     }
226 
227     @Test
228     void testIssue470() {
229 
230         checkOverflow(65537, 65536);
231         checkOverflow(46341, 46341);
232         checkOverflow(1000, 1000, 4295);
233         checkOverflow(641, 6700417);
234         checkOverflow(2, 2147483647);
235         checkOverflow(3, 1431655766);
236 
237         Assertions.assertEquals(        60, new MultidimensionalCounter(3, 4, 5).getSize());
238         Assertions.assertEquals(2147395600, new MultidimensionalCounter(46340, 46340).getSize());
239         Assertions.assertEquals(2147483647, new MultidimensionalCounter(2147483647).getSize());
240 
241     }
242 
243     private void checkOverflow(int... dims) {
244         try {
245             new MultidimensionalCounter(dims);
246             Assertions.fail("an exception should have been thrown");
247         } catch (MathIllegalArgumentException miae) {
248             Assertions.assertEquals(LocalizedCoreFormats.OUT_OF_RANGE_SIMPLE, miae.getSpecifier());
249         }
250     }
251 
252 }