Created
April 7, 2022 12:26
-
-
Save rjlutz/32694b0c30eaa276d006d7acb2cdd34a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MathStuff { | |
public int timesTwo(int n) { | |
if (n >= Integer.MAX_VALUE) | |
throw new IllegalArgumentException("Value can't be bigger than" + Integer.MAX_VALUE/2); | |
if (n <= Integer.MIN_VALUE) | |
throw new IllegalArgumentException("Value can't be less than" + Integer.MIN_VALUE/2); | |
int product = 2 * n; | |
return product; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MathStuffTest { | |
MathStuff stuff; | |
@Before | |
public void setUp() throws Exception { | |
stuff = new MathStuff(); | |
} | |
@After | |
public void tearDown() throws Exception { | |
} | |
@Test | |
public void twoTimesTwo() { | |
int result = stuff.timesTwo(2); | |
assertEquals(4,result); | |
} | |
@Test | |
public void zeroTimesTwo() { | |
int result = stuff.timesTwo(0); | |
assertEquals(0,result); | |
} | |
@Test | |
public void negTwentyTimesTwo() { | |
int result = stuff.timesTwo(-20); | |
assertEquals(-40,result); | |
} | |
@Test | |
public void wayBigTimesTwo() { | |
try { | |
stuff.timesTwo(Integer.MAX_VALUE); | |
} catch (IllegalArgumentException iae) { | |
System.out.println("Success"); | |
} catch (Exception e) { | |
fail("Test Failed!"); | |
} | |
} | |
@Test | |
public void wayNegativeTimesTwo() { | |
try { | |
stuff.timesTwo(Integer.MAX_VALUE); | |
} catch (IllegalArgumentException iae) { | |
//System.out.println("Success"); | |
} catch (Exception e) { | |
fail("Test Failed!"); | |
} | |
} | |
@Test | |
public void wayNegativeTimesTwo() { | |
boolean caught = false; | |
try { | |
stuff.timesTwo(Integer.MAX_VALUE); | |
} catch (IllegalArgumentException iae) { | |
caught = true; | |
} | |
if (!caught) | |
fail("Test Failed!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment