Skip to content

Commit e3a7990

Browse files
committed
test(zerobucket): add tests for lazy state, equality, invalid input, singleton
1 parent c6b0271 commit e3a7990

File tree

1 file changed

+49
-26
lines changed

1 file changed

+49
-26
lines changed

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

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,40 +21,63 @@
2121

2222
package org.elasticsearch.exponentialhistogram;
2323

24-
import static org.hamcrest.Matchers.equalTo;
24+
import org.junit.Test;
2525

26-
public class ZeroBucketTests extends ExponentialHistogramTestCase {
26+
import static org.junit.Assert.*;
2727

28-
public void testMinimalBucketHasZeroThreshold() {
29-
assertThat(ZeroBucket.minimalWithCount(42).zeroThreshold(), equalTo(0.0));
30-
}
28+
public class ZeroBucketTests {
3129

32-
public void testExactThresholdPreserved() {
33-
ZeroBucket bucket = new ZeroBucket(3.0, 10);
34-
assertThat(bucket.zeroThreshold(), equalTo(3.0));
30+
@Test
31+
public void testFromThresholdLazyIndex() {
32+
ZeroBucket z = ZeroBucket.fromThreshold(1.25d, 5L);
33+
assertTrue(z.isThresholdComputed());
34+
assertFalse(z.isIndexComputed());
35+
assertEquals(1.25d, z.zeroThreshold(), 0.0);
36+
z.index(); // compute index
37+
assertTrue(z.isIndexComputed());
3538
}
3639

37-
public void testMergingPreservesExactThreshold() {
38-
ZeroBucket bucketA = new ZeroBucket(3.0, 10);
39-
ZeroBucket bucketB = new ZeroBucket(3.5, 20);
40-
ZeroBucket merged = bucketA.merge(bucketB);
41-
assertThat(merged.zeroThreshold(), equalTo(3.5));
42-
assertThat(merged.count(), equalTo(30L));
40+
@Test
41+
public void testFromIndexLazyThreshold() {
42+
ZeroBucket z = ZeroBucket.fromIndexAndScale(42L, ExponentialHistogram.MAX_SCALE, 3L);
43+
assertTrue(z.isIndexComputed());
44+
assertFalse(z.isThresholdComputed());
45+
double thr = z.zeroThreshold();
46+
assertTrue(thr >= 0.0);
47+
assertTrue(z.isThresholdComputed());
4348
}
4449

45-
public void testBucketCollapsingPreservesExactThreshold() {
46-
FixedCapacityExponentialHistogram histo = createAutoReleasedHistogram(2);
47-
histo.resetBuckets(0);
48-
histo.tryAddBucket(0, 42, true); // bucket [1,2]
49-
50-
ZeroBucket bucketA = new ZeroBucket(3.0, 10);
50+
@Test
51+
public void testEqualityAndHashStable() {
52+
ZeroBucket a = ZeroBucket.fromThreshold(0.6d, 10L);
53+
ZeroBucket b = ZeroBucket.fromThreshold(0.6d, 10L);
54+
assertEquals(a, b);
55+
assertEquals(a.hashCode(), b.hashCode());
56+
a.index(); // force index
57+
assertEquals(a, b);
58+
b.index();
59+
assertEquals(a.hashCode(), b.hashCode());
60+
}
5161

52-
CopyableBucketIterator iterator = histo.positiveBuckets().iterator();
53-
ZeroBucket merged = bucketA.collapseOverlappingBuckets(iterator);
62+
@Test
63+
public void testMinimalEmptySingleton() {
64+
ZeroBucket m1 = ZeroBucket.minimalEmpty();
65+
ZeroBucket m2 = ZeroBucket.minimalEmpty();
66+
assertSame(m1, m2);
67+
assertEquals(0L, m1.count());
68+
assertEquals(0.0, m1.zeroThreshold(), 0.0);
69+
}
5470

55-
assertThat(iterator.hasNext(), equalTo(false));
56-
assertThat(merged.zeroThreshold(), equalTo(3.0));
57-
assertThat(merged.count(), equalTo(52L));
71+
@Test(expected = IllegalArgumentException.class)
72+
public void testInvalidNegativeThreshold() {
73+
ZeroBucket.fromThreshold(-0.01d, 1L);
5874
}
5975

60-
}
76+
@Test
77+
public void testToStringContainsKeyFields() {
78+
ZeroBucket z = ZeroBucket.fromThreshold(0.75d, 2L);
79+
String s = z.toString();
80+
assertTrue(s.contains("scale="));
81+
assertTrue(s.contains("count=2"));
82+
}
83+
}

0 commit comments

Comments
 (0)