Range Check

Tests to determine whether the data value is in the range [low, high) using Comparable objects of given Comaparator.

Overview

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.

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

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. 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 together with a Comparator 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 class ExamplesRangeCheck contains all test cases.

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.

Brief Examples

Here is an example that illustrates such tests.

public class ExamplesRangeCheck {
    public void testPrimitiveRange(Tester t) {

	t.checkRange(3, 3, 5, "Success: int: [3 <= 3 < 5)");
	t.checkRange(3, 3, 5, false, true, "Should fail: int: (3 < 3 <= 5]");

	short s3 = 3;
	short s5 = 5;
	t.checkRange(s3, s3, s5, "Success: short: [3 <= 3 < 5)");

	byte b3 = 3;
	byte b5 = 5;
	t.checkRange(b3, b3, b5, "Success: byte: [3 <= 3 < 5)");
	t.checkRange(b3, b3, b5, false, true, "Should fail: byte: (3 < 3 <= 5]");

	boolean btrue = true;
	boolean bfalse = false;
	t.checkRange(false, bfalse, btrue, "Success: boolean: [false <= false < true)");
	t.checkRange(true, bfalse, btrue, "Should fail: boolean: [false <= true < true)");

	char p1 = 'p';
	char p2 = 'p';
	char r1 = 'r';
	t.checkRange(r2, p2, r1, "Should fail: char: [p <= r < r)");

	float fminus2 = -2.0f;
	float f3 = 3.0f;
	float f5 = 5.0f;
	t.checkRange(f3, f3, f5, "Success: float: [3.0 <= 3.0 < 5.0)");
	t.checkRange(fminus2, f3, f5, "Should fail: float: [3.0 <= -2.0 < 5.0)");

	String abc1 = "abc";
	String abc2 = "abc";
	String cde = "cde";
	t.checkRange(abc2, abc1, cde, "Success: String: [abc <= abc < cde)");
    }

    public void testNumRange(Tester t) {
	t.checkNumRange(1.0, 1, 2.0, "Success: Num range: [1 <= 1.0 < 2.0)");
	t.checkNumRange(2, 1.0, 2.0, "Should fail: Num range: [1.0 <= 2 < 2.0]");
    }

    Person jon20 = new Person("Jon", 20);
    Person ann10 = new Person("Ann", 10);
    Person ken40 = new Person("Ken", 40);
    public void testRangeComparable(Tester t) {

	t.checkRange(jon20, jon20, ken40,
	"Success: RangeComparable: hits the lower bound");
	t.checkRange(ann10, jon20, ken40,
	"Should fail: RangeComparable: below the lower bound");
    }

    public void testRangeComparator(Tester t) {
	Comparator comp = new PersonComparator();

	t.checkRange(jon20, jon20, ken40, comp,
	"Success: RangeComparator: hits the lower bound");
	t.checkRange(ann10, jon20, ken40, comp,
	"Should fail: RangeComparator: below the lower bound");

	Book p = new Book("Pearl", null, 1930);
	Book c = new Book("Cosmos", null, 1960);
	Book h = new Book("Hamlet", null, 1600);

	t.checkRange(   // assert that
	    h,      // "Hamlet" is
	    c,      // between "Cosmos"
	    p,      // and "Pearl"
	    new Comparator() {  // according to alphabetical order
	public int compare(Book b1, Book b2){
	    return b1.title.compareTo(b2.title);
	}}, "Hamlet between Cosmos and Pearl");
    }
}

back

Last updated: April 1, 2011 10:41 am