I am trying to run some JUnit Tests on a Pinball game. The game uses a long for scores and ints for the number of balls. I want to do some boundary value analysis, so I am trying to first test the exact boundary of int and long, and then test that boundary + 1. In my code, when I set the value of max_long, I get an error saying it is out of bounds for an int, however it is cast as a long. My question is "How do I get the program to see the value of max_long as a long type rather than type int.
package com.dozingcatsoftware.bouncy;
import junit.framework.TestCase;
public class BoundaryValues extends TestCase {
Field field = new Field();
int max_int = 2147483647;
long max_long = 9223372036854775807;
public BoundaryValues(String name) {
super(name);
}
public void testGetTotalBalls() {
//test number of balls with impractically high number
field.getGameState().setTotalBalls(max_int);
int numballs = field.getGameState().getTotalBalls();
assertTrue("Number balls still ok.", numballs > 1);
}
public void testGetTotalBallsTooHigh() {
/*what we will do here is pass the setTotalBalls function a value just outside
the possible integer value. This should cause the program to crash, because the number
of balls has to be an integer. Allow this example is in a way impractical it is still theoretically
possible that someone could somehow get this many balls in pinball, so the program should still be tested.
*/
field.getGameState().setTotalBalls((max_int + 1));
long numballs = field.getGameState().getTotalBalls();
assertTrue("Number of Balls Too High", numballs == (int)numballs);
}
public void testSetTotalBalls() {
/*can number of balls be set to negative. We should not be able to set the number of balls in pinball
*to negative. this will test whether it is ok or not. if it is not okay a change should be maid so it
*checks for negative values.
*/
field.getGameState().setTotalBalls(-1);
int numballs = field.getGameState().getTotalBalls();
assertTrue("Number Balls Negative", numballs < 0);
}
public void testSetScoreOk() {
field.getGameState().setScore(max_long);
long score = field.getGameState().getScore();
assertTrue("Score should be okay for long", score > 100000000);
}
public void testSetScoreTooHigh() {
field.getGameState().setScore((max_long + 1));
long score = field.getGameState().getScore();
assertTrue("Score should be okay for long", score > 100000000);
}
}
Aucun commentaire:
Enregistrer un commentaire