Skip to content

Commit e6924e9

Browse files
committed
Add some tests based on TDigestTest
1 parent 92efdcf commit e6924e9

File tree

1 file changed

+62
-10
lines changed

1 file changed

+62
-10
lines changed

libs/exponential-histogram/src/test/java/org/elasticsearch/exponentialhistogram/QuantileAccuracyTests.java

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,12 @@
2828

2929
import static org.hamcrest.Matchers.closeTo;
3030
import static org.hamcrest.Matchers.lessThan;
31+
import static org.hamcrest.Matchers.notANumber;
3132

3233
public class QuantileAccuracyTests extends ESTestCase {
3334

3435
public static final double[] QUANTILES_TO_TEST = { 0, 0.01, 0.1, 0.25, 0.5, 0.75, 0.9, 0.95, 0.99, 1.0 };
3536

36-
public void testBasicSmall() {
37-
DoubleStream values = IntStream.range(1,10).mapToDouble(Double::valueOf);
38-
testQuantileAccuracy(values.toArray(), 100);
39-
}
40-
4137
public void testUniformDistribution() {
4238
testDistributionQuantileAccuracy(new UniformRealDistribution(new Well19937c(42), 0, 100), 50000, 500);
4339
}
@@ -66,13 +62,69 @@ public void testWeibullDistribution() {
6662
testDistributionQuantileAccuracy(new WeibullDistribution(new Well19937c(42), 2, 5), 50000, 500);
6763
}
6864

65+
public void testBasicSmall() {
66+
DoubleStream values = IntStream.range(1, 10).mapToDouble(Double::valueOf);
67+
double maxError = testQuantileAccuracy(values.toArray(), 100);
68+
assertThat(maxError, lessThan(0.000001));
69+
}
70+
6971
public void testBigJump() {
70-
double[] values = DoubleStream.concat(
71-
IntStream.range(0,18).mapToDouble(Double::valueOf),
72-
DoubleStream.of(1_000_000.0)
73-
).toArray();
72+
double[] values = DoubleStream.concat(IntStream.range(0, 18).mapToDouble(Double::valueOf), DoubleStream.of(1_000_000.0)).toArray();
73+
74+
double maxError = testQuantileAccuracy(values, 500);
75+
assertThat(maxError, lessThan(0.000001));
76+
}
7477

75-
testQuantileAccuracy(values, 500);
78+
public void testExplicitSkewedData() {
79+
double[] data = new double[] {
80+
245,
81+
246,
82+
247.249,
83+
240,
84+
243,
85+
248,
86+
250,
87+
241,
88+
244,
89+
245,
90+
245,
91+
247,
92+
243,
93+
242,
94+
241,
95+
50100,
96+
51246,
97+
52247,
98+
52249,
99+
51240,
100+
53243,
101+
59248,
102+
59250,
103+
57241,
104+
56244,
105+
55245,
106+
56245,
107+
575247,
108+
58243,
109+
51242,
110+
54241 };
111+
112+
double maxError = testQuantileAccuracy(data, data.length / 2);
113+
assertThat(maxError, lessThan(0.007));
114+
}
115+
116+
public void testEmptyHistogram() {
117+
ExponentialHistogram histo = ExponentialHistogramGenerator.createFor();
118+
for (double q : QUANTILES_TO_TEST) {
119+
assertThat(ExponentialHistogramQuantile.getQuantile(histo, q), notANumber());
120+
}
121+
}
122+
123+
public void testSingleValueHistogram() {
124+
ExponentialHistogram histo = ExponentialHistogramGenerator.createFor(42);
125+
for (double q : QUANTILES_TO_TEST) {
126+
assertThat(ExponentialHistogramQuantile.getQuantile(histo, q), closeTo(42, 0.0000001));
127+
}
76128
}
77129

78130
public void testBucketCountImpact() {

0 commit comments

Comments
 (0)