| Random Choice | |||||||||||||||||||||||||||
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 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 following example illustrates such tests. The test method is named
public void testReply(Tester t){
String s = answers.randomAnswer();
t.checkExpect(s.equals("Why do you want to know?") ||
s.equals("Who cares?") ||
s.equals("It does not matter."));
t.checkOneOf("The test should succeed :)",
s,
"Why do you want to know?",
"Who cares?",
"It does not matter.");
t.checkOneOf("The test should fail :(",
"HI",
"Why do you want to know?",
"Who cares?",
"It does not matter.");
}
In contrast, we also support the test that assures that the actual value if not one of the given selection of possible values. The following example illustrates such test. The test method is named
t.checkNoneOf("The test should succeed :)",
"HI",
"Why do you want to know?",
"Who cares?",
"It does not matter.");
t.checkNoneOf("The test should fail :(",
"Who cares?",
"Why do you want to know?",
"Who cares?",
"It does not matter.");
Inexact variants for both method are also available, though we do not expect them to be used extensively.
double myGrade = 3.001;
t.checkInexactOneOf("The test should succeed :)",
0.01, // the tolerance
myGrade, // actual value
3.333,
3.0,
2.666);
t.checkInexactOneOf("The test should fail :(",
0.01, // the tolerance
myGrade, // actual value
3.333,
3.5,
2.666);
Code sourcesHere 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 2011 | generated with DrRacket |