|
9 | 9 |
|
10 | 10 | package org.elasticsearch.common.unit; |
11 | 11 |
|
12 | | -import com.carrotsearch.randomizedtesting.annotations.Repeat; |
13 | | - |
14 | 12 | import org.elasticsearch.ElasticsearchParseException; |
15 | 13 | import org.elasticsearch.common.io.stream.Writeable.Reader; |
16 | 14 | import org.elasticsearch.test.AbstractWireSerializingTestCase; |
|
32 | 30 | import static org.hamcrest.Matchers.equalTo; |
33 | 31 | import static org.hamcrest.Matchers.is; |
34 | 32 |
|
35 | | -@Repeat(iterations = 1000) |
36 | 33 | public class ByteSizeValueTests extends AbstractWireSerializingTestCase<ByteSizeValue> { |
37 | 34 | public void testActualPeta() { |
38 | 35 | MatcherAssert.assertThat(ByteSizeValue.of(4, PB).getBytes(), equalTo(4503599627370496L)); |
@@ -569,12 +566,34 @@ private static String percentToDecimal(int percent) { |
569 | 566 | } |
570 | 567 |
|
571 | 568 | public void testWithAutomaticUnit() { |
572 | | - long kibi = 1024; |
573 | | - long mebi = kibi * kibi; |
574 | | - assertEquals(new ByteSizeValue(kibi-1, BYTES), ByteSizeValue.withAutomaticUnit(kibi-1)); |
575 | | - assertEquals(new ByteSizeValue(kibi, KB), ByteSizeValue.withAutomaticUnit(kibi)); |
576 | | - assertEquals(new ByteSizeValue(kibi+1, BYTES), ByteSizeValue.withAutomaticUnit(kibi+1)); |
577 | | - assertEquals(new ByteSizeValue(mebi-kibi, KB), ByteSizeValue.withAutomaticUnit(mebi-kibi)); |
| 569 | + for (var unit : ByteSizeUnit.values()) { |
| 570 | + if (unit == BYTES) { |
| 571 | + continue; |
| 572 | + } |
| 573 | + long bytes = unit.toBytes(1); |
| 574 | + for (int numFourths = 1; numFourths <= 8; numFourths++) { |
| 575 | + checkAutomaticUnitWithCornerCases(bytes * numFourths / 4, unit); |
| 576 | + } |
| 577 | + } |
| 578 | + } |
| 579 | + |
| 580 | + private void checkAutomaticUnitWithCornerCases(long sizeInBytes, ByteSizeUnit expectedUnit) { |
| 581 | + assertEquals( |
| 582 | + sizeInBytes + " should use " + expectedUnit, |
| 583 | + new ByteSizeValue(sizeInBytes, expectedUnit), |
| 584 | + ByteSizeValue.withAutomaticUnit(sizeInBytes) |
| 585 | + ); |
| 586 | + // Plus or minus one byte, it should revert to bytes |
| 587 | + assertEquals( |
| 588 | + sizeInBytes - 1 + " should use " + BYTES, |
| 589 | + new ByteSizeValue(sizeInBytes - 1, BYTES), |
| 590 | + ByteSizeValue.withAutomaticUnit(sizeInBytes - 1) |
| 591 | + ); |
| 592 | + assertEquals( |
| 593 | + sizeInBytes + 1 + " should use " + BYTES, |
| 594 | + new ByteSizeValue(sizeInBytes + 1, BYTES), |
| 595 | + ByteSizeValue.withAutomaticUnit(sizeInBytes + 1) |
| 596 | + ); |
578 | 597 | } |
579 | 598 |
|
580 | 599 | @Override |
|
0 commit comments