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