View Javadoc
1   /*
2    * Licensed to the Hipparchus project 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 Hipparchus project 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  package org.hipparchus.util;
18  
19  import org.hipparchus.exception.LocalizedCoreFormats;
20  import org.hipparchus.exception.MathIllegalArgumentException;
21  import org.junit.jupiter.api.Test;
22  
23  import java.util.NoSuchElementException;
24  
25  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
26  import static org.junit.jupiter.api.Assertions.assertEquals;
27  import static org.junit.jupiter.api.Assertions.assertFalse;
28  import static org.junit.jupiter.api.Assertions.assertTrue;
29  import static org.junit.jupiter.api.Assertions.fail;
30  
31  /**
32   * Test cases for the {@link RosenNumberPartitionIterator} class.
33   */
34  class RosenNumberPartitionIteratorTest {
35  
36      @Test
37      void testRosenPartitionNegativeK() {
38          try {
39           new RosenNumberPartitionIterator(4, -1);
40           fail("an exception should have been thrown");
41          } catch (MathIllegalArgumentException miae) {
42              assertEquals(LocalizedCoreFormats.OUT_OF_RANGE_SIMPLE, miae.getSpecifier());
43              assertEquals(-1, ((Integer) miae.getParts()[0]).intValue());
44              assertEquals( 1, ((Integer) miae.getParts()[1]).intValue());
45              assertEquals( 4, ((Integer) miae.getParts()[2]).intValue());
46          }
47      }
48  
49      @Test
50      void testRosenPartitionKGreaterThanN() {
51          try {
52           new RosenNumberPartitionIterator(4, 5);
53           fail("an exception should have been thrown");
54          } catch (MathIllegalArgumentException miae) {
55              assertEquals(LocalizedCoreFormats.OUT_OF_RANGE_SIMPLE, miae.getSpecifier());
56              assertEquals( 5, ((Integer) miae.getParts()[0]).intValue());
57              assertEquals( 1, ((Integer) miae.getParts()[1]).intValue());
58              assertEquals( 4, ((Integer) miae.getParts()[2]).intValue());
59          }
60      }
61  
62      @Test
63      void testRosenPartition42() {
64          RosenNumberPartitionIterator i = new RosenNumberPartitionIterator(4, 2);
65          assertTrue(i.hasNext());
66          assertArrayEquals(new int[] { 1,  3 }, i.next());
67          assertTrue(i.hasNext());
68          assertArrayEquals(new int[] { 2,  2 }, i.next());
69          assertTrue(i.hasNext());
70          assertArrayEquals(new int[] { 3, 1 }, i.next());
71          assertFalse(i.hasNext());
72          try {
73              i.next();
74              fail("an exception should have been thrown");
75          } catch (NoSuchElementException e) {
76              // expected
77          }
78      }
79  
80      @Test
81      void testRosenPartition103() {
82          RosenNumberPartitionIterator i = new RosenNumberPartitionIterator(10, 3);
83          assertTrue(i.hasNext());
84          assertArrayEquals(new int[] { 1, 1, 8 }, i.next());
85          assertTrue(i.hasNext());
86          assertArrayEquals(new int[] { 1, 2, 7 }, i.next());
87          assertTrue(i.hasNext());
88          assertArrayEquals(new int[] { 1, 3, 6 }, i.next());
89          assertTrue(i.hasNext());
90          assertArrayEquals(new int[] { 1, 4, 5 }, i.next());
91          assertTrue(i.hasNext());
92          assertArrayEquals(new int[] { 1, 5, 4 }, i.next());
93          assertTrue(i.hasNext());
94          assertArrayEquals(new int[] { 1, 6, 3 }, i.next());
95          assertTrue(i.hasNext());
96          assertArrayEquals(new int[] { 1, 7, 2 }, i.next());
97          assertTrue(i.hasNext());
98          assertArrayEquals(new int[] { 1, 8, 1 }, i.next());
99          assertTrue(i.hasNext());
100         assertArrayEquals(new int[] { 2, 1, 7 }, i.next());
101         assertTrue(i.hasNext());
102         assertArrayEquals(new int[] { 2, 2, 6 }, i.next());
103         assertTrue(i.hasNext());
104         assertArrayEquals(new int[] { 2, 3, 5 }, i.next());
105         assertTrue(i.hasNext());
106         assertArrayEquals(new int[] { 2, 4, 4 }, i.next());
107         assertTrue(i.hasNext());
108         assertArrayEquals(new int[] { 2, 5, 3 }, i.next());
109         assertTrue(i.hasNext());
110         assertArrayEquals(new int[] { 2, 6, 2 }, i.next());
111         assertTrue(i.hasNext());
112         assertArrayEquals(new int[] { 2, 7, 1 }, i.next());
113         assertTrue(i.hasNext());
114         assertArrayEquals(new int[] { 3, 1, 6 }, i.next());
115         assertTrue(i.hasNext());
116         assertArrayEquals(new int[] { 3, 2, 5 }, i.next());
117         assertTrue(i.hasNext());
118         assertArrayEquals(new int[] { 3, 3, 4 }, i.next());
119         assertTrue(i.hasNext());
120         assertArrayEquals(new int[] { 3, 4, 3 }, i.next());
121         assertTrue(i.hasNext());
122         assertArrayEquals(new int[] { 3, 5, 2 }, i.next());
123         assertTrue(i.hasNext());
124         assertArrayEquals(new int[] { 3, 6, 1 }, i.next());
125         assertTrue(i.hasNext());
126         assertArrayEquals(new int[] { 4, 1, 5 }, i.next());
127         assertTrue(i.hasNext());
128         assertArrayEquals(new int[] { 4, 2, 4 }, i.next());
129         assertTrue(i.hasNext());
130         assertArrayEquals(new int[] { 4, 3, 3 }, i.next());
131         assertTrue(i.hasNext());
132         assertArrayEquals(new int[] { 4, 4, 2 }, i.next());
133         assertTrue(i.hasNext());
134         assertArrayEquals(new int[] { 4, 5, 1 }, i.next());
135         assertTrue(i.hasNext());
136         assertArrayEquals(new int[] { 5, 1, 4 }, i.next());
137         assertTrue(i.hasNext());
138         assertArrayEquals(new int[] { 5, 2, 3 }, i.next());
139         assertTrue(i.hasNext());
140         assertArrayEquals(new int[] { 5, 3, 2 }, i.next());
141         assertTrue(i.hasNext());
142         assertArrayEquals(new int[] { 5, 4, 1 }, i.next());
143         assertTrue(i.hasNext());
144         assertArrayEquals(new int[] { 6, 1, 3 }, i.next());
145         assertTrue(i.hasNext());
146         assertArrayEquals(new int[] { 6, 2, 2 }, i.next());
147         assertTrue(i.hasNext());
148         assertArrayEquals(new int[] { 6, 3, 1 }, i.next());
149         assertTrue(i.hasNext());
150         assertArrayEquals(new int[] { 7, 1, 2 }, i.next());
151         assertTrue(i.hasNext());
152         assertArrayEquals(new int[] { 7, 2, 1 }, i.next());
153         assertTrue(i.hasNext());
154         assertArrayEquals(new int[] { 8, 1, 1 }, i.next());
155         assertFalse(i.hasNext());
156     }
157 
158 }