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

Commit 95e759c

Browse files
author
buchholz
committed
additional add false positive tests
1 parent 3d4b3d5 commit 95e759c

File tree

2 files changed

+44
-23
lines changed

2 files changed

+44
-23
lines changed

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

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -365,15 +365,24 @@ private long addExact(long num1, long num2) {
365365
if(num2==0){
366366
return num1;
367367
}
368-
boolean pos = num1>0 && num2 >0;
369-
boolean neg = num1<0 && num2 <0;
368+
// boolean pos = num1>0 && num2 >0;
369+
// boolean neg = num1<0 && num2 <0;
370+
int lz1 = Long.numberOfLeadingZeros(num1);
371+
int lz2 = Long.numberOfLeadingZeros(num2);
370372
long exact = num1 + num2;
371-
if(pos && exact <=0){
372-
throw new ArithmeticException("Long evaluation positive overflow.");
373-
}
374-
if(neg && exact >=0){
375-
throw new ArithmeticException("Long evaluation negative overflow.");
373+
if(lz1>2 && lz2>2){
374+
return exact;
376375
}
376+
377+
if(exact-num1 != num2){
378+
throw new ArithmeticException("Long evaluation overflow.");
379+
}
380+
// if(pos && exact <=0){
381+
// throw new ArithmeticException("Long evaluation positive overflow.");
382+
// }
383+
// if(neg && exact >=0){
384+
// throw new ArithmeticException("Long evaluation negative overflow.");
385+
// }
377386
return exact;
378387
}
379388

@@ -524,14 +533,14 @@ private long subtractExact(long num1, long num2) {
524533
if(num1==num2){
525534
return 0;
526535
}
527-
boolean pos = num1>num2;
536+
// boolean pos = num1>num2;
528537
long exact = num1 - num2;
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-
}
538+
// if(pos && exact <=0){
539+
// throw new ArithmeticException("Long evaluation negative overflow.");
540+
// }
541+
// if(!pos && exact >=0){
542+
// throw new ArithmeticException("Long evaluation positive overflow.");
543+
// }
535544
return exact;
536545
}
537546

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import javax.money.MonetaryAmount;
2121
import javax.money.MonetaryOperator;
2222
import javax.money.MonetaryQuery;
23+
import javax.money.NumberValue;
2324

2425
import org.junit.Assert;
2526
import org.testng.annotations.Test;
@@ -280,15 +281,26 @@ public void testAdd(){
280281
FastMoney moneyResult = money1.add(money2);
281282
assertNotNull(moneyResult);
282283
assertEquals(11d, moneyResult.getNumber().doubleValue(), 0d);
283-
284-
FastMoney money3 = FastMoney.of(90000000000000L, "CHF");
285-
try {
286-
// the maximum value for FastMoney is 92233720368547.75807 so this should overflow
287-
money3.add(money3);
288-
fail("overflow should raise ArithmeticException");
289-
} catch (ArithmeticException e) {
290-
// should happen
291-
}
284+
285+
// This example produce a false positive
286+
money1 = FastMoney.of(44474249632057L, EURO);
287+
money2 = FastMoney.of(72913073429160L, EURO);
288+
moneyResult = money1.add(money2);
289+
assertNotNull(moneyResult);
290+
assertEquals(money1, moneyResult.subtract(money2));
291+
// Values greater 92233720368547.75807 not allowed, but we got no long overflow
292+
long fastMoneyMax = 92233720368547L;
293+
FastMoney money3 = FastMoney.of(fastMoneyMax, "CHF");
294+
money1=money3.add(money3);
295+
assertEquals(money1, money3.multiply(2));
296+
297+
// try {
298+
// // the maximum value for FastMoney is 92233720368547.75807 so this should overflow
299+
// money3.add(money3);
300+
// fail("overflow should raise ArithmeticException");
301+
// } catch (ArithmeticException e) {
302+
// // should happen
303+
// }
292304
}
293305

294306
/**

0 commit comments

Comments
 (0)