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