CSE 331: Section 2 — Testing
Task 1
Use the testing heuristics from lecture to write a test suite for the following method:
/**
* Returns the sum of the array
* @param arr an array of integers
* @requires arr is not null
* @returns the sum of the elements
* in arr or 0 if arr is empty
*/
public static int arraySum(int[] arr) {
if (arr == null) {
throw new IllegalArgumentException("");
}
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
@Test
public void testArraySum() {
// Your tests here
}
Task 2
Write JUnit tests using our testing heuristics for the method spec and impl below.
/**
* Removes x from the prefix a[0..n]
* @param a the non-null array to remove from
* @param n the length of the prefix
* @param x the value to remove
* @requires 0 <= n <= a.length
* @modifies a
* @effects if x does not occur in a[0..n], leaves a unchanged; otherwise, if x
* occurs k > 0 times in a[0..n], then remove all k occurrences of x,
* leaving remaining elements in the same relative order as before;
* the new value of elements in a[n-k..n] is unspecified.
* @return n - k
*/
public static int removeValue(int[] a, int n, int x) {
int w = 0;
for (int r = 0; r < n; r++) {
if (a[r] != x) {
a[w] = a[r];
w++;
}
}
return w;
}
We have given you a template for the first test. Feel free to use a similar format for additional tests as well. Include a comment in the space provided above each assertion stating which testing heuristic(s) is being covered.
Your tests do not necessarily need to be purely specification-based (in other words, your tests might test behavior of the method that is not guaranteed by the spec).
@Test public void testRemoveValue() {
int[] arr = {__________________________________________________________________};
// _____________________________________________________________________________
assertEquals(__________________________________________________________________);
assertArrayEquals(_____________________________________________________________);
}
Task 3
/**
* Reads whitespace-separated integers from standard input and prints
* either their sum (default) or their average.
*
* Usage:
* java Sum [options]
*
* Options:
* -a print the average instead of the sum
*
* Input is read from standard input until end of file.
*/
public static void main(String[] args) {
boolean average = false;
if (args.length > 0 && args[0].equals("-a")) {
average = true;
}
Scanner scan = new Scanner(System.in);
int sum = 0;
int count = 0;
while (scan.hasNextInt()) {
sum += scan.nextInt();
count++;
}
if (average) {
System.out.println((double) sum / count);
} else {
System.out.println(sum);
}
}
Part A
Identify at least three distinct jobs that main is performing.
Part B
Rewrite the main method by "wishful thinking". Which 2 or 3 helper methods might you create?
public static void main(String[] args) {
}
Part C
Of the helper methods you listed above, which one would be most valuable to extract if your goal is to make the program easier to unit test? Briefly explain.