• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.math;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import com.google.common.annotations.GwtCompatible;
22 import java.math.BigInteger;
23 import java.math.RoundingMode;
24 import junit.framework.TestCase;
25 
26 /**
27  * Unit tests for {@link MathPreconditions}.
28  *
29  * @author Ben Yu
30  */
31 @GwtCompatible
32 public class MathPreconditionsTest extends TestCase {
33 
testCheckPositive_zeroInt()34   public void testCheckPositive_zeroInt() {
35     try {
36       MathPreconditions.checkPositive("int", 0);
37       fail();
38     } catch (IllegalArgumentException expected) {
39     }
40   }
41 
testCheckPositive_maxInt()42   public void testCheckPositive_maxInt() {
43     MathPreconditions.checkPositive("int", Integer.MAX_VALUE);
44   }
45 
testCheckPositive_minInt()46   public void testCheckPositive_minInt() {
47     try {
48       MathPreconditions.checkPositive("int", Integer.MIN_VALUE);
49       fail();
50     } catch (IllegalArgumentException expected) {
51     }
52   }
53 
testCheckPositive_positiveInt()54   public void testCheckPositive_positiveInt() {
55     MathPreconditions.checkPositive("int", 1);
56   }
57 
testCheckPositive_negativeInt()58   public void testCheckPositive_negativeInt() {
59     try {
60       MathPreconditions.checkPositive("int", -1);
61       fail();
62     } catch (IllegalArgumentException expected) {
63     }
64   }
65 
testCheckPositive_zeroLong()66   public void testCheckPositive_zeroLong() {
67     try {
68       MathPreconditions.checkPositive("long", 0L);
69       fail();
70     } catch (IllegalArgumentException expected) {
71     }
72   }
73 
testCheckPositive_maxLong()74   public void testCheckPositive_maxLong() {
75     MathPreconditions.checkPositive("long", Long.MAX_VALUE);
76   }
77 
testCheckPositive_minLong()78   public void testCheckPositive_minLong() {
79     try {
80       MathPreconditions.checkPositive("long", Long.MIN_VALUE);
81       fail();
82     } catch (IllegalArgumentException expected) {
83     }
84   }
85 
testCheckPositive_positiveLong()86   public void testCheckPositive_positiveLong() {
87     MathPreconditions.checkPositive("long", 1);
88   }
89 
testCheckPositive_negativeLong()90   public void testCheckPositive_negativeLong() {
91     try {
92       MathPreconditions.checkPositive("long", -1L);
93       fail();
94     } catch (IllegalArgumentException expected) {
95     }
96   }
97 
testCheckPositive_zeroBigInteger()98   public void testCheckPositive_zeroBigInteger() {
99     try {
100       MathPreconditions.checkPositive("BigInteger", BigInteger.ZERO);
101       fail();
102     } catch (IllegalArgumentException expected) {
103     }
104   }
105 
testCheckPositive_postiveBigInteger()106   public void testCheckPositive_postiveBigInteger() {
107     MathPreconditions.checkPositive("BigInteger", BigInteger.ONE);
108   }
109 
testCheckPositive_negativeBigInteger()110   public void testCheckPositive_negativeBigInteger() {
111     try {
112       MathPreconditions.checkPositive("BigInteger", BigInteger.ZERO.negate());
113       fail();
114     } catch (IllegalArgumentException expected) {
115     }
116   }
117 
testCheckNonNegative_zeroInt()118   public void testCheckNonNegative_zeroInt() {
119     MathPreconditions.checkNonNegative("int", 0);
120   }
121 
testCheckNonNegative_maxInt()122   public void testCheckNonNegative_maxInt() {
123     MathPreconditions.checkNonNegative("int", Integer.MAX_VALUE);
124   }
125 
testCheckNonNegative_minInt()126   public void testCheckNonNegative_minInt() {
127     try {
128       MathPreconditions.checkNonNegative("int", Integer.MIN_VALUE);
129       fail();
130     } catch (IllegalArgumentException expected) {
131     }
132   }
133 
testCheckNonNegative_positiveInt()134   public void testCheckNonNegative_positiveInt() {
135     MathPreconditions.checkNonNegative("int", 1);
136   }
137 
testCheckNonNegative_negativeInt()138   public void testCheckNonNegative_negativeInt() {
139     try {
140       MathPreconditions.checkNonNegative("int", -1);
141       fail();
142     } catch (IllegalArgumentException expected) {
143     }
144   }
145 
testCheckNonNegative_zeroLong()146   public void testCheckNonNegative_zeroLong() {
147     MathPreconditions.checkNonNegative("long", 0L);
148   }
149 
testCheckNonNegative_maxLong()150   public void testCheckNonNegative_maxLong() {
151     MathPreconditions.checkNonNegative("long", Long.MAX_VALUE);
152   }
153 
testCheckNonNegative_minLong()154   public void testCheckNonNegative_minLong() {
155     try {
156       MathPreconditions.checkNonNegative("long", Long.MIN_VALUE);
157       fail();
158     } catch (IllegalArgumentException expected) {
159     }
160   }
161 
testCheckNonNegative_positiveLong()162   public void testCheckNonNegative_positiveLong() {
163     MathPreconditions.checkNonNegative("long", 1L);
164   }
165 
testCheckNonNegative_negativeLong()166   public void testCheckNonNegative_negativeLong() {
167     try {
168       MathPreconditions.checkNonNegative("int", -1L);
169       fail();
170     } catch (IllegalArgumentException expected) {
171     }
172   }
173 
testCheckNonNegative_zeroBigInteger()174   public void testCheckNonNegative_zeroBigInteger() {
175     MathPreconditions.checkNonNegative("BigInteger", BigInteger.ZERO);
176   }
177 
testCheckNonNegative_positiveBigInteger()178   public void testCheckNonNegative_positiveBigInteger() {
179     MathPreconditions.checkNonNegative("BigInteger", BigInteger.ONE);
180   }
181 
testCheckNonNegative_negativeBigInteger()182   public void testCheckNonNegative_negativeBigInteger() {
183     try {
184       MathPreconditions.checkNonNegative("int", BigInteger.ONE.negate());
185       fail();
186     } catch (IllegalArgumentException expected) {
187     }
188   }
189 
testCheckNonNegative_zeroFloat()190   public void testCheckNonNegative_zeroFloat() {
191     MathPreconditions.checkNonNegative("float", 0f);
192   }
193 
testCheckNonNegative_maxFloat()194   public void testCheckNonNegative_maxFloat() {
195     MathPreconditions.checkNonNegative("float", Float.MAX_VALUE);
196   }
197 
testCheckNonNegative_minFloat()198   public void testCheckNonNegative_minFloat() {
199     MathPreconditions.checkNonNegative("float", Float.MIN_VALUE);
200   }
201 
testCheckNonNegative_positiveFloat()202   public void testCheckNonNegative_positiveFloat() {
203     MathPreconditions.checkNonNegative("float", 1f);
204   }
205 
testCheckNonNegative_negativeFloat()206   public void testCheckNonNegative_negativeFloat() {
207     try {
208       MathPreconditions.checkNonNegative("float", -1f);
209       fail();
210     } catch (IllegalArgumentException expected) {
211     }
212   }
213 
testCheckNonNegative_nanFloat()214   public void testCheckNonNegative_nanFloat() {
215     try {
216       MathPreconditions.checkNonNegative("float", Float.NaN);
217       fail();
218     } catch (IllegalArgumentException expected) {
219     }
220   }
221 
testCheckNonNegative_zeroDouble()222   public void testCheckNonNegative_zeroDouble() {
223     MathPreconditions.checkNonNegative("double", 0d);
224   }
225 
testCheckNonNegative_maxDouble()226   public void testCheckNonNegative_maxDouble() {
227     MathPreconditions.checkNonNegative("double", Double.MAX_VALUE);
228   }
229 
testCheckNonNegative_minDouble()230   public void testCheckNonNegative_minDouble() {
231     MathPreconditions.checkNonNegative("double", Double.MIN_VALUE);
232   }
233 
testCheckNonNegative_positiveDouble()234   public void testCheckNonNegative_positiveDouble() {
235     MathPreconditions.checkNonNegative("double", 1d);
236   }
237 
testCheckNonNegative_negativeDouble()238   public void testCheckNonNegative_negativeDouble() {
239     try {
240       MathPreconditions.checkNonNegative("double", -1d);
241       fail();
242     } catch (IllegalArgumentException expected) {
243     }
244   }
245 
testCheckNonNegative_nanDouble()246   public void testCheckNonNegative_nanDouble() {
247     try {
248       MathPreconditions.checkNonNegative("double", Double.NaN);
249       fail();
250     } catch (IllegalArgumentException expected) {
251     }
252   }
253 
testCheckRoundingUnnnecessary_success()254   public void testCheckRoundingUnnnecessary_success() {
255     MathPreconditions.checkRoundingUnnecessary(true);
256   }
257 
testCheckRoundingUnnecessary_failure()258   public void testCheckRoundingUnnecessary_failure() {
259     try {
260       MathPreconditions.checkRoundingUnnecessary(false);
261       fail();
262     } catch (ArithmeticException expected) {
263     }
264   }
265 
testCheckInRange_success()266   public void testCheckInRange_success() {
267     MathPreconditions.checkInRangeForRoundingInputs(true, 1.0, RoundingMode.UP);
268   }
269 
testCheckInRange_failure()270   public void testCheckInRange_failure() {
271     try {
272       MathPreconditions.checkInRangeForRoundingInputs(false, 1.0, RoundingMode.UP);
273       fail();
274     } catch (ArithmeticException expected) {
275       assertThat(expected).hasMessageThat().contains("1.0");
276       assertThat(expected).hasMessageThat().contains("UP");
277     }
278   }
279 
testCheckNoOverflow_success()280   public void testCheckNoOverflow_success() {
281     MathPreconditions.checkNoOverflow(true, "testCheckNoOverflow_success", 0, 0);
282   }
283 
testCheckNoOverflow_failure()284   public void testCheckNoOverflow_failure() {
285     try {
286       MathPreconditions.checkNoOverflow(false, "testCheckNoOverflow_failure", 0, 0);
287       fail();
288     } catch (ArithmeticException expected) {
289       assertThat(expected).hasMessageThat().contains("testCheckNoOverflow_failure(0, 0)");
290     }
291   }
292 
testNulls()293   public void testNulls() {
294     /*
295      * Don't bother testing. All non-primitive parameters are used only to construct error messages.
296      * We never want to pass null for them, so we haven't annotated them to say that null is
297      * allowed. But at the same time, it seems wasteful to bother inserting the checkNotNull calls
298      * that NullPointerTester wants.
299      *
300      * (This empty method disables the automatic null testing provided by PackageSanityTests.)
301      */
302   }
303 }
304