1313
1414import java .util .Random ;
1515
16+ import static org .elasticsearch .exponentialhistogram .ExponentialHistogram .MAX_INDEX ;
17+ import static org .elasticsearch .exponentialhistogram .ExponentialHistogram .MAX_SCALE ;
18+ import static org .elasticsearch .exponentialhistogram .ExponentialHistogram .MIN_INDEX ;
1619import static org .elasticsearch .exponentialhistogram .ExponentialHistogramUtils .adjustScale ;
1720import static org .elasticsearch .exponentialhistogram .ExponentialHistogramUtils .compareLowerBoundaries ;
1821import static org .elasticsearch .exponentialhistogram .ExponentialHistogramUtils .getLowerBucketBoundary ;
1922import static org .elasticsearch .exponentialhistogram .ExponentialHistogramUtils .getMaximumScaleIncrease ;
2023import static org .elasticsearch .exponentialhistogram .ExponentialHistogramUtils .getUpperBucketBoundary ;
2124import static org .hamcrest .Matchers .equalTo ;
25+ import static org .hamcrest .Matchers .greaterThan ;
26+ import static org .hamcrest .Matchers .lessThan ;
2227import static org .hamcrest .Matchers .lessThanOrEqualTo ;
2328
2429public class ExponentialHistogramUtilsTests extends ESTestCase {
2530
2631 public void testMaxValue () {
27- assertThat (getMaximumScaleIncrease (Long . MAX_VALUE ), equalTo (0 ));
28- assertThat (getMaximumScaleIncrease (Long . MAX_VALUE >> 1 ), equalTo (1 ));
32+ assertThat (getMaximumScaleIncrease (MAX_INDEX ), equalTo (0 ));
33+ assertThat (getMaximumScaleIncrease (MAX_INDEX >> 1 ), equalTo (1 ));
2934
30- assertThat (adjustScale (Long .MAX_VALUE , 0 ), equalTo (Long .MAX_VALUE ));
31- assertThat (adjustScale (Long .MAX_VALUE >> 1 , 1 ), equalTo (Long .MAX_VALUE - 1 ));
32- assertThat (adjustScale (Long .MAX_VALUE >> 2 , 2 ), equalTo ((Long .MAX_VALUE & ~3 ) + 1 ));
33- assertThat (adjustScale (Long .MAX_VALUE >> 4 , 4 ), equalTo ((Long .MAX_VALUE & ~15 ) + 6 ));
35+ assertThrows (ArithmeticException .class , () -> Math .multiplyExact (MAX_INDEX , 4 ));
3436 }
3537
3638 public void testMinValue () {
37- assertThat (getMaximumScaleIncrease (Long .MIN_VALUE ), equalTo (0 ));
38- assertThat (getMaximumScaleIncrease (Long .MIN_VALUE >> 1 ), equalTo (1 ));
39-
40- assertThat (adjustScale (Long .MIN_VALUE , 0 ), equalTo (Long .MIN_VALUE ));
41- assertThat (adjustScale (Long .MIN_VALUE >> 1 , 1 ), equalTo (Long .MIN_VALUE ));
42- assertThat (adjustScale (Long .MIN_VALUE >> 2 , 2 ), equalTo ((Long .MIN_VALUE & ~3 ) + 1 ));
43- assertThat (adjustScale (Long .MIN_VALUE >> 4 , 4 ), equalTo ((Long .MIN_VALUE & ~15 ) + 6 ));
39+ assertThat (getMaximumScaleIncrease (MIN_INDEX ), equalTo (0 ));
40+ assertThat (getMaximumScaleIncrease (MIN_INDEX >> 1 ), equalTo (1 ));
4441 }
4542
46- public void testRandom () {
43+ public void testRandomIndicesScaleAdjustement () {
4744 Random rnd = new Random (42 );
4845
4946 for (int i = 0 ; i < 100_000 ; i ++) {
50- long index = rnd .nextLong ();
47+ long index = rnd .nextLong () % MAX_INDEX ;
5148 int maxScale = getMaximumScaleIncrease (index );
5249
5350 assertThat (adjustScale (adjustScale (index , maxScale ), -maxScale ), equalTo (index ));
54- assertThrows (ArithmeticException .class , () -> Math .multiplyExact (adjustScale (index , maxScale ), 2 ));
51+ if (index >0 ) {
52+ assertThat (adjustScale (index , maxScale ) *2 , greaterThan (MAX_INDEX ));
53+ } else {
54+ assertThat (adjustScale (index , maxScale ) *2 , lessThan (MIN_INDEX ));
55+
56+ }
5557 }
5658
5759 }
5860
59- public void testRandomComparison () {
61+ public void testRandomBucketBoundaryComparison () {
6062 Random rnd = new Random (42 );
6163
6264 for (int i = 0 ; i < 100_000 ; i ++) {
63- long indexA = rnd .nextLong ();
64- long indexB = rnd .nextLong ();
65- int scaleA = rnd .nextInt () % 40 ;
66- int scaleB = rnd .nextInt () % 40 ;
65+ long indexA = rnd .nextLong () % MAX_INDEX ;
66+ long indexB = rnd .nextLong () % MAX_INDEX ;
67+ int scaleA = rnd .nextInt () % MAX_SCALE ;
68+ int scaleB = rnd .nextInt () % MAX_SCALE ;
6769
6870 double lowerBoundA = getLowerBucketBoundary (indexA , scaleA );
6971 while (Double .isInfinite (lowerBoundA )) {
@@ -77,7 +79,6 @@ public void testRandomComparison() {
7779 }
7880
7981 if (lowerBoundA != lowerBoundB ) {
80- System .out .println ("Comparing " + lowerBoundA + " to " + lowerBoundB );
8182 assertThat (Double .compare (lowerBoundA , lowerBoundB ), equalTo (compareLowerBoundaries (indexA , scaleA , indexB , scaleB )));
8283 }
8384 }
@@ -98,10 +99,9 @@ public void testSaneBucketBoundaries() {
9899 assertThat (getLowerBucketBoundary (0 , 42 ), equalTo (1.0 ));
99100 assertThat (getLowerBucketBoundary (1 , 0 ), equalTo (2.0 ));
100101 assertThat (getLowerBucketBoundary (1 , -1 ), equalTo (4.0 ));
101- assertThat (getLowerBucketBoundary (1 , -2 ), equalTo (16.0 ));
102102
103- double limit1 = getLowerBucketBoundary (Long . MAX_VALUE - 1 , 56 );
104- double limit2 = getLowerBucketBoundary (Long . MAX_VALUE , 56 );
103+ double limit1 = getLowerBucketBoundary (MIN_INDEX , MAX_SCALE );
104+ double limit2 = getLowerBucketBoundary (MIN_INDEX , MAX_SCALE );
105105 assertThat (limit1 , lessThanOrEqualTo (limit2 ));
106106 }
107107}
0 commit comments