This lab will help you learn how to use a simple test harness for Java programs, and you will also practice designing various representations for an abstract datatype (ADT). Part I walks through some sample code to show you how to use a simple testing framework. Parts II and III leave you to your own devices to design the code for a general implementation of Lists and various implementations of a Set datatype.
Begin by creating a new Eclipse project (called Lab7). Then download the following two libraries:
Add them to your Eclipse project:Lab7 in the left-hand pane.Build Path and then choose Add External Archives
public interface ISame {
// is this object the same as the given object?
public boolean same(Object obj);
}
The second is the class SimpleTestHarness. SimpleTestHarness objects contain methods that run test cases, keep track of success and failure, and print out detailed reports. Let's look at a simple class to learn how to use the test harness.
Lab7. Select Import....BookTest class is marked with errors. That's because the test harness expects the Book class to implement the ISame interface (so that it can compare the actual results to expected results), but it currently does not. Implement the ISame interface for the Book class.
BookTest. Open that file and Run it:
BookTest to open it.BookTest and select Run As and choose Java Application. You'll see output in the console window at the bottom. Each test case prints out a title, and then indicates success or failure. It also prints out the expected and actual values.
Important: If your expected and actual values are objects, then you must remember to implement the toString() method in order to see a readable printout! You'll know that you've forgotten to do so if you see text like Book@7fde3 in the results.
Books.
Book htdp = new Book("HtDP", 500);
Book anotherHtdp = new Book("HtDP", 500);
Book htdch = new Book("HtDCH", 300);
These will serve as data for all of the test cases that follow.
Next we see a method definition:
// Run the test suite for the same method
public void testSameMethod() {
SimpleTestHarness sth = new SimpleTestHarness("Test same Method ");
sth.test(this.htdp + " same as " + htdch, false, htdp.same(htdch));
sth.test(this.htdp + " same as the other " + this.anotherHtdp,
true, htdp.same(anotherHtdp));
sth.fullTestReport();
}
The first line of this method creates an instance of the test harness. The constructor consumes a String that serves as the title for this set of tests in the printout of the results. The next two lines are calls to the test method. The contract and purpose of the test method are:
// Record whether or not the actual value matches the expected one.
public void test(String title, boolean expected, boolean actual);
Actually, there are many such test methods. The expected and actual could both be ints, doubles, booleans, or any Objects that implement the ISame interface. Note that doubles are compared by asking if they are sufficiently close to each other, not by simply using ==.
The fourth line uses the fullTestReport method to print out the test results to the Eclipse console.
The last thing to note about this method is the strange return type: void. This indicates that the method does not produce any value at all! This method does its job in a different manner than all of those that we have seen so far. Rather than produce a value, it simply prints out information to the console. For the time being, the only time it's appropriate for you to use void is when writing these test suites.
Once you have written this method, you need to call it from the main method to actually get it to run. Your main method should create a new instance of the BookTest class and then call the testSameMethod method to perform the tests:
public static void main(String[] args) {
BookTest bookTest = new BookTest();
bookTest.testSameMethod();
}
IComparable that extends ISame with one more method. This method should determine whether a given Object is smaller than the object that calls the method. Make your Book class implement this interface (compare Books by number of pages). Create and run some test cases by adding code to BookTest. Be sure that the method is correct now because we will be using it in the next part of the lab.
ISame interface). Your list implementation should support at least four methods:
Again, you will also have to implement the ISame interface in order to build and run test cases. Be prepared to design helper methods in your List data structure from Part II.
You should build three implementations of ISet. They differ in the way that the elements are represented: