Debian Codegraph @

test/func/lib/RangeMatcher.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2008 Steve Ratcliffe
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 or
6 * version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13 /* Create date: 01-Jul-2009 */
14 package func.lib;
15
16 import org.hamcrest.BaseMatcher;
17 import org.hamcrest.Description;
18
19 /**
20 * Test for a range of values around the expected one. This allows
21 * small changes without having to fix the test every time there is a
22 * small change to the output size.
23 *
24 * @author Steve Ratcliffe
25 */
26 public class RangeMatcher extends BaseMatcher<Integer> {
27 private final int minVal;
28 private final int maxVal;
29
30 public RangeMatcher(int size) {
31 this(size, size/20+1);
32 }
33
34 public RangeMatcher(int size, int range) {
35 this.minVal = size - range;
36 this.maxVal = size + range;
37 }
38
39 public boolean matches(Object o) {
40 int other = (Integer) o;
41 if (other > minVal && other < maxVal)
42 return true;
43 else return false;
44 }
45
46 public void describeTo(Description description) {
47 description.appendValueList("between ", " and ", "", minVal, maxVal);
48 }
49 }