JavaLib
 
Tester
 
Overview
Basic Types
User Types
Inexact Values
Sets
Iterable
Traversals
Maps
Random Choice
Range Check
Equivalence
ISame
Exceptions
Constructors
Methods
CheckFail
 
Sources
Downloads
 
TSRJ Home
TSRJ Local

Methods

The tester defines the method checkMethod that allows the programmer to test the expected value produced by a method invocation by providing the expected value, the instance that invokes the method, the method name, and the arguments that the method consumes. If the expected object contains inexact value the programmer must use the checkInexactMethod variant of the method.

The examples below illustrate both versions:

public class Song{

  protected String title;
  private int time; // the playing time
  
  public Song(String title, int time){
    this.title=title;
    this.time=time;
  }
  
  // Determine if the total playing time 
  // for this and the two given songs is 10
  public boolean tenMinutesTotal(Song two, Song three){
    return this.time + two.time + three.time == 10;
  }
  
  // Is the playing time for this song shorter than for that song?
  private Song shorter(Song that){
    if (this.time < that.time)
      return this;
    else
      return that;
  }
} 

// Tests:
class Examples{
Song song1 = new Song("title1", 4);
Song song2 = new Song("title2", 2);
Song song1a = new Song("title1", 4);

public void testMethods(Tester t){
  // test this.song1.shorter(this.song2) -- expect song1
  t.checkExpect("Check if you get the shorter song -- should fail",
                this.song1,       // expected value
                this.song1,       // object that invokes the method
                "shorter",        // method name
                this.song2);      // method arguments

  // test song1.tenMinutesTotal(song1a, song2) -- expect true
  t.checkExpect("Check if the total playing time is 10 -- should succeed",
                true                       // expected value
                this.song1,                // object that invokes the method
                "tenMinutesTotal",         // method name
                this.song1a, this.song2);  // method arguments
}}

Here is an example that uses the checkInexactMethod variant of the method:

class CartPtDouble{
  double x;
  double y;

  CartPtDouble(double x, double y){
    this.x = x; this.y = y;
  }

  // compute the distance from this point to the origin
  double distTo0(){
    return Math.sqrt(this.x * this.x + this.y * this.y);
  }
  
  // compute the distance to the given point
  double distTo(CartPtDouble that){
    return Math.sqrt((this.x - that.x) * (this.x - that.x) + 
    		     (this.y - that.y) * (this.y - that.y));
  }
}

class Examples{
  Examples(){}

  void testMethodInvocation(Tester t){
  	t.checkMethod("exact - will succeed", 5.0, this.pt, "distTo0");
  	t.checkInexactMethod("inexact check - success",
  		   	     4.998,
  			     0.001,
  			     this.pt,
  			     "distTo", 
  			     new CartPtDouble(7.0, 7.0));  	
  }
}

Code sources

Here is the complete source code for this test suite.

You can also download the entire souce code as a zip file.

Complete test results are shown here.


last updated on Mon Apr 4 15:26:25 EDT 2011generated with DrRacket