public class StringToInteger {
public static void main(String[] args) {
// Driver
System.out.println(stringToInteger("234"));
System.out.println(stringToInteger("-234"));
System.out.println(stringToInteger2("234"));
System.out.println(stringToInteger2("-234"));
}
private static int stringToInteger(String numberString){
// Stub - to be filled in to form the actual method
}
}public class StringToInteger {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(stringToInteger("234"));
System.out.println(stringToInteger("-234"));
System.out.println(stringToInteger2("234"));
System.out.println(stringToInteger2("-234"));
}
private static int stringToInteger(String numberString){
/**
* Uses horner's rule
* Multiplies each successive digit by 10 and adds it to the value of the previous digit
*/
char[] strArray = numberString.toCharArray();
int integerValue = 0;
int charValue;
boolean isNegative = false;
int start = 0;
if (strArray[0] == '-'){
isNegative = true;
start = 1;
}
while (start < strArray.length){
//beware of infinite loops - always increment your counter
charValue = strArray[start] - '0';
integerValue = integerValue * 10 + charValue;
start = start + 1;
}
if (isNegative){
integerValue = -integerValue;
}
return integerValue;
}
}These methods will serve as guidelines for you as you design test cases.
If a player lands on a property owned by other players, he or she needs to pay the rent. If the player does not have enough money, he or she is out of the game. If the property is not owned by any players and the player has enough money buying the property, he or she may buy the property with the price associated with the property.
Logical conditions include if-then, if-then-else, selection, or loops.
Using this flow graph, we can compute the number of independent paths through the code.
Count the number of conditionals/predicates and add 1. Six independent paths through the code
We need to write a test case to ensure that each of these paths is tested at least once. This is the lower bound.
class AmazingGrace {
public int howSweetIsTheSound(String sound){
// calculate how sweet the sound is
return 1;
}
public boolean savedAWretch(Person person){
// Did amazing grace save this wretch
return true;
}
}class AmazingGraceTest {
@Test
public void testHowSweetIsTheSound(){
String sound = "xasxasdsaxs";
AmazingGrace amazingGrace = new AmazingGrace();
assertEquals(amazingGrace.howSweetIsTheSound(sound), 1);
}
@Test
public void testSavedaWretch(){
Person person = new Person();
AmazingGrace amazingGrace = new AmazingGrace();
assertEquals(amazingGrace.savedAWretch(person), true);
}
}100% method coverage since all methods are tested by the test cases.
int foo(int a, int b, int c, int d, float e){
float e;
if (a == 0){
return 0;
}
int x = 0;
if ((a == b) OR ((c == d) AND goNuclear(a)){
x = 1;
}
e = 1/x;
return e;
}Test case 1: foo(0, 0, 0, 0, 0)
Test case 2: foo(1, 1, 1, 1, 1)
100% statement coverage required two test cases
int foo(int a, int b, int c, int d, float e){
float e;
if (a == 0){ // DECISION POINT 1
return 0;
}
int x = 0;
if ((a == b) OR ((c == d) AND goNuclear(a)){ // DECISION POINT 2
x = 1;
}
e = 1/x;
return e;
}For decision/branch coverage, we evaluate an entire Boolean expression as one true-or-false predicate.
int foo(int a, int b, int c, int d, float e){
float e;
if (a == 0){ // DECISION POINT 1
return 0;
}
int x = 0;
if ((a == b) OR ((c == d) AND goNuclear(a)){
// three subexpresions
// goNuclear returns FALSE if passed a value greater than 1
x = 1;
}
e = 1/x;
return e;
}