1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
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
44 }
45 try {
46 c = new MultidimensionalCounter(2, 0);
47 Assert.fail("MathIllegalArgumentException expected");
48 } catch (MathIllegalArgumentException e) {
49
50 }
51 try {
52 c = new MultidimensionalCounter(-1, 1);
53 Assert.fail("MathIllegalArgumentException expected");
54 } catch (MathIllegalArgumentException e) {
55
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
64 }
65 try {
66 c.getCount(3, 1);
67 Assert.fail("MathIllegalArgumentException expected");
68 } catch (MathIllegalArgumentException e) {
69
70 }
71 try {
72 c.getCount(0, -1);
73 Assert.fail("MathIllegalArgumentException expected");
74 } catch (MathIllegalArgumentException e) {
75
76 }
77 try {
78 c.getCounts(-1);
79 Assert.fail("MathIllegalArgumentException expected");
80 } catch (MathIllegalArgumentException e) {
81
82 }
83 try {
84 c.getCounts(6);
85 Assert.fail("MathIllegalArgumentException expected");
86 } catch (MathIllegalArgumentException e) {
87
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
99 }
100 try {
101 iter.getCount(2);
102 Assert.fail("IndexOutOfBoundsException expected");
103 } catch (IndexOutOfBoundsException e) {
104
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
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
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 }