1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.hipparchus.stat.data;
24
25 import org.hipparchus.UnitTestUtils;
26 import org.hipparchus.stat.descriptive.DescriptiveStatistics;
27 import org.hipparchus.stat.descriptive.StreamingStatistics;
28 import org.junit.jupiter.api.AfterEach;
29 import org.junit.jupiter.api.BeforeEach;
30 import org.junit.jupiter.api.Test;
31
32 import java.io.BufferedReader;
33 import java.io.IOException;
34 import java.io.InputStreamReader;
35 import java.lang.reflect.InvocationTargetException;
36 import java.lang.reflect.Method;
37 import java.net.URL;
38 import java.util.HashMap;
39 import java.util.Map;
40
41 import static org.junit.jupiter.api.Assertions.fail;
42
43
44
45 public abstract class CertifiedDataAbstractTest {
46
47 private DescriptiveStatistics descriptives;
48
49 private StreamingStatistics summaries;
50
51 private Map<String, Double> certifiedValues;
52
53 @BeforeEach
54 public void setUp() throws IOException {
55 descriptives = new DescriptiveStatistics();
56 summaries = new StreamingStatistics();
57 certifiedValues = new HashMap<String, Double>();
58
59 loadData();
60 }
61
62 private void loadData() throws IOException {
63 BufferedReader in = null;
64
65 try {
66 URL resourceURL = getClass().getClassLoader().getResource(getResourceName());
67 in = new BufferedReader(new InputStreamReader(resourceURL.openStream()));
68
69 String line = in.readLine();
70 while (line != null) {
71
72
73
74
75 line = line.trim();
76
77
78 if (!("".equals(line) || line.startsWith("#"))) {
79 int n = line.indexOf('=');
80 if (n == -1) {
81
82 double value = Double.parseDouble(line);
83 descriptives.addValue(value);
84 summaries.addValue(value);
85 } else {
86
87 String name = line.substring(0, n).trim();
88 String valueString = line.substring(n + 1).trim();
89 Double value = Double.valueOf(valueString);
90 certifiedValues.put(name, value);
91 }
92 }
93 line = in.readLine();
94 }
95 } finally {
96 if (in != null) {
97 in.close();
98 }
99 }
100 }
101
102 protected abstract String getResourceName();
103
104 protected double getMaximumAbsoluteError() {
105 return 1.0e-5;
106 }
107
108 @AfterEach
109 public void tearDown() {
110 descriptives.clear();
111 descriptives = null;
112
113 summaries.clear();
114 summaries = null;
115
116 certifiedValues.clear();
117 certifiedValues = null;
118 }
119
120 @Test
121 public void testCertifiedValues() {
122 for (String name : certifiedValues.keySet()) {
123 Double expectedValue = certifiedValues.get(name);
124
125 Double summariesValue = getProperty(summaries, name);
126 if (summariesValue != null) {
127 UnitTestUtils.customAssertEquals("summary value for " + name + " is incorrect.",
128 summariesValue.doubleValue(), expectedValue.doubleValue(),
129 getMaximumAbsoluteError());
130 }
131
132 Double descriptivesValue = getProperty(descriptives, name);
133 if (descriptivesValue != null) {
134 UnitTestUtils.customAssertEquals("descriptive value for " + name + " is incorrect.",
135 descriptivesValue.doubleValue(), expectedValue.doubleValue(),
136 getMaximumAbsoluteError());
137 }
138 }
139 }
140
141
142 protected Double getProperty(Object bean, String name) {
143 try {
144
145 String prop = "get" + name.substring(0,1).toUpperCase() + name.substring(1);
146 Method meth = bean.getClass().getMethod(prop, new Class[0]);
147 meth.setAccessible(true);
148 Object property = meth.invoke(bean, new Object[0]);
149 if (meth.getReturnType().equals(Double.TYPE)) {
150 return (Double) property;
151 } else if (meth.getReturnType().equals(Long.TYPE)) {
152 return Double.valueOf(((Long) property).doubleValue());
153 } else {
154 fail("wrong type: " + meth.getReturnType().getName());
155 }
156 } catch (NoSuchMethodException nsme) {
157
158 } catch (InvocationTargetException ite) {
159 fail(ite.getMessage());
160 } catch (IllegalAccessException iae) {
161 fail(iae.getMessage());
162 }
163 return null;
164 }
165 }