UserInputHandler.java
package midterm2023;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class UserInputHandler {
public static List<String> readUserInput() {
List<String> userData = new ArrayList<>();
System.out.println("Please enter your data below: (send 'bye' to exit) ");
Scanner input = new Scanner(System.in);
while (true) {
String line = input.nextLine();
if ("bye".equalsIgnoreCase(line)) {
break;
}
userData.add(line);
}
return userData;
}
public static void main(String[] args) {
List<String> userData = readUserInput();
System.out.printf("User Input Data:n%s", String.join("n", userData));
}
}
UserInputHandlerUnitTest.java
package midterm2023;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
public class UserInputHandlerUnitTest {
//@Test
void testByMakeUpDataInSystemIn() {
String[] inputLines = new String[]{
"The first line.",
"The second line.",
"bye",
"The last line.",
"anything after 'bye' will be ignored"
};
String[] expectedLines = Arrays.copyOf(inputLines, inputLines.length - 2);
List<String> expected = Arrays.stream(expectedLines).collect(Collectors.toList());
InputStream stdin = System.in;
try {
System.setIn(new ByteArrayInputStream(String.join("n", inputLines).getBytes()));
List<String> actual = UserInputHandler.readUserInput();
assertThat(actual).isEqualTo(expected);
} finally {
System.setIn(stdin);
}
}
}
QuickSort.java
package coen352.ch07;
public class QuickSort {
public static <E extends Comparable<? super E>> void sort(E[] A) {
qsort(A, 0, A.length-1);}
static <E extends Comparable<? super E>>
void qsort(E[] A, int i, int j) { // Quicksort
int pivotindex = findpivot(A, i, j); // Pick a pivot
DSutil.swap(A, pivotindex, j); // Stick pivot at end
// k will be the first position in the right subarray
int k = partition(A, i-1, j, A[j]);
DSutil.swap(A, k, j); // Put pivot in place
if ((k-i) > 1) qsort(A, i, k-1); // Sort left partition
if ((j-k) > 1) qsort(A, k+1, j); // Sort right partition
}
static <E extends Comparable<? super E>>
int partition(E[] A, int l, int r, E pivot) {
do {// Move bounds inward until they meet
while (A[++l].compareTo(pivot)<0);
while ((r!=0) && (A[--r].compareTo(pivot)>0));
DSutil.swap(A, l, r); // Swap out-of-place values
} while (l < r); // Stop when they cross
DSutil.swap(A, l, r); // Reverse last, wasted swap
return l; // Return first position in right partition
}
static <E extends Comparable<? super E>>
int findpivot(E[] A, int i, int j)
{ return (i+j)/2; }
}
In this assignment, we aim to practise white box testing and coverage strategies to produce unit
test cases. Problem 1 (30 MARKS) The UserInputHandler program code is provided in the
attachment. It is a simple function that stores the user's input until a certain terminating token is
typed. The original code applies.
1. UserInputHandler.java
package midterm2023;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class UserInputHandler {
public static List<String> readUserInput() {
List<String> userData = new ArrayList<>();
System.out.println("Please enter your data below: (send 'bye' to exit) ");
Scanner input = new Scanner(System.in);
while (true) {
String line = input.nextLine();
if ("bye".equalsIgnoreCase(line)) {
break;
}
userData.add(line);
}
return userData;
}
public static void main(String[] args) {
List<String> userData = readUserInput();
System.out.printf("User Input Data:n%s", String.join("n", userData));
}
}
UserInputHandlerUnitTest.java
package midterm2023;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
public class UserInputHandlerUnitTest {
//@Test
void testByMakeUpDataInSystemIn() {
String[] inputLines = new String[]{
"The first line.",
"The second line.",
"bye",
"The last line.",
"anything after 'bye' will be ignored"
2. };
String[] expectedLines = Arrays.copyOf(inputLines, inputLines.length - 2);
List<String> expected = Arrays.stream(expectedLines).collect(Collectors.toList());
InputStream stdin = System.in;
try {
System.setIn(new ByteArrayInputStream(String.join("n", inputLines).getBytes()));
List<String> actual = UserInputHandler.readUserInput();
assertThat(actual).isEqualTo(expected);
} finally {
System.setIn(stdin);
}
}
}
QuickSort.java
package coen352.ch07;
public class QuickSort {
public static <E extends Comparable<? super E>> void sort(E[] A) {
qsort(A, 0, A.length-1);}
static <E extends Comparable<? super E>>
void qsort(E[] A, int i, int j) { // Quicksort
int pivotindex = findpivot(A, i, j); // Pick a pivot
DSutil.swap(A, pivotindex, j); // Stick pivot at end
// k will be the first position in the right subarray
int k = partition(A, i-1, j, A[j]);
DSutil.swap(A, k, j); // Put pivot in place
if ((k-i) > 1) qsort(A, i, k-1); // Sort left partition
if ((j-k) > 1) qsort(A, k+1, j); // Sort right partition
}
static <E extends Comparable<? super E>>
int partition(E[] A, int l, int r, E pivot) {
do {// Move bounds inward until they meet
while (A[++l].compareTo(pivot)<0);
while ((r!=0) && (A[--r].compareTo(pivot)>0));
3. DSutil.swap(A, l, r); // Swap out-of-place values
} while (l < r); // Stop when they cross
DSutil.swap(A, l, r); // Reverse last, wasted swap
return l; // Return first position in right partition
}
static <E extends Comparable<? super E>>
int findpivot(E[] A, int i, int j)
{ return (i+j)/2; }
}
In this assignment, we aim to practise white box testing and coverage strategies to produce unit
test cases. Problem 1 (30 MARKS) The UserInputHandler program code is provided in the
attachment. It is a simple function that stores the user's input until a certain terminating token is
typed. The original code applies the token 'bye' to terminate. Now please revise the program to
make the options with token 'bye' or 'quit' or 'exit'. 1.1. Develop CFG of the revised program and
compute the CC value (5 MARKS). 1.2. Based on the CC value, derive the basic paths. (5
MARKS) 1.3. Analyze the program's code structure and estimate the number of test cases needed
for condition coverage and decision coverage respectively. (5MARKS) 1.4. Generate test cases
for 100% basic path coverage, condition coverage and