Skip to content

Commit 3bc38b9

Browse files
committed
Tests for Special Doubles
Add Tests for passing the special doubles POSITIVE_INFINITY, NEGATIVE_INFINITY and NaN to #multiply and #divide. * multiplying with POSITIVE_INFINITY should throw ArithmeticException because it overflows * multiplying with NEGATIVE_INFINITY should throw ArithmeticException because it overflows * multiplying with NaN should throw ArithmeticException * dividing by POSITIVE_INFINITY should return 0 * dividing by NEGATIVE_INFINITY should return 0 * dividing by NaN should throw ArithmeticException For FastMoney: * multiplying with POSITIVE_INFINITY returns the biggest possible FastMoney because Math.round rounds POSITIVE_INFINITY to Long.MAX_VALUE * multiplying with NEGATIVE_INFINITY returns the smallest possible FastMoney because Math.round rounds NEGATIVE_INFINITY to Long.MIN_VALUE * dividing by NaN returns 0 because Math.round rounds NaN to 0 For Money: * multiplying with any of the three throws NumberFormatException because their string representation can not be parsed * dividing by any of the three throws NumberFormatException because their string representation can not be parsed This commit contains failing tests. There are still issues untested related to using Math.round in FastMoney.multiply. For example multiplying 90000000000000L with 90000000000000L will overflow and throw ArithmeticException. However multiplying 90000000000000L * 90000000000000d will be rounded to Long.MAX_VALUE.
1 parent 9001e98 commit 3bc38b9

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed

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

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,20 @@
2020
import javax.money.*;
2121

2222
import java.io.*;
23+
import java.lang.invoke.MethodHandles;
2324
import java.math.BigDecimal;
2425
import java.math.BigInteger;
26+
import java.util.logging.Level;
27+
import java.util.logging.Logger;
2528

2629
import static org.testng.Assert.*;
2730

