floating point - Does JUnit or Hamcrest have tolerance assertions? -
i want assertion actual value within either fixed +/- value of expected value or percent +/- value of expected value.
while googling noticed nunit had nice syntax :
assert.that( 5.5, is.equalto( 5 ).within(0.075); assert.that( 5.5, is.equalto( 5 ).within(1.5).percent; does junit or hamcrest have similar can use? if not, there nice way express behavior?
the short reply - yes.
old fashioned org.junit.assert has method assertequals(java.lang.string message, float expected, float actual, float delta), , bunch of similar method doubles, overloaded variants wihout message, , similar implementations of assertarrayequals.
if want utilize hamcrest, closeto matcher you're looking for.
edit: address question in comments percentages - there isn't out-of-box matcher this, can gimmy-rig dividing two, , making sure between desired ratio , inverse. take illustration op:
float expected = 5.0; float actual = 5.5 float ratio = 1.0075; float inverse = 1/ratio; float div = actual/expected; assertthat(div, allof(greaterthan(inverse), lessthan(ratio))); it's bit (well, lot) clunky, should trick.
junit floating-point hamcrest
No comments:
Post a Comment