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

Commit 792d855

Browse files
author
Jochen Buchholz
committed
Remove the new addition test
1 parent 95e759c commit 792d855

File tree

2 files changed

+46
-39
lines changed

2 files changed

+46
-39
lines changed

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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -365,24 +365,20 @@ 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;
370-
int lz1 = Long.numberOfLeadingZeros(num1);
371-
int lz2 = Long.numberOfLeadingZeros(num2);
368+
boolean pos = num1>0 && num2 >0;
369+
boolean neg = num1<0 && num2 <0;
372370
long exact = num1 + num2;
373-
if(lz1>2 && lz2>2){
374-
return exact;
375-
}
371+
372+
// if((num1 ^ num2) >= 0 & (num1 ^ exact) < 0){
373+
// throw new ArithmeticException("Long evaluation overflow.");
374+
// }
376375

377-
if(exact-num1 != num2){
378-
throw new ArithmeticException("Long evaluation overflow.");
376+
if(pos && exact <=0){
377+
throw new ArithmeticException("Long evaluation positive overflow.");
378+
}
379+
if(neg && exact >=0){
380+
throw new ArithmeticException("Long evaluation negative overflow.");
379381
}
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-
// }
386382
return exact;
387383
}
388384

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

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,33 @@
1515
*/
1616
package org.javamoney.moneta;
1717

18+
import static org.testng.Assert.assertEquals;
19+
import static org.testng.Assert.assertFalse;
20+
import static org.testng.Assert.assertNotNull;
21+
import static org.testng.Assert.assertNotSame;
22+
import static org.testng.Assert.assertTrue;
23+
import static org.testng.Assert.fail;
24+
25+
import java.io.ByteArrayInputStream;
26+
import java.io.ByteArrayOutputStream;
27+
import java.io.IOException;
28+
import java.io.ObjectInputStream;
29+
import java.io.ObjectOutputStream;
30+
import java.lang.invoke.MethodHandles;
31+
import java.math.BigDecimal;
32+
import java.math.BigInteger;
33+
import java.util.logging.Level;
34+
import java.util.logging.Logger;
35+
1836
import javax.money.CurrencyUnit;
1937
import javax.money.Monetary;
2038
import javax.money.MonetaryAmount;
2139
import javax.money.MonetaryOperator;
2240
import javax.money.MonetaryQuery;
23-
import javax.money.NumberValue;
2441

2542
import org.junit.Assert;
2643
import org.testng.annotations.Test;
2744

28-
import java.io.*;
29-
import java.lang.invoke.MethodHandles;
30-
import java.math.BigDecimal;
31-
import java.math.BigInteger;
32-
import java.util.logging.Level;
33-
import java.util.logging.Logger;
34-
35-
import static org.testng.Assert.*;
36-
3745
/**
3846
* @author Anatole
3947
*/
@@ -282,25 +290,28 @@ public void testAdd(){
282290
assertNotNull(moneyResult);
283291
assertEquals(11d, moneyResult.getNumber().doubleValue(), 0d);
284292

285-
// This example produce a false positive
293+
// This example produce a false positive in the old version
286294
money1 = FastMoney.of(44474249632057L, EURO);
287295
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
296+
297+
try {
298+
moneyResult = money1.add(money2);
299+
fail("overflow should raise ArithmeticException");
300+
} catch (ArithmeticException e) {
301+
// should happen
302+
}
303+
304+
// check greates 92233720368547.75807 value
292305
long fastMoneyMax = 92233720368547L;
293306
FastMoney money3 = FastMoney.of(fastMoneyMax, "CHF");
294-
money1=money3.add(money3);
295-
assertEquals(money1, money3.multiply(2));
296307

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-
// }
308+
try {
309+
// the maximum value for FastMoney is 92233720368547.75807 so this should overflow
310+
money3.add(money3);
311+
fail("overflow should raise ArithmeticException");
312+
} catch (ArithmeticException e) {
313+
// should happen
314+
}
304315
}
305316

306317
/**

0 commit comments

Comments
 (0)