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

Range Check

At times we just want to make sure the expected value falls within the given range. The tester supports three variants of such range checks. For every variant the programmer supplies the expected value and the lower and upper bounds for the desired range. Additional arguments may specify whether the bounds are inclusive or exclusive (see below).

The first variant, the method checkNumRange, defines the comparison of arbitrary numerical values, allowing (for example) for mixing double range with int expected values and vice versa:

t.checkNumRange(5, 0, 4.9, "fails");
t.checkNumRange(5, 0, 5.0, false, true, 
      "succeeds: inclusive upper bound");
t.checkNumRange(0.0, 0, 5.0, false, true, 
      "fails: exclusive lower bound");

The second variant the method checkRange accepts the actual value (and the desired lower and upper bounds) of the type that implements the Comparable interface. All wrapper classes for the primitive types implement this interface, but require that the argument matches the invoking wrapper class. That means, for example, that we can use this variant to compare the actual int value within int bounds, but need to use the checkNumRange variant if the types are mixed.

The third variant, the method checkRange, accepts three values of arbitrary type <T> together with an Comparator<T> object that allows us to compare the actual value with the given lower and upper bounds.

In all three variants, by default, the test uses the inclusive lower bound and the exclusive upper bound . The user may supply two additional boolean values that determine whether the lower bound and the upper bound should be inclusive.


The following example illustrates the use of the checkRange tests:

  public void testReply(Tester t){
    
    String aaa = "aaa";
    String abc1 = "abc";
    String abc2 = "abc";
    String bcd = "bcd";
    String cde = "cde";
    String cde2 = "cde";
    String def = "def";
    t.checkRange(aaa, abc1, cde,     
          "fails: aaa is below low: [abc cde)");
    t.checkRange(abc2, abc1, cde, 
           "abc is equal low: [abc cde)");
    t.checkRange(abc2, abc1, cde, false, true, 
           "abc is equal low: [abc cde)");
    t.checkRange(bcd, abc1, cde, 
           "bcd is within range: [abc cde)");
    t.checkRange(cde, abc1, cde, 
           "fails: cde equals high: [abc cde)");
    t.checkRange(cde, abc1, cde, true, true, 
           "succeedss: cde equals high: [abc cde)");
    t.checkRange(def, abc1, cde, 
           "fails def is above high: [abc cde)");

    Book p = new Book("Pearl", "JS");
    Book c = new Book("Cosmos", "CS");
    Book h = new Book("Hamlet", "WS");

     t.checkRange(h, c, p, 
                         new Comparator<Book>(){
                         public int compare(Book b1, Book b2){
                             return b1.title.compareTo(b2.title);
                         }}, "Hamlet between Cosmos and Pearl");

  }

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