Skip to content

Commit 81dbdb2

Browse files
authored
[Test] Add unit tests for InternalTDigestPercentilesTests (#24090)
1 parent a973197 commit 81dbdb2

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.search.aggregations.metrics.percentiles.tdigest;
21+
22+
import org.elasticsearch.common.io.stream.Writeable;
23+
import org.elasticsearch.search.DocValueFormat;
24+
import org.elasticsearch.search.aggregations.InternalAggregationTestCase;
25+
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
26+
27+
import java.util.List;
28+
import java.util.Map;
29+
30+
public class InternalTDigestPercentilesTests extends InternalAggregationTestCase<InternalTDigestPercentiles> {
31+
32+
private final double[] percents = randomPercents();
33+
34+
@Override
35+
protected InternalTDigestPercentiles createTestInstance(String name,
36+
List<PipelineAggregator> pipelineAggregators,
37+
Map<String, Object> metaData) {
38+
boolean keyed = randomBoolean();
39+
DocValueFormat format = DocValueFormat.RAW;
40+
TDigestState state = new TDigestState(100);
41+
42+
int numValues = randomInt(10);
43+
for (int i = 0; i < numValues; ++i) {
44+
state.add(randomDouble() * 100);
45+
}
46+
assertEquals(state.centroidCount(), numValues);
47+
return new InternalTDigestPercentiles(name, percents, state, keyed, format, pipelineAggregators, metaData);
48+
}
49+
50+
@Override
51+
protected void assertReduced(InternalTDigestPercentiles reduced, List<InternalTDigestPercentiles> inputs) {
52+
final TDigestState expectedState = new TDigestState(reduced.state.compression());
53+
54+
long totalCount = 0;
55+
for (InternalTDigestPercentiles input : inputs) {
56+
assertArrayEquals(reduced.keys, input.keys, 0d);
57+
expectedState.add(input.state);
58+
totalCount += input.state.size();
59+
}
60+
61+
assertEquals(totalCount, reduced.state.size());
62+
if (totalCount > 0) {
63+
assertEquals(expectedState.quantile(0), reduced.state.quantile(0), 0d);
64+
assertEquals(expectedState.quantile(1), reduced.state.quantile(1), 0d);
65+
}
66+
}
67+
68+
@Override
69+
protected Writeable.Reader<InternalTDigestPercentiles> instanceReader() {
70+
return InternalTDigestPercentiles::new;
71+
}
72+
73+
private static double[] randomPercents() {
74+
List<Double> randomCdfValues = randomSubsetOf(randomIntBetween(1, 7), 0.01d, 0.05d, 0.25d, 0.50d, 0.75d, 0.95d, 0.99d);
75+
double[] percents = new double[randomCdfValues.size()];
76+
for (int i = 0; i < randomCdfValues.size(); i++) {
77+
percents[i] = randomCdfValues.get(i);
78+
}
79+
return percents;
80+
}
81+
}

0 commit comments

Comments
 (0)