Skip to content

Commit 99b0c62

Browse files
author
psi
committed
sanity check for StepModelFit.setSteps
added unit test for StepModelFit
1 parent 7edf573 commit 99b0c62

2 files changed

Lines changed: 85 additions & 2 deletions

File tree

androidplot-core/src/main/java/com/androidplot/xy/StepModelFit.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.androidplot.Region;
44

5+
import java.util.Arrays;
6+
57
/**
68
* Subclass of StepModel that chooses from predefined step values. Depending on the currently
79
* displayed range (by value) choose increment so that the number of lines
@@ -24,6 +26,16 @@ public double[] getSteps() {
2426
}
2527

2628
public void setSteps(double[] steps) {
29+
30+
// sanity checks: no null, 0 or negative
31+
if (steps == null || steps.length == 0)
32+
return;
33+
34+
for (double step : steps) {
35+
if (step <= 0.0d)
36+
return;
37+
}
38+
2739
this.steps = steps;
2840
}
2941

@@ -39,8 +51,9 @@ public void setScale(Region scale) {
3951
@Override
4052
public double getValue() {
4153

42-
// no possible increments where supplied (e.g. switched into this mode without calling setSteps(...)
43-
if (steps == null)
54+
// no possible increments where supplied
55+
// or no region defined
56+
if (steps == null || scale == null || !scale.isDefined())
4457
return super.getValue();
4558

4659
double curStep = steps[0];
@@ -60,4 +73,13 @@ public double getValue() {
6073
}
6174
return curStep;
6275
}
76+
77+
@Override
78+
public String toString() {
79+
return "StepModelFit{" +
80+
"steps=" + Arrays.toString(steps) +
81+
", scale=" + scale +
82+
", current stepping=" + getValue() +
83+
'}';
84+
}
6385
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.androidplot.xy;
2+
3+
import com.androidplot.Region;
4+
5+
import org.junit.After;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
9+
import static org.junit.Assert.*;
10+
11+
public class StepModelFitTest {
12+
13+
Region regionSmall = new Region(0,11);
14+
Region regionBig = new Region(-111,420);
15+
Region regionZero = new Region(0, 0);
16+
Region regionUndef = new Region(0, null);
17+
18+
double[] stpSmall = {1,2,5}, stpBig = {1,10,100}, nonsense = {0};
19+
20+
@Before
21+
public void setUp() throws Exception {
22+
23+
}
24+
25+
@After
26+
public void tearDown() throws Exception {
27+
28+
}
29+
30+
@Test
31+
public void getValue() throws Exception {
32+
33+
StepModelFit model = new StepModelFit(regionSmall,stpSmall,3);
34+
35+
assertEquals(5.0, model.getValue(), 0.0);
36+
model.setValue(5.0);
37+
assertEquals(2.0, model.getValue(), 0.0);
38+
model.setValue(7.0);
39+
assertEquals(2.0, model.getValue(), 0.0);
40+
41+
model.setSteps(stpBig);
42+
assertEquals(1.0, model.getValue(), 0.0);
43+
44+
model.setScale(regionBig);
45+
assertEquals(100.0, model.getValue(), 0.0);
46+
model.setValue(1000.0);
47+
assertEquals(1.0, model.getValue(), 0.0);
48+
49+
// bad parameters
50+
model.setSteps(nonsense);
51+
assertArrayEquals(stpBig,model.getSteps(), 0.0);
52+
53+
model.setScale(regionZero);
54+
assertEquals(stpBig[0], model.getValue(), 0.0);
55+
56+
model.setScale(regionUndef);
57+
model.setValue(1.1);
58+
assertEquals(1.1, model.getValue(), 0.0);
59+
}
60+
61+
}

0 commit comments

Comments
 (0)