1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math.stat;
18
19 import junit.framework.Test;
20 import junit.framework.TestCase;
21 import junit.framework.TestSuite;
22
23 import java.io.BufferedReader;
24 import java.io.InputStreamReader;
25
26 import org.apache.commons.math.stat.descriptive.SummaryStatistics;
27 import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
28
29
30
31
32
33 public class CertifiedDataTest extends TestCase {
34
35 protected double mean = Double.NaN;
36
37 protected double std = Double.NaN;
38
39
40
41
42
43 public CertifiedDataTest(String name) {
44 super(name);
45 }
46
47
48
49
50 public static Test suite() {
51 TestSuite suite = new TestSuite(CertifiedDataTest.class);
52 suite.setName("Certified Tests");
53 return suite;
54 }
55
56
57
58
59
60 public void testSummaryStatistics() throws Exception {
61 SummaryStatistics u = new SummaryStatistics();
62 loadStats("data/PiDigits.txt", u);
63 assertEquals("PiDigits: std", std, u.getStandardDeviation(), 1E-13);
64 assertEquals("PiDigits: mean", mean, u.getMean(), 1E-13);
65
66 loadStats("data/Mavro.txt", u);
67 assertEquals("Mavro: std", std, u.getStandardDeviation(), 1E-14);
68 assertEquals("Mavro: mean", mean, u.getMean(), 1E-14);
69
70 loadStats("data/Michelso.txt", u);
71 assertEquals("Michelso: std", std, u.getStandardDeviation(), 1E-13);
72 assertEquals("Michelso: mean", mean, u.getMean(), 1E-13);
73
74 loadStats("data/NumAcc1.txt", u);
75 assertEquals("NumAcc1: std", std, u.getStandardDeviation(), 1E-14);
76 assertEquals("NumAcc1: mean", mean, u.getMean(), 1E-14);
77
78 loadStats("data/NumAcc2.txt", u);
79 assertEquals("NumAcc2: std", std, u.getStandardDeviation(), 1E-14);
80 assertEquals("NumAcc2: mean", mean, u.getMean(), 1E-14);
81 }
82
83
84
85
86
87 public void testDescriptiveStatistics() throws Exception {
88
89 DescriptiveStatistics u = new DescriptiveStatistics();
90
91 loadStats("data/PiDigits.txt", u);
92 assertEquals("PiDigits: std", std, u.getStandardDeviation(), 1E-14);
93 assertEquals("PiDigits: mean", mean, u.getMean(), 1E-14);
94
95 loadStats("data/Mavro.txt", u);
96 assertEquals("Mavro: std", std, u.getStandardDeviation(), 1E-14);
97 assertEquals("Mavro: mean", mean, u.getMean(), 1E-14);
98
99 loadStats("data/Michelso.txt", u);
100 assertEquals("Michelso: std", std, u.getStandardDeviation(), 1E-14);
101 assertEquals("Michelso: mean", mean, u.getMean(), 1E-14);
102
103 loadStats("data/NumAcc1.txt", u);
104 assertEquals("NumAcc1: std", std, u.getStandardDeviation(), 1E-14);
105 assertEquals("NumAcc1: mean", mean, u.getMean(), 1E-14);
106
107 loadStats("data/NumAcc2.txt", u);
108 assertEquals("NumAcc2: std", std, u.getStandardDeviation(), 1E-14);
109 assertEquals("NumAcc2: mean", mean, u.getMean(), 1E-14);
110 }
111
112
113
114
115
116
117 private void loadStats(String resource, Object u) throws Exception {
118
119 DescriptiveStatistics d = null;
120 SummaryStatistics s = null;
121 if (u instanceof DescriptiveStatistics) {
122 d = (DescriptiveStatistics) u;
123 } else {
124 s = (SummaryStatistics) u;
125 }
126 u.getClass().getDeclaredMethod(
127 "clear", new Class[]{}).invoke(u, new Object[]{});
128 mean = Double.NaN;
129 std = Double.NaN;
130
131 BufferedReader in =
132 new BufferedReader(
133 new InputStreamReader(
134 CertifiedDataTest.class.getResourceAsStream(resource)));
135
136 String line = null;
137
138 for (int j = 0; j < 60; j++) {
139 line = in.readLine();
140 if (j == 40) {
141 mean =
142 Double.parseDouble(
143 line.substring(line.lastIndexOf(":") + 1).trim());
144 }
145 if (j == 41) {
146 std =
147 Double.parseDouble(
148 line.substring(line.lastIndexOf(":") + 1).trim());
149 }
150 }
151
152 line = in.readLine();
153
154 while (line != null) {
155 if (d != null) {
156 d.addValue(Double.parseDouble(line.trim()));
157 } else {
158 s.addValue(Double.parseDouble(line.trim()));
159 }
160 line = in.readLine();
161 }
162
163 in.close();
164 }
165 }