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 void setUp() {
51 }
52
53
54
55
56 public static Test suite() {
57 TestSuite suite = new TestSuite(CertifiedDataTest.class);
58 suite.setName("Certified Tests");
59 return suite;
60 }
61
62
63
64
65
66 public void testSummaryStatistics() throws Exception {
67 SummaryStatistics u = new SummaryStatistics();
68 loadStats("data/PiDigits.txt", u);
69 assertEquals("PiDigits: std", std, u.getStandardDeviation(), 1E-13);
70 assertEquals("PiDigits: mean", mean, u.getMean(), 1E-13);
71
72 loadStats("data/Mavro.txt", u);
73 assertEquals("Mavro: std", std, u.getStandardDeviation(), 1E-14);
74 assertEquals("Mavro: mean", mean, u.getMean(), 1E-14);
75
76 loadStats("data/Michelso.txt", u);
77 assertEquals("Michelso: std", std, u.getStandardDeviation(), 1E-13);
78 assertEquals("Michelso: mean", mean, u.getMean(), 1E-13);
79
80 loadStats("data/NumAcc1.txt", u);
81 assertEquals("NumAcc1: std", std, u.getStandardDeviation(), 1E-14);
82 assertEquals("NumAcc1: mean", mean, u.getMean(), 1E-14);
83
84 loadStats("data/NumAcc2.txt", u);
85 assertEquals("NumAcc2: std", std, u.getStandardDeviation(), 1E-14);
86 assertEquals("NumAcc2: mean", mean, u.getMean(), 1E-14);
87 }
88
89
90
91
92
93 public void testDescriptiveStatistics() throws Exception {
94
95 DescriptiveStatistics u = new DescriptiveStatistics();
96
97 loadStats("data/PiDigits.txt", u);
98 assertEquals("PiDigits: std", std, u.getStandardDeviation(), 1E-14);
99 assertEquals("PiDigits: mean", mean, u.getMean(), 1E-14);
100
101 loadStats("data/Mavro.txt", u);
102 assertEquals("Mavro: std", std, u.getStandardDeviation(), 1E-14);
103 assertEquals("Mavro: mean", mean, u.getMean(), 1E-14);
104
105 loadStats("data/Michelso.txt", u);
106 assertEquals("Michelso: std", std, u.getStandardDeviation(), 1E-14);
107 assertEquals("Michelso: mean", mean, u.getMean(), 1E-14);
108
109 loadStats("data/NumAcc1.txt", u);
110 assertEquals("NumAcc1: std", std, u.getStandardDeviation(), 1E-14);
111 assertEquals("NumAcc1: mean", mean, u.getMean(), 1E-14);
112
113 loadStats("data/NumAcc2.txt", u);
114 assertEquals("NumAcc2: std", std, u.getStandardDeviation(), 1E-14);
115 assertEquals("NumAcc2: mean", mean, u.getMean(), 1E-14);
116 }
117
118
119
120
121
122
123 private void loadStats(String resource, Object u) throws Exception {
124
125 DescriptiveStatistics d = null;
126 SummaryStatistics s = null;
127 if (u instanceof DescriptiveStatistics) {
128 d = (DescriptiveStatistics) u;
129 } else {
130 s = (SummaryStatistics) u;
131 }
132 u.getClass().getDeclaredMethod(
133 "clear", new Class[]{}).invoke(u, new Object[]{});
134 mean = Double.NaN;
135 std = Double.NaN;
136
137 BufferedReader in =
138 new BufferedReader(
139 new InputStreamReader(
140 CertifiedDataTest.class.getResourceAsStream(resource)));
141
142 String line = null;
143
144 for (int j = 0; j < 60; j++) {
145 line = in.readLine();
146 if (j == 40) {
147 mean =
148 Double.parseDouble(
149 line.substring(line.lastIndexOf(":") + 1).trim());
150 }
151 if (j == 41) {
152 std =
153 Double.parseDouble(
154 line.substring(line.lastIndexOf(":") + 1).trim());
155 }
156 }
157
158 line = in.readLine();
159
160 while (line != null) {
161 if (d != null) {
162 d.addValue(Double.parseDouble(line.trim()));
163 } else {
164 s.addValue(Double.parseDouble(line.trim()));
165 }
166 line = in.readLine();
167 }
168
169 in.close();
170 }
171 }