1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.hipparchus.util;
15
16 import org.hipparchus.exception.LocalizedCoreFormats;
17 import org.hipparchus.exception.MathIllegalStateException;
18 import org.junit.jupiter.api.Test;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertFalse;
22 import static org.junit.jupiter.api.Assertions.assertNotEquals;
23 import static org.junit.jupiter.api.Assertions.assertThrows;
24 import static org.junit.jupiter.api.Assertions.assertTrue;
25 import static org.junit.jupiter.api.Assertions.fail;
26
27
28
29
30 class IncrementorTest {
31 @Test
32 void testConstructor1() {
33 final Incrementor i = new Incrementor();
34 assertEquals(Integer.MAX_VALUE, i.getMaximalCount());
35 assertEquals(0, i.getCount());
36 }
37
38 @Test
39 void testConstructor2() {
40 final Incrementor i = new Incrementor(10);
41 assertEquals(10, i.getMaximalCount());
42 assertEquals(0, i.getCount());
43 }
44
45 @Test
46 void testCanIncrement1() {
47 final Incrementor i = new Incrementor(3);
48 assertTrue(i.canIncrement());
49 i.increment();
50 assertTrue(i.canIncrement());
51 i.increment();
52 assertTrue(i.canIncrement());
53 i.increment();
54 assertFalse(i.canIncrement());
55 }
56
57 @Test
58 void testCanIncrement2() {
59 final Incrementor i = new Incrementor(3);
60 while (i.canIncrement()) {
61 i.increment();
62 }
63
64
65
66 try {
67 i.increment();
68 fail("MathIllegalStateException expected");
69 } catch (MathIllegalStateException e) {
70
71 }
72 }
73
74 @Test
75 void testBulkCanIncrement() {
76 final Incrementor i = new Incrementor(3);
77 while (i.canIncrement(2)) {
78 i.increment(2);
79 }
80
81 assertEquals(2, i.getCount());
82 }
83
84 @Test
85 void testAccessor() {
86 final Incrementor i = new Incrementor(10);
87
88 assertEquals(10, i.getMaximalCount());
89 assertEquals(0, i.getCount());
90 }
91
92 @Test
93 void testBelowMaxCount() {
94 final Incrementor i = new Incrementor(3);
95
96 i.increment();
97 i.increment();
98 i.increment();
99
100 assertEquals(3, i.getCount());
101 }
102
103 @Test
104 void testAboveMaxCount() {
105 assertThrows(MathIllegalStateException.class, () -> {
106 final Incrementor i = new Incrementor(3);
107
108 i.increment();
109 i.increment();
110 i.increment();
111 i.increment();
112 });
113 }
114
115 @Test
116 void testAlternateException() {
117 assertThrows(IllegalStateException.class, () -> {
118 final Incrementor.MaxCountExceededCallback cb =
119 (int max) -> {
120 throw new IllegalStateException();
121 };
122
123 final Incrementor i = new Incrementor(0, cb);
124 i.increment();
125 });
126 }
127
128 @Test
129 void testReset() {
130 final Incrementor i = new Incrementor(3);
131
132 i.increment();
133 i.increment();
134 i.increment();
135 assertEquals(3, i.getCount());
136 i.reset();
137 assertEquals(0, i.getCount());
138 }
139
140 @Test
141 void testBulkIncrement() {
142 final Incrementor i = new Incrementor(3);
143
144 i.increment(2);
145 assertEquals(2, i.getCount());
146 i.increment(1);
147 assertEquals(3, i.getCount());
148 }
149
150 @Test
151 void testBulkIncrementExceeded() {
152 assertThrows(MathIllegalStateException.class, () -> {
153 final Incrementor i = new Incrementor(3);
154
155 i.increment(2);
156 assertEquals(2, i.getCount());
157 i.increment(2);
158 });
159 }
160
161 @Test
162 void testWithMaximalValue()
163 {
164 final Incrementor i = new Incrementor(3);
165
166 assertEquals(3, i.getMaximalCount());
167
168 Incrementor i2 = i.withMaximalCount(10);
169
170 assertNotEquals(i, i2);
171 assertEquals(3, i.getMaximalCount());
172 assertEquals(10, i2.getMaximalCount());
173 }
174
175 @Test
176 void testMaxInt() {
177 final Incrementor i = new Incrementor().withCount(Integer.MAX_VALUE - 2);
178 i.increment();
179 assertEquals(Integer.MAX_VALUE - 1, i.getCount());
180 i.increment();
181 assertEquals(Integer.MAX_VALUE, i.getCount());
182 assertFalse(i.canIncrement());
183 try {
184 i.increment();
185 fail("an exception should have been throwns");
186 } catch (MathIllegalStateException mise) {
187 assertEquals(LocalizedCoreFormats.MAX_COUNT_EXCEEDED, mise.getSpecifier());
188 }
189 }
190
191 }