Home | History | Annotate | Download | only in math
      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 com.google.common.annotations.GwtCompatible;
     20 
     21 import junit.framework.TestCase;
     22 
     23 import java.math.BigInteger;
     24 
     25 /**
     26  * Unit tests for {@link MathPreconditions}.
     27  *
     28  * @author Ben Yu
     29  */
     30 @GwtCompatible
     31 public class MathPreconditionsTest extends TestCase {
     32 
     33   public void testCheckPositive_zeroInt() {
     34     try {
     35       MathPreconditions.checkPositive("int", 0);
     36       fail();
     37     } catch (IllegalArgumentException expected) {}
     38   }
     39 
     40   public void testCheckPositive_maxInt() {
     41     MathPreconditions.checkPositive("int", Integer.MAX_VALUE);
     42   }
     43 
     44   public void testCheckPositive_minInt() {
     45     try {
     46       MathPreconditions.checkPositive("int", Integer.MIN_VALUE);
     47       fail();
     48     } catch (IllegalArgumentException expected) {}
     49   }
     50 
     51   public void testCheckPositive_positiveInt() {
     52     MathPreconditions.checkPositive("int", 1);
     53   }
     54 
     55   public void testCheckPositive_negativeInt() {
     56     try {
     57       MathPreconditions.checkPositive("int", -1);
     58       fail();
     59     } catch (IllegalArgumentException expected) {}
     60   }
     61 
     62   public void testCheckPositive_zeroLong() {
     63     try {
     64       MathPreconditions.checkPositive("long", 0L);
     65       fail();
     66     } catch (IllegalArgumentException expected) {}
     67   }
     68 
     69   public void testCheckPositive_maxLong() {
     70     MathPreconditions.checkPositive("long", Long.MAX_VALUE);
     71   }
     72 
     73   public void testCheckPositive_minLong() {
     74     try {
     75       MathPreconditions.checkPositive("long", Long.MIN_VALUE);
     76       fail();
     77     } catch (IllegalArgumentException expected) {}
     78   }
     79 
     80   public void testCheckPositive_positiveLong() {
     81     MathPreconditions.checkPositive("long", 1);
     82   }
     83 
     84   public void testCheckPositive_negativeLong() {
     85     try {
     86       MathPreconditions.checkPositive("long", -1L);
     87       fail();
     88     } catch (IllegalArgumentException expected) {}
     89   }
     90 
     91   public void testCheckPositive_zeroBigInteger() {
     92     try {
     93       MathPreconditions.checkPositive("BigInteger", BigInteger.ZERO);
     94       fail();
     95     } catch (IllegalArgumentException expected) {}
     96   }
     97 
     98   public void testCheckPositive_postiveBigInteger() {
     99     MathPreconditions.checkPositive("BigInteger", BigInteger.ONE);
    100   }
    101 
    102   public void testCheckPositive_negativeBigInteger() {
    103     try {
    104       MathPreconditions.checkPositive("BigInteger", BigInteger.ZERO.negate());
    105       fail();
    106     } catch (IllegalArgumentException expected) {}
    107   }
    108 
    109   public void testCheckNonNegative_zeroInt() {
    110     MathPreconditions.checkNonNegative("int", 0);
    111   }
    112 
    113   public void testCheckNonNegative_maxInt() {
    114     MathPreconditions.checkNonNegative("int", Integer.MAX_VALUE);
    115   }
    116 
    117   public void testCheckNonNegative_minInt() {
    118     try {
    119       MathPreconditions.checkNonNegative("int", Integer.MIN_VALUE);
    120       fail();
    121     } catch (IllegalArgumentException expected) {}
    122   }
    123 
    124   public void testCheckNonNegative_positiveInt() {
    125     MathPreconditions.checkNonNegative("int", 1);
    126   }
    127 
    128   public void testCheckNonNegative_negativeInt() {
    129     try {
    130       MathPreconditions.checkNonNegative("int", -1);
    131       fail();
    132     } catch (IllegalArgumentException expected) {}
    133   }
    134 
    135   public void testCheckNonNegative_zeroLong() {
    136     MathPreconditions.checkNonNegative("long", 0L);
    137   }
    138 
    139   public void testCheckNonNegative_maxLong() {
    140     MathPreconditions.checkNonNegative("long", Long.MAX_VALUE);
    141   }
    142 
    143   public void testCheckNonNegative_minLong() {
    144     try {
    145       MathPreconditions.checkNonNegative("long", Long.MIN_VALUE);
    146       fail();
    147     } catch (IllegalArgumentException expected) {}
    148   }
    149 
    150   public void testCheckNonNegative_positiveLong() {
    151     MathPreconditions.checkNonNegative("long", 1L);
    152   }
    153 
    154   public void testCheckNonNegative_negativeLong() {
    155     try {
    156       MathPreconditions.checkNonNegative("int", -1L);
    157       fail();
    158     } catch (IllegalArgumentException expected) {}
    159   }
    160 
    161   public void testCheckNonNegative_zeroBigInteger() {
    162     MathPreconditions.checkNonNegative("BigInteger", BigInteger.ZERO);
    163   }
    164 
    165   public void testCheckNonNegative_positiveBigInteger() {
    166     MathPreconditions.checkNonNegative("BigInteger", BigInteger.ONE);
    167   }
    168 
    169   public void testCheckNonNegative_negativeBigInteger() {
    170     try {
    171       MathPreconditions.checkNonNegative("int", BigInteger.ONE.negate());
    172       fail();
    173     } catch (IllegalArgumentException expected) {}
    174   }
    175 
    176   public void testCheckNonNegative_zeroFloat() {
    177     MathPreconditions.checkNonNegative("float", 0f);
    178   }
    179 
    180   public void testCheckNonNegative_maxFloat() {
    181     MathPreconditions.checkNonNegative("float", Float.MAX_VALUE);
    182   }
    183 
    184   public void testCheckNonNegative_minFloat() {
    185     MathPreconditions.checkNonNegative("float", Float.MIN_VALUE);
    186   }
    187 
    188   public void testCheckNonNegative_positiveFloat() {
    189     MathPreconditions.checkNonNegative("float", 1f);
    190   }
    191 
    192   public void testCheckNonNegative_negativeFloat() {
    193     try {
    194       MathPreconditions.checkNonNegative("float", -1f);
    195       fail();
    196     } catch (IllegalArgumentException expected) {}
    197   }
    198 
    199   public void testCheckNonNegative_nanFloat() {
    200     try {
    201       MathPreconditions.checkNonNegative("float", Float.NaN);
    202       fail();
    203     } catch (IllegalArgumentException expected) {}
    204   }
    205 
    206   public void testCheckNonNegative_zeroDouble() {
    207     MathPreconditions.checkNonNegative("double", 0d);
    208   }
    209 
    210   public void testCheckNonNegative_maxDouble() {
    211     MathPreconditions.checkNonNegative("double", Double.MAX_VALUE);
    212   }
    213 
    214   public void testCheckNonNegative_minDouble() {
    215     MathPreconditions.checkNonNegative("double", Double.MIN_VALUE);
    216   }
    217 
    218   public void testCheckNonNegative_positiveDouble() {
    219     MathPreconditions.checkNonNegative("double", 1d);
    220   }
    221 
    222   public void testCheckNonNegative_negativeDouble() {
    223     try {
    224       MathPreconditions.checkNonNegative("double", -1d);
    225       fail();
    226     } catch (IllegalArgumentException expected) {}
    227   }
    228 
    229   public void testCheckNonNegative_nanDouble() {
    230     try {
    231       MathPreconditions.checkNonNegative("double", Double.NaN);
    232       fail();
    233     } catch (IllegalArgumentException expected) {}
    234   }
    235 
    236   public void testCheckRoundingUnnnecessary_success() {
    237     MathPreconditions.checkRoundingUnnecessary(true);
    238   }
    239 
    240   public void testCheckRoundingUnnecessary_failure() {
    241     try {
    242       MathPreconditions.checkRoundingUnnecessary(false);
    243       fail();
    244     } catch (ArithmeticException expected) {}
    245   }
    246 
    247   public void testCheckInRange_success() {
    248     MathPreconditions.checkInRange(true);
    249   }
    250 
    251   public void testCheckInRange_failure() {
    252     try {
    253       MathPreconditions.checkInRange(false);
    254       fail();
    255     } catch (ArithmeticException expected) {}
    256   }
    257 
    258   public void testCheckNoOverflow_success() {
    259     MathPreconditions.checkNoOverflow(true);
    260   }
    261 
    262   public void testCheckNoOverflow_failure() {
    263     try {
    264       MathPreconditions.checkNoOverflow(false);
    265       fail();
    266     } catch (ArithmeticException expected) {}
    267   }
    268 }
    269