|
15 | 15 | */ |
16 | 16 | package org.javamoney.moneta; |
17 | 17 |
|
| 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 | + |
18 | 36 | import javax.money.CurrencyUnit; |
19 | 37 | import javax.money.Monetary; |
20 | 38 | import javax.money.MonetaryAmount; |
|
24 | 42 | import org.junit.Assert; |
25 | 43 | import org.testng.annotations.Test; |
26 | 44 |
|
27 | | -import java.io.*; |
28 | | -import java.lang.invoke.MethodHandles; |
29 | | -import java.math.BigDecimal; |
30 | | -import java.math.BigInteger; |
31 | | -import java.util.logging.Level; |
32 | | -import java.util.logging.Logger; |
33 | | - |
34 | | -import static org.testng.Assert.*; |
35 | | - |
36 | 45 | /** |
37 | 46 | * @author Anatole |
38 | 47 | */ |
@@ -281,7 +290,22 @@ public void testAdd(){ |
281 | 290 | assertNotNull(moneyResult); |
282 | 291 | assertEquals(11d, moneyResult.getNumber().doubleValue(), 0d); |
283 | 292 |
|
284 | | - FastMoney money3 = FastMoney.of(90000000000000L, "CHF"); |
| 293 | + // This example create a sum that is to big for fastmoney, it should overflow |
| 294 | + // 87978089321359 + 4866358678300 = 92844447999659 > 92233720368547.75807 |
| 295 | + money1 = FastMoney.of(87978089321359L, EURO); |
| 296 | + money2 = FastMoney.of(4866358678300L, EURO); |
| 297 | + |
| 298 | + try { |
| 299 | + moneyResult = money1.add(money2); |
| 300 | + fail("overflow should raise ArithmeticException"); |
| 301 | + } catch (ArithmeticException e) { |
| 302 | + // should happen |
| 303 | + } |
| 304 | + |
| 305 | + // check greates FM 92233720368547.75807 value |
| 306 | + long fastMoneyMax = 92233720368547L; |
| 307 | + FastMoney money3 = FastMoney.of(fastMoneyMax, "CHF"); |
| 308 | + |
285 | 309 | try { |
286 | 310 | // the maximum value for FastMoney is 92233720368547.75807 so this should overflow |
287 | 311 | money3.add(money3); |
@@ -409,14 +433,29 @@ public void testMultiplyLong(){ |
409 | 433 | assertEquals(FastMoney.of(200, "CHF"), m.multiply(2)); |
410 | 434 | assertEquals(FastMoney.of(new BigDecimal("50.0"), "CHF"), m.multiply(0.5)); |
411 | 435 |
|
| 436 | + // Zero test |
| 437 | + m = FastMoney.of(100, "CHF"); |
| 438 | + assertEquals( m.multiply(0), FastMoney.of(0, "CHF")); |
| 439 | + m = FastMoney.of(0, "CHF"); |
| 440 | + assertEquals( m.multiply(10), FastMoney.of(0, "CHF")); |
| 441 | + |
412 | 442 | try { |
413 | 443 | // the maximum value for FastMoney is 92233720368547.75807 so this should overflow |
414 | 444 | FastMoney.of(90000000000000L, "CHF").multiply(90000000000000L); |
415 | 445 | fail("overflow should raise ArithmeticException"); |
416 | 446 | } catch (ArithmeticException e) { |
417 | 447 | // should happen |
418 | 448 | } |
419 | | - } |
| 449 | + try { |
| 450 | + // the maximum value for FastMoney is 92233720368547.75807 |
| 451 | + // these values are lower, but the overflow detection does not work |
| 452 | + // correct. |
| 453 | + FastMoney.of(-53484567177043L, "CHF").multiply(2178802625L); |
| 454 | + fail("overflow should raise ArithmeticException"); |
| 455 | + } catch (ArithmeticException e) { |
| 456 | + // should happen |
| 457 | + } |
| 458 | + } |
420 | 459 |
|
421 | 460 | /** |
422 | 461 | * Test method for {@link FastMoney#multiply(double)}. |
@@ -562,6 +601,9 @@ public void testNegate(){ |
562 | 601 | } catch (ArithmeticException e) { |
563 | 602 | // should happen |
564 | 603 | } |
| 604 | + |
| 605 | + m = FastMoney.of(0, "CHF"); |
| 606 | + assertEquals(m.negate(), FastMoney.of(0, "CHF")); |
565 | 607 | } |
566 | 608 |
|
567 | 609 | /** |
|
0 commit comments