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 this
4    * work for additional information regarding copyright ownership. The ASF
5    * licenses this file to You under the Apache License, Version 2.0 (the
6    * "License"); you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * https://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
9    * or agreed to in writing, software distributed under the License is
10   * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11   * KIND, either express or implied. See the License for the specific language
12   * governing permissions and limitations under the License.
13   */
14  package org.hipparchus.util;
15  
16  import org.hipparchus.exception.LocalizedCoreFormats;
17  import org.hipparchus.exception.MathIllegalStateException;
18  import org.junit.Assert;
19  import org.junit.Test;
20  
21  /**
22   * Test for {@link Incrementor}.
23   */
24  public class IncrementorTest {
25      @Test
26      public void testConstructor1() {
27          final Incrementor i = new Incrementor();
28          Assert.assertEquals(Integer.MAX_VALUE, i.getMaximalCount());
29          Assert.assertEquals(0, i.getCount());
30      }
31  
32      @Test
33      public void testConstructor2() {
34          final Incrementor i = new Incrementor(10);
35          Assert.assertEquals(10, i.getMaximalCount());
36          Assert.assertEquals(0, i.getCount());
37      }
38  
39      @Test
40      public void testCanIncrement1() {
41          final Incrementor i = new Incrementor(3);
42          Assert.assertTrue(i.canIncrement());
43          i.increment();
44          Assert.assertTrue(i.canIncrement());
45          i.increment();
46          Assert.assertTrue(i.canIncrement());
47          i.increment();
48          Assert.assertFalse(i.canIncrement());
49      }
50  
51      @Test
52      public void testCanIncrement2() {
53          final Incrementor i = new Incrementor(3);
54          while (i.canIncrement()) {
55              i.increment();
56          }
57  
58          // Must keep try/catch because the exception must be generated here,
59          // and not in the previous loop.
60          try {
61              i.increment();
62              Assert.fail("MathIllegalStateException expected");
63          } catch (MathIllegalStateException e) {
64              // Expected.
65          }
66      }
67  
68      @Test
69      public void testBulkCanIncrement() {
70          final Incrementor i = new Incrementor(3);
71          while (i.canIncrement(2)) {
72              i.increment(2);
73          }
74  
75          Assert.assertEquals(2, i.getCount());
76      }
77  
78      @Test
79      public void testAccessor() {
80          final Incrementor i = new Incrementor(10);
81  
82          Assert.assertEquals(10, i.getMaximalCount());
83          Assert.assertEquals(0, i.getCount());
84      }
85  
86      @Test
87      public void testBelowMaxCount() {
88          final Incrementor i = new Incrementor(3);
89  
90          i.increment();
91          i.increment();
92          i.increment();
93  
94          Assert.assertEquals(3, i.getCount());
95      }
96  
97      @Test(expected=MathIllegalStateException.class)
98      public void testAboveMaxCount() {
99          final Incrementor i = new Incrementor(3);
100 
101         i.increment();
102         i.increment();
103         i.increment();
104         i.increment();
105     }
106 
107     @Test(expected=IllegalStateException.class)
108     public void testAlternateException() {
109         final Incrementor.MaxCountExceededCallback cb =
110             (int max) -> {
111                 throw new IllegalStateException();
112             };
113 
114         final Incrementor i = new Incrementor(0, cb);
115         i.increment();
116     }
117 
118     @Test
119     public void testReset() {
120         final Incrementor i = new Incrementor(3);
121 
122         i.increment();
123         i.increment();
124         i.increment();
125         Assert.assertEquals(3, i.getCount());
126         i.reset();
127         Assert.assertEquals(0, i.getCount());
128     }
129 
130     @Test
131     public void testBulkIncrement() {
132         final Incrementor i = new Incrementor(3);
133 
134         i.increment(2);
135         Assert.assertEquals(2, i.getCount());
136         i.increment(1);
137         Assert.assertEquals(3, i.getCount());
138     }
139 
140     @Test(expected=MathIllegalStateException.class)
141     public void testBulkIncrementExceeded() {
142         final Incrementor i = new Incrementor(3);
143 
144         i.increment(2);
145         Assert.assertEquals(2, i.getCount());
146         i.increment(2);
147     }
148 
149     @Test
150     public void testWithMaximalValue()
151     {
152         final Incrementor i = new Incrementor(3);
153 
154         Assert.assertEquals(3, i.getMaximalCount());
155 
156         Incrementor i2 = i.withMaximalCount(10);
157 
158         Assert.assertNotEquals(i, i2);
159         Assert.assertEquals(3, i.getMaximalCount());
160         Assert.assertEquals(10, i2.getMaximalCount());
161     }
162 
163     @Test
164     public void testMaxInt() {
165        final Incrementor i = new Incrementor().withCount(Integer.MAX_VALUE - 2);
166         i.increment();
167         Assert.assertEquals(Integer.MAX_VALUE - 1, i.getCount());
168         i.increment();
169         Assert.assertEquals(Integer.MAX_VALUE, i.getCount());
170         Assert.assertFalse(i.canIncrement());
171         try {
172             i.increment();
173             Assert.fail("an exception should have been throwns");
174         } catch (MathIllegalStateException mise) {
175             Assert.assertEquals(mise.getSpecifier(), LocalizedCoreFormats.MAX_COUNT_EXCEEDED);
176         }
177     }
178 
179 }