Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit 70b1d43

Browse files
author
buchholz
committed
Revert add and substract tests, they are correct
1 parent 792d855 commit 70b1d43

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

src/main/java/org/javamoney/moneta/FastMoney.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -368,11 +368,6 @@ private long addExact(long num1, long num2) {
368368
boolean pos = num1>0 && num2 >0;
369369
boolean neg = num1<0 && num2 <0;
370370
long exact = num1 + num2;
371-
372-
// if((num1 ^ num2) >= 0 & (num1 ^ exact) < 0){
373-
// throw new ArithmeticException("Long evaluation overflow.");
374-
// }
375-
376371
if(pos && exact <=0){
377372
throw new ArithmeticException("Long evaluation positive overflow.");
378373
}
@@ -481,7 +476,7 @@ private long multiplyExact(long num1, long num2) {
481476

482477
long exact = num1 * num2;
483478

484-
// very expensive - this check is only executed in special cases
479+
// very expensive - this check is only executed in edge cases
485480
// zero check is not needed, see above
486481
if( exact / num1 != num2 ) {
487482
throw new ArithmeticException("overflow");
@@ -529,14 +524,14 @@ private long subtractExact(long num1, long num2) {
529524
if(num1==num2){
530525
return 0;
531526
}
532-
// boolean pos = num1>num2;
527+
boolean pos = num1>num2;
533528
long exact = num1 - num2;
534-
// if(pos && exact <=0){
535-
// throw new ArithmeticException("Long evaluation negative overflow.");
536-
// }
537-
// if(!pos && exact >=0){
538-
// throw new ArithmeticException("Long evaluation positive overflow.");
539-
// }
529+
if(pos && exact <=0){
530+
throw new ArithmeticException("Long evaluation negative overflow.");
531+
}
532+
if(!pos && exact >=0){
533+
throw new ArithmeticException("Long evaluation positive overflow.");
534+
}
540535
return exact;
541536
}
542537

src/test/java/org/javamoney/moneta/FastMoneyTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,11 @@ public void testAdd(){
290290
assertNotNull(moneyResult);
291291
assertEquals(11d, moneyResult.getNumber().doubleValue(), 0d);
292292

293+
// 87978089321359 + 4866358678300 = 92844447999659 > 92233720368547.75807
294+
293295
// This example produce a false positive in the old version
294-
money1 = FastMoney.of(44474249632057L, EURO);
295-
money2 = FastMoney.of(72913073429160L, EURO);
296+
money1 = FastMoney.of(87978089321359L, EURO);
297+
money2 = FastMoney.of(4866358678300L, EURO);
296298

297299
try {
298300
moneyResult = money1.add(money2);
@@ -301,7 +303,7 @@ public void testAdd(){
301303
// should happen
302304
}
303305

304-
// check greates 92233720368547.75807 value
306+
// check greates FM 92233720368547.75807 value
305307
long fastMoneyMax = 92233720368547L;
306308
FastMoney money3 = FastMoney.of(fastMoneyMax, "CHF");
307309

0 commit comments

Comments
 (0)