Skip to content

Instantly share code, notes, and snippets.

@rjlutz
Created April 7, 2022 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjlutz/32694b0c30eaa276d006d7acb2cdd34a to your computer and use it in GitHub Desktop.
Save rjlutz/32694b0c30eaa276d006d7acb2cdd34a to your computer and use it in GitHub Desktop.
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;
}
}
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