2831
/**
2932
* @author Anatole
3033
*/
3134
public class FastMoneyTest{
35+
36+
private static final Logger LOG = Logger.getLogger(MethodHandles.lookup().lookupClass().getName());
3237

3338
private static final BigDecimal TEN = new BigDecimal(10.0d);
3439
protected static final CurrencyUnit EURO = MonetaryCurrencies.getCurrency("EUR");
@@ -408,6 +413,123 @@ public void testMultiplyDouble(){
408413
FastMoney m = FastMoney.of(100, "CHF");
409414
assertEquals(FastMoney.of(new BigDecimal("50.0"), "CHF"), m.multiply(0.5));
410415
}
416+
417+
/**
418+
* Test method for {@link org.javamoney.moneta.FastMoney#multiply(double)}.
419+
*/
420+
@Test
421+
public void testMultiplyDoublePositiveInfinity(){
422+
FastMoney m = FastMoney.of(new BigDecimal("50.0"), "USD");
423+
try {
424+
m.multiply(Double.POSITIVE_INFINITY);
425+
fail("multiplying with POSITIVE_INFINITY should fail");
426+
} catch (ArithmeticException e) {
427+
LOG.log(Level.FINE, "multiplying with POSITIVE_INFINITY fails as expected", e);
428+
}
429+
}
430+
431+
/**
432+
* Test method for {@link org.javamoney.moneta.FastMoney#multiply(double)}.
433+
*/
434+
@Test
435+
public void testMultiplyDoubleNegativeInfinity(){
436+
FastMoney m = FastMoney.of(new BigDecimal("50.0"), "USD");
437+
try {
438+
m.multiply(Double.NEGATIVE_INFINITY);
439+
fail("multiplying with NEGATIVE_INFINITY should fail");
440+
} catch (ArithmeticException e) {
441+
LOG.log(Level.FINE, "multiplying with NEGATIVE_INFINITY fails as expected", e);
442+
}
443+
}
444+
445+
/**
446+
* Test method for {@link org.javamoney.moneta.FastMoney#multiply(double)}.
447+
*/
448+
@Test
449+
public void testMultiplyDoubleNaN(){
450+
FastMoney m = FastMoney.of(new BigDecimal("50.0"), "USD");
451+
try {
452+
m.multiply(Double.NaN);
453+
fail("multiplying with NaN should fail");
454+
} catch (ArithmeticException e) {
455+
LOG.log(Level.FINE, "multiplying with NaN fails as expected", e);
456+
}
457+
}
458+
459+
/**
460+
* Test method for {@link org.javamoney.moneta.FastMoney#multiply(Number)}.
461+
*/
462+
@Test
463+
public void testMultiplyNumberPositiveInfinity(){
464+
FastMoney m = FastMoney.of(new BigDecimal("50.0"), "USD");
465+
try {
466+
m.multiply(Double.valueOf(Double.POSITIVE_INFINITY));
467+
fail("multiplying with POSITIVE_INFINITY should fail");
468+
} catch (ArithmeticException e) {
469+
LOG.log(Level.FINE, "multiplying with POSITIVE_INFINITY fails as expected", e);
470+
}
471+
}
472+
473+
/**
474+
* Test method for {@link org.javamoney.moneta.FastMoney#multiply(Number)}.
475+
*/
476+
@Test
477+
public void testMultiplyNumberNegativeInfinity(){
478+
FastMoney m = FastMoney.of(new BigDecimal("50.0"), "USD");
479+
try {
480+
m.multiply(Double.valueOf(Double.NEGATIVE_INFINITY));
481+
fail("multiplying with NEGATIVE_INFINITY should fail");
482+
} catch (ArithmeticException e) {
483+
LOG.log(Level.FINE, "multiplying with NEGATIVE_INFINITY fails as expected", e);
484+
}
485+
}
486+
487+
/**
488+
* Test method for {@link org.javamoney.moneta.FastMoney#multiply(Number)}.
489+
*/
490+
@Test
491+
public void testMultiplyNumberNaN(){
492+
FastMoney m = FastMoney.of(new BigDecimal("50.0"), "USD");
493+
try {
494+
m.multiply(Double.valueOf(Double.NaN));
495+
fail("multiplying with NaN should fail");
496+
} catch (ArithmeticException e) {
497+
LOG.log(Level.FINE, "multiplying with NaN fails as expected", e);
498+
}
499+
}
500+
501+
/**
502+
* Test method for {@link org.javamoney.moneta.FastMoney#divide(double)}.
503+
*/
504+
@Test
505+
public void testDivideBadNaN(){
506+
FastMoney m = FastMoney.of(new BigDecimal("50.0"), "USD");
507+
try {
508+
m.divide(Double.NaN);
509+
fail("dividing by NaN should not be allowed");
510+
} catch (ArithmeticException e) {
511+
LOG.log(Level.FINE, "dividing by NaN fails as expected", e);
512+
}
513+
try {
514+
m.divide(Double.valueOf(Double.NaN));
515+
fail("dividing by h NaN should not be allowed");
516+
} catch (ArithmeticException e) {
517+
LOG.log(Level.FINE, "dividing by NaN fails as expected", e);
518+
}
519+
}
520+
521+
/**
522+
* Test method for {@link org.javamoney.moneta.FastMoney#divide(double)}.
523+
*/
524+
@Test
525+
public void testDivideInfinityDoubles(){
526+
double[] values = new double[]{Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY};
527+
FastMoney m = FastMoney.of(new BigDecimal("50.0"), "USD");
528+
for (double d : values) {
529+
assertTrue(m.divide(d).isZero());
530+
assertTrue(m.divide(Double.valueOf(d)).isZero());
531+
}
532+
}
411533

412534
/**
413535
* Test method for {@link org.javamoney.moneta.FastMoney#negate()}.

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,67 @@ public void testMultiplyNumber(){
525525
assertEquals(Money.of(200, "CHF"), m.multiply(2));
526526
assertEquals(Money.of(new BigDecimal("50.0"), "CHF"), m.multiply(0.5));
527527
}
528+
529+
530+
531+
/**
532+
* Test method for {@link org.javamoney.moneta.Money#multiply(double)}.
533+
*/
534+
@Test
535+
public void testMultiplyBadDoubles(){
536+
double[] values = new double[]{
537+
Double.POSITIVE_INFINITY,
538+
Double.NEGATIVE_INFINITY,
539+
Double.NaN};
540+
Money m = Money.of(new BigDecimal("50.0"), "USD");
541+
for (double d : values) {
542+
try {
543+
m.multiply(d);
544+
fail("multiplying with:" + d + "should not be allowed");
545+
} catch (ArithmeticException e) {
546+
// should reach here
547+
}
548+
try {
549+
m.multiply(Double.valueOf(d));
550+
fail("multiplying with:" + d + "should not be allowed");
551+
} catch (ArithmeticException e) {
552+
// should reach here
553+
}
554+
}
555+
}
556+
557+
/**
558+
* Test method for {@link org.javamoney.moneta.Money#divide(double)}.
559+
*/
560+
@Test
561+
public void testDivideBadDoubles(){
562+
Money m = Money.of(new BigDecimal("50.0"), "USD");
563+
try {
564+
m.divide(Double.NaN);
565+
fail("dividing with:NaN should not be allowed");
566+
} catch (ArithmeticException e) {
567+
// should reach here
568+
}
569+
try {
570+
m.divide(Double.valueOf(Double.NaN));
571+
fail("dividing with NaN should not be allowed");
572+
} catch (ArithmeticException e) {
573+
// should reach here
574+
}
575+
}
576+
577+
/**
578+
* Test method for {@link org.javamoney.moneta.Money#divide(double)}.
579+
*/
580+
@Test
581+
public void testDivideInfinityDoubles(){
582+
double[] values = new double[]{Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY};
583+
Money m = Money.of(new BigDecimal("50.0"), "USD");
584+
for (double d : values) {
585+
assertTrue(m.divide(d).isZero());
586+
assertTrue(m.divide(Double.valueOf(d)).isZero());
587+
}
588+
}
528589

529590
/**
530591
* Test method for {@link org.javamoney.moneta.Money#negate()}.

0 commit comments

Comments
 (0)