Hi, this is my first program using JUnit and I am very lost, this is the piece of code that I am running the test on
public class BoundedBag {
// Data members
private int[] contents; // Elements of the BoundedBag.
private int topPosition; // Index of highest used position in contents.
/**
* Default constructor - creates an empty BoundedBag.
*/
public BoundedBag() {
contents = new int[4];
topPosition = -1;
}
public void add(int theValue) {
// test if still room
if (topPosition == 3) {
// no more room
return;
}
// OK still room
topPosition++;
contents[topPosition] = theValue;
}
// returns true if theValue is in the BoundedBag
public boolean contains(int theValue) {
for (int i = 0; i < topPosition; i++) {
if (contents[i] == theValue) {
}
}
return true;
}
// deletes theValue from the BoundedBag
// returns true if theValue was in the Bag,
// otherwise return false.
public boolean remove(int theValue) {
// declare i
for (int i = 0; i < topPosition; i++) {
if (contents[i] == theValue) {
// value found move back the other values
for (int j = i; j < topPosition - 1; j++) {
contents[j] = contents[j + 1];
}
// value found and deleted, return true
return true;
}
}
// value wasn't found
return false;
}
}
Here is the JUnit skeleton
import static java.lang.System.out;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
public class BoundedBagTest {
// Data Members
private BoundedBag empty, singleton, typical, full;
@Before
public void setup() {
empty = new BoundedBag();
singleton = new BoundedBag();
singleton.add(347);
typical = new BoundedBag();
typical.add(33);
typical.add(33);
typical.add(-33);
full = new BoundedBag();
full.add(1);
full.add(2);
full.add(2);
full.add(-999999999);
}
@Test
public void defaultConstructor() {
BoundedBag b0 = new BoundedBag();
out.println("\n*** b0 after default constructor = " + b0);
assertEquals(0, b0.size());
}
@Test
public void add() {
out.println("\n***add");
BoundedBag b1 = new BoundedBag();
b1.add(1);
out.println("b1 = " + b1);
assertEquals(1, b1.size());
b1.add(2);
out.println("b1 = " + b1);
assertEquals(2, b1.size());
b1.add(2);
out.println("b1 = " + b1);
assertEquals(3, b1.size());
b1.add(-999999999);
out.println("b1 = " + b1);
assertEquals(4, b1.size());
}
}
Can someone help explain how this works please