Changeset 12906 in josm for trunk/test
 Timestamp:
 20170929T10:57:14+02:00 (2 years ago)
 Location:
 trunk/test/unit/org
 Files:

 1 added
 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/test/unit/org/CustomMatchers.java
r11867 r12906 4 4 import java.awt.geom.Point2D; 5 5 import java.util.Collection; 6 import java.util.Locale; 6 7 import java.util.Objects; 7 8 import java.util.function.Predicate; … … 12 13 import org.hamcrest.TypeSafeMatcher; 13 14 import org.junit.Ignore; 15 import org.openstreetmap.josm.data.Bounds; 14 16 import org.openstreetmap.josm.data.coor.EastNorth; 15 17 import org.openstreetmap.josm.data.coor.LatLon; … … 21 23 public final class CustomMatchers { 22 24 25 /** 26 * Error mode, denoting different ways to calculate the error of a number relative to an expected value. 27 */ 28 public enum ErrorMode { 29 /** 30 * absolute error (difference of actual and expected value) 31 */ 32 ABSOLUTE, 33 34 /** 35 * relative error (difference divided by the expected value) 36 */ 37 RELATIVE 38 } 39 23 40 private CustomMatchers() { 24 41 // Hide constructor for utility classes … … 126 143 }; 127 144 } 145 146 /** 147 * Matcher for a {@link Bounds} object 148 * @param expected expected bounds 149 * @param tolerance acceptable deviation (epsilon) 150 * @return Matcher for a {@link Bounds} object 151 */ 152 public static Matcher<Bounds> is(final Bounds expected, double tolerance) { 153 return new TypeSafeMatcher<Bounds>() { 154 @Override 155 public void describeTo(Description description) { 156 description.appendText("is ") 157 .appendValue(expected) 158 .appendText(" (tolarance: " + tolerance + ")"); 159 } 160 161 @Override 162 protected void describeMismatchSafely(Bounds bounds, Description mismatchDescription) { 163 mismatchDescription.appendText("was ").appendValue(bounds); 164 } 165 166 @Override 167 protected boolean matchesSafely(Bounds bounds) { 168 return Math.abs(expected.getMinLon()  bounds.getMinLon()) <= tolerance && 169 Math.abs(expected.getMinLat()  bounds.getMinLat()) <= tolerance && 170 Math.abs(expected.getMaxLon()  bounds.getMaxLon()) <= tolerance && 171 Math.abs(expected.getMaxLat()  bounds.getMaxLat()) <= tolerance; 172 } 173 }; 174 } 175 176 /** 177 * Matcher for a floating point number. 178 * @param expected expected value 179 * @param errorMode the error mode 180 * @param tolerance admissible error 181 * @return Matcher for a floating point number 182 */ 183 public static Matcher<Double> isFP(final double expected, ErrorMode errorMode, double tolerance) { 184 return new TypeSafeMatcher<Double>() { 185 @Override 186 public void describeTo(Description description) { 187 description.appendText("is ") 188 .appendValue(expected) 189 .appendText(" (tolarance") 190 .appendText(errorMode == ErrorMode.RELATIVE ? ", relative:" : ":") 191 .appendText(Double.toString(tolerance)) 192 .appendText(")"); 193 } 194 195 @Override 196 protected void describeMismatchSafely(Double was, Description mismatchDescription) { 197 mismatchDescription.appendText("was ").appendValue(was); 198 if (errorMode == ErrorMode.RELATIVE) { 199 mismatchDescription.appendText(" (actual relative error: ") 200 .appendText(String.format(Locale.US, "%.2e", Math.abs((was  expected) / expected))) 201 .appendText(")"); 202 } 203 } 204 205 @Override 206 protected boolean matchesSafely(Double x) { 207 switch (errorMode) { 208 case ABSOLUTE: 209 return Math.abs(x  expected) <= tolerance; 210 case RELATIVE: 211 return Math.abs((x  expected) / expected) <= tolerance; 212 default: 213 throw new AssertionError(); 214 } 215 } 216 }; 217 } 218 219 /** 220 * Matcher for a floating point number. 221 * @param expected expected value 222 * @param tolerance admissible error (absolute) 223 * @return Matcher for a floating point number 224 */ 225 public static Matcher<Double> isFP(final double expected, double tolerance) { 226 return isFP(expected, ErrorMode.ABSOLUTE, tolerance); 227 } 228 229 /** 230 * Matcher for a floating point number. 231 * @param expected expected value 232 * @return Matcher for a floating point number 233 */ 234 public static Matcher<Double> isFP(final double expected) { 235 return isFP(expected, 1e8); 236 } 128 237 }
Note: See TracChangeset
for help on using the changeset viewer.