1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.apache.commons.math.stat.descriptive;
15
16 import junit.framework.Test;
17 import junit.framework.TestSuite;
18
19 import org.apache.commons.math.stat.descriptive.rank.Percentile;
20
21
22
23
24
25
26
27
28
29 public final class DescriptiveStatisticsTest extends DescriptiveStatisticsAbstractTest {
30
31 public DescriptiveStatisticsTest(String name) {
32 super(name);
33 }
34
35 public static Test suite() {
36 TestSuite suite = new TestSuite(DescriptiveStatisticsTest.class);
37 suite.setName("DescriptiveStatistics Tests");
38 return suite;
39 }
40
41 protected DescriptiveStatistics createDescriptiveStatistics() {
42 return new DescriptiveStatistics();
43 }
44
45 public void testSetterInjection() throws Exception {
46 DescriptiveStatistics stats = new DescriptiveStatistics();
47 stats.addValue(1);
48 stats.addValue(3);
49 assertEquals(2, stats.getMean(), 1E-10);
50
51 stats.setMeanImpl(new deepMean());
52 assertEquals(42, stats.getMean(), 1E-10);
53 }
54
55 public void testPercentileSetter() throws Exception {
56 DescriptiveStatistics stats = new DescriptiveStatistics();
57 stats.addValue(1);
58 stats.addValue(2);
59 stats.addValue(3);
60 assertEquals(2, stats.getPercentile(50.0), 1E-10);
61
62
63 stats.setPercentileImpl(new goodPercentile());
64 assertEquals(2, stats.getPercentile(50.0), 1E-10);
65
66
67 stats.setPercentileImpl(new subPercentile());
68 assertEquals(10.0, stats.getPercentile(10.0), 1E-10);
69
70
71 try {
72 stats.setPercentileImpl(new badPercentile());
73 fail("Expecting IllegalArgumentException");
74 } catch (IllegalArgumentException ex) {
75
76 }
77 }
78
79
80
81
82
83
84 static class deepMean implements UnivariateStatistic {
85 private static final long serialVersionUID = 9108665370122541953L;
86
87 public double evaluate(double[] values, int begin, int length) {
88 return 42;
89 }
90
91 public double evaluate(double[] values) {
92 return 42;
93 }
94 }
95
96
97
98
99 static class goodPercentile implements UnivariateStatistic {
100 private static final long serialVersionUID = 801005145532790795L;
101 private Percentile percentile = new Percentile();
102 public void setQuantile(double quantile) {
103 percentile.setQuantile(quantile);
104 }
105 public double evaluate(double[] values, int begin, int length) {
106 return percentile.evaluate(values, begin, length);
107 }
108 public double evaluate(double[] values) {
109 return percentile.evaluate(values);
110 }
111 }
112
113
114
115
116
117 static class subPercentile extends Percentile {
118 public double evaluate(double[] values, int begin, int length) {
119 return getQuantile();
120 }
121 public double evaluate(double[] values) {
122 return getQuantile();
123 }
124 private static final long serialVersionUID = 8040701391045914979L;
125 }
126
127
128
129
130 static class badPercentile implements UnivariateStatistic {
131 private static final long serialVersionUID = -707437653388052183L;
132 private Percentile percentile = new Percentile();
133 public double evaluate(double[] values, int begin, int length) {
134 return percentile.evaluate(values, begin, length);
135 }
136 public double evaluate(double[] values) {
137 return percentile.evaluate(values);
138 }
139 }
140 }