Problem 1
Below is code for a set of integers. A set can contain any number of integers. Operations for checking if a given integer is in the set,
if the set is empty, adding an integer to the set, removing an integer from the set, and returning the smallest integer in the set are provided.
public class IntSet {
private int[] elements;
private int size;
public IntSet() {
elements = new int[2]; // allocate an array of integers
size = 0;
}
public boolean contains(int element) {
for (int i = 0; i < size; i++) {
if (elements[i] == element) {
return true;
}
}
return false;
}
public boolean isEmpty() {
return size == 0;
}
public void add(int element) {
if (contains(element)) {
return;
}
if (size == elements.length - 1) {
increaseStore();
}
elements[size++] = element;
}
public void remove(int element) {
for (int i = 0; i < size; i++) {
if (elements[i] == element) {
shift(i);
size--;
}
}
}
public int getMin() {
if (size == 0) {
throw new RuntimeException("Cannot return the minimal element of an empty set");
}
int result = elements[0];
for (int i = 1; i < size; i++) {
if (result > elements[i]) {
result = elements[i];
}
}
return result;
}
private void increaseStore() {
int[] newElements = new int[elements.length * 2];
for (int i = 0; i < size; i++) {
newElements[i] = elements[i];
}
elements = newElements;
}
private void shift(int positionToRemove) {
for (int i = positionToRemove; i < size - 1; i++) {
elements[i] = elements[i + 1];
}
}
}
Give an EQN function for the ASTOOT method; it can be recursive or implementation-based.
Problem 2
Outline a strategy for unit-testing of the IntSet class from Problem 1. You don't have to use specifics of any testing frameworks.
You don't even have to write actual code. Your strategy can simply be in the following format: "to test whether the contains method
behaves correctly, I will populate an IntSet object with several integers and then check if the contains method determines
that all these integers are in the set. I will also check that several integers that were not added to the set are not in the set."
Problem 3
Consider the following declarations of two classes A and B, where B is a subclass of A, and B overrides method g() of A.
class A {
private int x;
private int y;
public A() {
x = 3;
y = -11;
}
public int f() {
return x + 3;
}
public void g(int value) {
x = value;
}
public void h(int value) {
g(value);
y = x * 2;
}
}
class B extends A {
public void g(int value) {
x = value * 2;
}
}
- a) Suppose that for each of the functions f( ), g( ), and h( ) of A, you have a test set that tests that function in separation.
Which of the functions in B need re-testing (note that B contains f( ) and h( ), since it inherits them from A) ?
- b) Suppose that you construct sequences of function calls for class A that have to be called in the order specified. Of the test sequences
- A( ), A.f( ), A.g( a ), A.h( b )
- A( ), A.f( ), A.g( a ), A.h( b ), A.g( c )
- A( ), A.g( a ), A.f( ), A.h( b ), A.g( c )
- A( ), A.g( a ), A.h( b )
which ones are equivalent, i.e. create the same state of the object as some other sequences in this set?
(Assume that all parameters represent some constants and same letters are used for equal constants.)
Problem 4
Consider the following simple method (you are right, it does not do anything useful):
void method(int i, int j) {
int k = i + 2 * j;
int m;
while (k > j) {
if ((i > 2) && (j < 5)) {
m = 1;
}
else {
m = 0;
}
k--;
}
}
- a) Draw a control flow graph for this method.
- b) Come up with a set of test cases (in terms of the parameters for this function) that cover all nodes in the control flow graph.
- c) Come up with a set of test cases (in terms of the parameters for this function) that cover all edges in the contol flow graph.
- d) Come up with a set of test cases (in terms of the parameters for this function) that provide hidden path coverage.
- e) The number of independent paths in a program can be discovered by computing the cyclomatic complexity (McCabe 1976) of the program flow graph.
The cyclomatic complexity, CC, of any connected graph G may be computed according to the following formula:
- CC(G) = Number(edges) - Number(nodes) + 2
Compute the cyclomatic complexity for the program flow graph in part a.