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