Random Choice

Tests to determine whether the data value is one of or none of a finite collection of options for both the exact and inexact variants.

Overview

At times a method produces one of several random values. As long as the number of possible choices is fixed (and reasonably small) the tester can be used to determine whether the produced value matches one of or none of the possible given values. The comparison of the actual value with each of the possible expected values follows the same rules as the direct comparison of data. The test methods are named checkOneOf, checkNoneOf, checkInexactOneOf and checkInexactNoneOf.

The class ExamplesRandomChoice 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 void testReply(Tester t) {

    double myGrade = 3.001;

    t.checkOneOf("Successs: checkOneOf",
	"Who cares?",
	"Why do you want to know?", "Who cares?", "It does not matter.");

    t.checkOneOf("Should fail: checkOneOf",
	"HI",
	"Why do you want to know?", "Who cares?", "It does not matter.");

    t.checkNoneOf("Success: checkNoneOf",
	"HI",
	"Why do you want to know?", "Who cares?", "It does not matter.");


    t.checkInexactOneOf("Success: checkInexactOneOf",
	0.01,     // the tolerance
	myGrade,  // actual value
	3.333, 3.0, 2.666);

    t.checkInexactNoneOf("Success: checkInexactNoneOf",
	0.01,     // the tolerance
	myGrade,  // actual value
	3.333, 3.5, 2.666);

    t.checkInexactNoneOf("Should fail: checkInexactNoneOf",
	0.01,     // the tolerance
	myGrade,  // actual value
	3.333, 3.0, 2.666);
}

back

Last updated: March 29, 2011 11:11 am