1919
2020import static java .lang .Math .multiplyExact ;
2121import static org .elasticsearch .common .unit .ByteSizeUnit .BYTES ;
22+ import static org .elasticsearch .common .unit .ByteSizeUnit .GB ;
2223import static org .elasticsearch .common .unit .ByteSizeUnit .KB ;
24+ import static org .elasticsearch .common .unit .ByteSizeUnit .MB ;
2325import static org .elasticsearch .common .unit .ByteSizeUnit .PB ;
26+ import static org .elasticsearch .common .unit .ByteSizeUnit .TB ;
2427import static org .elasticsearch .common .unit .ByteSizeValue .format2Decimals ;
2528import static org .hamcrest .Matchers .containsString ;
2629import static org .hamcrest .Matchers .equalTo ;
@@ -32,27 +35,27 @@ public void testActualPeta() {
3235 }
3336
3437 public void testActualTera () {
35- MatcherAssert .assertThat (ByteSizeValue .of (4 , ByteSizeUnit . TB ).getBytes (), equalTo (4398046511104L ));
38+ MatcherAssert .assertThat (ByteSizeValue .of (4 , TB ).getBytes (), equalTo (4398046511104L ));
3639 }
3740
3841 public void testActual () {
39- MatcherAssert .assertThat (ByteSizeValue .of (4 , ByteSizeUnit . GB ).getBytes (), equalTo (4294967296L ));
42+ MatcherAssert .assertThat (ByteSizeValue .of (4 , GB ).getBytes (), equalTo (4294967296L ));
4043 }
4144
4245 public void testSimple () {
4346 assertThat (ByteSizeUnit .BYTES .toBytes (10 ), is (ByteSizeValue .of (10 , ByteSizeUnit .BYTES ).getBytes ()));
4447 assertThat (ByteSizeUnit .KB .toKB (10 ), is (ByteSizeValue .of (10 , ByteSizeUnit .KB ).getKb ()));
45- assertThat (ByteSizeUnit . MB .toMB (10 ), is (ByteSizeValue .of (10 , ByteSizeUnit . MB ).getMb ()));
46- assertThat (ByteSizeUnit . GB .toGB (10 ), is (ByteSizeValue .of (10 , ByteSizeUnit . GB ).getGb ()));
47- assertThat (ByteSizeUnit . TB .toTB (10 ), is (ByteSizeValue .of (10 , ByteSizeUnit . TB ).getTb ()));
48+ assertThat (MB .toMB (10 ), is (ByteSizeValue .of (10 , MB ).getMb ()));
49+ assertThat (GB .toGB (10 ), is (ByteSizeValue .of (10 , GB ).getGb ()));
50+ assertThat (TB .toTB (10 ), is (ByteSizeValue .of (10 , TB ).getTb ()));
4851 assertThat (PB .toPB (10 ), is (ByteSizeValue .of (10 , PB ).getPb ()));
4952 }
5053
5154 public void testToIntBytes () {
5255 assertThat (ByteSizeUnit .BYTES .toIntBytes (4 ), equalTo (4 ));
5356 assertThat (ByteSizeUnit .KB .toIntBytes (4 ), equalTo (4096 ));
5457 assertThat (
55- expectThrows (AssertionError .class , () -> ByteSizeUnit . GB .toIntBytes (4 )).getMessage (),
58+ expectThrows (AssertionError .class , () -> GB .toIntBytes (4 )).getMessage (),
5659 containsString ("could not convert [4 GB] to an int" )
5760 );
5861 }
@@ -69,13 +72,35 @@ public void testEquality() {
6972 }
7073
7174 public void testToString () {
72- assertThat ("10b" , is (ByteSizeValue .of (10 , ByteSizeUnit .BYTES ).toString ()));
73- assertThat ("1.5kb" , is (ByteSizeValue .of ((long ) (1024 * 1.5 ), ByteSizeUnit .BYTES ).toString ()));
74- assertThat ("1.5mb" , is (ByteSizeValue .of ((long ) (1024 * 1.5 ), ByteSizeUnit .KB ).toString ()));
75- assertThat ("1.5gb" , is (ByteSizeValue .of ((long ) (1024 * 1.5 ), ByteSizeUnit .MB ).toString ()));
76- assertThat ("1.5tb" , is (ByteSizeValue .of ((long ) (1024 * 1.5 ), ByteSizeUnit .GB ).toString ()));
77- assertThat ("1.5pb" , is (ByteSizeValue .of ((long ) (1024 * 1.5 ), ByteSizeUnit .TB ).toString ()));
78- assertThat ("1536pb" , is (ByteSizeValue .of ((long ) (1024 * 1.5 ), PB ).toString ()));
75+ assertEquals ("10b" , new ByteSizeValue (10 , ByteSizeUnit .BYTES ).toString ());
76+
77+ long bytes = (long ) (0.75 * 1024 );
78+ assertEquals (bytes + "b" , new ByteSizeValue (bytes , BYTES ).toString ());
79+ assertEquals ("0.75kb" , new ByteSizeValue (bytes , KB ).toString ());
80+
81+ bytes *= 1024 ;
82+ assertEquals (bytes + "b" , new ByteSizeValue (bytes , BYTES ).toString ());
83+ assertEquals ("768kb" , new ByteSizeValue (bytes , KB ).toString ());
84+ assertEquals ("0.75mb" , new ByteSizeValue (bytes , MB ).toString ());
85+
86+ bytes *= 1024 ;
87+ assertEquals (bytes + "b" , new ByteSizeValue (bytes , BYTES ).toString ());
88+ assertEquals ("768mb" , new ByteSizeValue (bytes , MB ).toString ());
89+ assertEquals ("0.75gb" , new ByteSizeValue (bytes , GB ).toString ());
90+
91+ bytes *= 1024 ;
92+ assertEquals (bytes + "b" , new ByteSizeValue (bytes , BYTES ).toString ());
93+ assertEquals ("768gb" , new ByteSizeValue (bytes , GB ).toString ());
94+ assertEquals ("0.75tb" , new ByteSizeValue (bytes , TB ).toString ());
95+
96+ bytes *= 1024 ;
97+ assertEquals (bytes + "b" , new ByteSizeValue (bytes , BYTES ).toString ());
98+ assertEquals ("768tb" , new ByteSizeValue (bytes , TB ).toString ());
99+ assertEquals ("0.75pb" , new ByteSizeValue (bytes , PB ).toString ());
100+
101+ bytes *= 1024 ;
102+ assertEquals (bytes + "b" , new ByteSizeValue (bytes , BYTES ).toString ());
103+ assertEquals ("768pb" , new ByteSizeValue (bytes , PB ).toString ());
79104 }
80105
81106 public void testParsing () {
@@ -212,7 +237,7 @@ public void testOutOfRange() {
212237 }
213238
214239 public void testConversionHashCode () {
215- ByteSizeValue firstValue = ByteSizeValue .of (randomIntBetween (0 , Integer .MAX_VALUE ), ByteSizeUnit . GB );
240+ ByteSizeValue firstValue = ByteSizeValue .of (randomIntBetween (0 , Integer .MAX_VALUE ), GB );
216241 ByteSizeValue secondValue = ByteSizeValue .of (firstValue .getBytes (), ByteSizeUnit .BYTES );
217242 assertEquals (firstValue .hashCode (), secondValue .hashCode ());
218243 }
@@ -365,15 +390,15 @@ public void testOfKb() {
365390 }
366391
367392 public void testOfMb () {
368- testOf (ByteSizeUnit . MB , ByteSizeValue ::ofMb );
393+ testOf (MB , ByteSizeValue ::ofMb );
369394 }
370395
371396 public void testOfGb () {
372- testOf (ByteSizeUnit . GB , ByteSizeValue ::ofGb );
397+ testOf (GB , ByteSizeValue ::ofGb );
373398 }
374399
375400 public void testOfTb () {
376- testOf (ByteSizeUnit . TB , ByteSizeValue ::ofTb );
401+ testOf (TB , ByteSizeValue ::ofTb );
377402 }
378403
379404 public void testOfPb () {
@@ -399,23 +424,11 @@ public void testAddition() {
399424 ByteSizeValue .add (ByteSizeValue .of (8 , ByteSizeUnit .KB ), ByteSizeValue .of (4 , ByteSizeUnit .KB )),
400425 is (ByteSizeValue .ofBytes (12288L ))
401426 );
402- assertThat (
403- ByteSizeValue .add (ByteSizeValue .of (8 , ByteSizeUnit .MB ), ByteSizeValue .of (4 , ByteSizeUnit .MB )),
404- is (ByteSizeValue .ofBytes (12582912L ))
405- );
406- assertThat (
407- ByteSizeValue .add (ByteSizeValue .of (8 , ByteSizeUnit .GB ), ByteSizeValue .of (4 , ByteSizeUnit .GB )),
408- is (ByteSizeValue .ofBytes (12884901888L ))
409- );
410- assertThat (
411- ByteSizeValue .add (ByteSizeValue .of (8 , ByteSizeUnit .TB ), ByteSizeValue .of (4 , ByteSizeUnit .TB )),
412- is (ByteSizeValue .ofBytes (13194139533312L ))
413- );
427+ assertThat (ByteSizeValue .add (ByteSizeValue .of (8 , MB ), ByteSizeValue .of (4 , MB )), is (ByteSizeValue .ofBytes (12582912L )));
428+ assertThat (ByteSizeValue .add (ByteSizeValue .of (8 , GB ), ByteSizeValue .of (4 , GB )), is (ByteSizeValue .ofBytes (12884901888L )));
429+ assertThat (ByteSizeValue .add (ByteSizeValue .of (8 , TB ), ByteSizeValue .of (4 , TB )), is (ByteSizeValue .ofBytes (13194139533312L )));
414430 assertThat (ByteSizeValue .add (ByteSizeValue .of (8 , PB ), ByteSizeValue .of (4 , PB )), is (ByteSizeValue .ofBytes (13510798882111488L )));
415- assertThat (
416- ByteSizeValue .add (ByteSizeValue .of (8 , PB ), ByteSizeValue .of (4 , ByteSizeUnit .GB )),
417- is (ByteSizeValue .ofBytes (9007203549708288L ))
418- );
431+ assertThat (ByteSizeValue .add (ByteSizeValue .of (8 , PB ), ByteSizeValue .of (4 , GB )), is (ByteSizeValue .ofBytes (9007203549708288L )));
419432
420433 Exception e = expectThrows (
421434 ArithmeticException .class ,
@@ -444,23 +457,11 @@ public void testSubtraction() {
444457 ByteSizeValue .subtract (ByteSizeValue .of (8 , ByteSizeUnit .KB ), ByteSizeValue .of (4 , ByteSizeUnit .KB )),
445458 is (ByteSizeValue .ofBytes (4096L ))
446459 );
447- assertThat (
448- ByteSizeValue .subtract (ByteSizeValue .of (8 , ByteSizeUnit .MB ), ByteSizeValue .of (4 , ByteSizeUnit .MB )),
449- is (ByteSizeValue .ofBytes (4194304L ))
450- );
451- assertThat (
452- ByteSizeValue .subtract (ByteSizeValue .of (8 , ByteSizeUnit .GB ), ByteSizeValue .of (4 , ByteSizeUnit .GB )),
453- is (ByteSizeValue .ofBytes (4294967296L ))
454- );
455- assertThat (
456- ByteSizeValue .subtract (ByteSizeValue .of (8 , ByteSizeUnit .TB ), ByteSizeValue .of (4 , ByteSizeUnit .TB )),
457- is (ByteSizeValue .ofBytes (4398046511104L ))
458- );
460+ assertThat (ByteSizeValue .subtract (ByteSizeValue .of (8 , MB ), ByteSizeValue .of (4 , MB )), is (ByteSizeValue .ofBytes (4194304L )));
461+ assertThat (ByteSizeValue .subtract (ByteSizeValue .of (8 , GB ), ByteSizeValue .of (4 , GB )), is (ByteSizeValue .ofBytes (4294967296L )));
462+ assertThat (ByteSizeValue .subtract (ByteSizeValue .of (8 , TB ), ByteSizeValue .of (4 , TB )), is (ByteSizeValue .ofBytes (4398046511104L )));
459463 assertThat (ByteSizeValue .subtract (ByteSizeValue .of (8 , PB ), ByteSizeValue .of (4 , PB )), is (ByteSizeValue .ofBytes (4503599627370496L )));
460- assertThat (
461- ByteSizeValue .subtract (ByteSizeValue .of (8 , PB ), ByteSizeValue .of (4 , ByteSizeUnit .GB )),
462- is (ByteSizeValue .ofBytes (9007194959773696L ))
463- );
464+ assertThat (ByteSizeValue .subtract (ByteSizeValue .of (8 , PB ), ByteSizeValue .of (4 , GB )), is (ByteSizeValue .ofBytes (9007194959773696L )));
464465
465466 Exception e = expectThrows (
466467 IllegalArgumentException .class ,
@@ -496,31 +497,19 @@ public void testMinimum() {
496497 ByteSizeValue .min (ByteSizeValue .of (8 , ByteSizeUnit .KB ), ByteSizeValue .of (4 , ByteSizeUnit .KB )),
497498 is (ByteSizeValue .of (4 , ByteSizeUnit .KB ))
498499 );
499- assertThat (
500- ByteSizeValue .min (ByteSizeValue .of (4 , ByteSizeUnit .MB ), ByteSizeValue .of (8 , ByteSizeUnit .MB )),
501- is (ByteSizeValue .of (4 , ByteSizeUnit .MB ))
502- );
503- assertThat (
504- ByteSizeValue .min (ByteSizeValue .of (16 , ByteSizeUnit .GB ), ByteSizeValue .of (15 , ByteSizeUnit .GB )),
505- is (ByteSizeValue .of (15 , ByteSizeUnit .GB ))
506- );
507- assertThat (
508- ByteSizeValue .min (ByteSizeValue .of (90 , ByteSizeUnit .TB ), ByteSizeValue .of (91 , ByteSizeUnit .TB )),
509- is (ByteSizeValue .of (90 , ByteSizeUnit .TB ))
510- );
500+ assertThat (ByteSizeValue .min (ByteSizeValue .of (4 , MB ), ByteSizeValue .of (8 , MB )), is (ByteSizeValue .of (4 , MB )));
501+ assertThat (ByteSizeValue .min (ByteSizeValue .of (16 , GB ), ByteSizeValue .of (15 , GB )), is (ByteSizeValue .of (15 , GB )));
502+ assertThat (ByteSizeValue .min (ByteSizeValue .of (90 , TB ), ByteSizeValue .of (91 , TB )), is (ByteSizeValue .of (90 , TB )));
511503 assertThat (ByteSizeValue .min (ByteSizeValue .of (2 , PB ), ByteSizeValue .of (1 , PB )), is (ByteSizeValue .of (1 , PB )));
512- assertThat (
513- ByteSizeValue .min (ByteSizeValue .of (1 , PB ), ByteSizeValue .of (1 , ByteSizeUnit .GB )),
514- is (ByteSizeValue .of (1 , ByteSizeUnit .GB ))
515- );
504+ assertThat (ByteSizeValue .min (ByteSizeValue .of (1 , PB ), ByteSizeValue .of (1 , GB )), is (ByteSizeValue .of (1 , GB )));
516505
517- ByteSizeValue equalityResult = ByteSizeValue .min (ByteSizeValue .of (1024 , ByteSizeUnit . MB ), ByteSizeValue .of (1 , ByteSizeUnit . GB ));
518- assertThat (equalityResult , is (ByteSizeValue .of (1024 , ByteSizeUnit . MB )));
519- assertThat (equalityResult .preferredUnit , is (ByteSizeUnit . MB ));
506+ ByteSizeValue equalityResult = ByteSizeValue .min (ByteSizeValue .of (1024 , MB ), ByteSizeValue .of (1 , GB ));
507+ assertThat (equalityResult , is (ByteSizeValue .of (1024 , MB )));
508+ assertThat (equalityResult .preferredUnit , is (MB ));
520509
521- equalityResult = ByteSizeValue .min (ByteSizeValue .of (1 , ByteSizeUnit . GB ), ByteSizeValue .of (1024 , ByteSizeUnit . MB ));
522- assertThat (equalityResult , is (ByteSizeValue .of (1 , ByteSizeUnit . GB )));
523- assertThat (equalityResult .preferredUnit , is (ByteSizeUnit . GB ));
510+ equalityResult = ByteSizeValue .min (ByteSizeValue .of (1 , GB ), ByteSizeValue .of (1024 , MB ));
511+ assertThat (equalityResult , is (ByteSizeValue .of (1 , GB )));
512+ assertThat (equalityResult .preferredUnit , is (GB ));
524513
525514 String exceptionMessage = "one of the arguments has -1 bytes" ;
526515 Exception e = expectThrows (IllegalArgumentException .class , () -> ByteSizeValue .min (ByteSizeValue .MINUS_ONE , ByteSizeValue .ONE ));
@@ -564,7 +553,7 @@ public void testFormat2DecimalsExhaustively() {
564553 format2Decimals (wholeUnits * granularity , granularity )
565554 );
566555 for (var percent = 1 ; percent < 100 ; percent ++) {
567- long numerator = wholeUnits + (long ) (percent * 0.01 * granularity );
556+ long numerator = wholeUnits * granularity + (long ) (percent * 0.01 * granularity );
568557 String expected = wholeUnits + percentToDecimal (percent );
569558 assertEquals (
570559 "format2Decimals on " + percent + "% of one " + unit .getSuffix (),
0 commit comments