From efd1d51440f9a69e804666febbcea686ae7e143e Mon Sep 17 00:00:00 2001 From: Vali Tuguran Date: Mon, 10 Oct 2022 02:57:44 +0300 Subject: [PATCH] BAEL-5733 Infinity in Java (#12843) * Add deep vs shallow copy Java. * Refactor sheep names. * BAEL-5733 Add infinity in java test. * Removed test article code. * BAEL-5733 Refactor unit test classname. --- core-java-modules/core-java-lang-5/README.md | 1 + .../infinity/DoubleInfinityUnitTest.java | 81 +++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 core-java-modules/core-java-lang-5/src/test/java/com/baeldung/infinity/DoubleInfinityUnitTest.java diff --git a/core-java-modules/core-java-lang-5/README.md b/core-java-modules/core-java-lang-5/README.md index de76f63411..d62dff7037 100644 --- a/core-java-modules/core-java-lang-5/README.md +++ b/core-java-modules/core-java-lang-5/README.md @@ -8,3 +8,4 @@ This module contains articles about core features in the Java language - [Advantages and Disadvantages of Using Java Wildcard Imports](https://www.baeldung.com/java-wildcard-imports) - [Toggle a Boolean Variable in Java](https://www.baeldung.com/java-toggle-boolean) - [Handle Classes With the Same Name in Java](https://www.baeldung.com/java-classes-same-name) +- [Infinity in Java](https://www.baeldung.com/java-infinity) diff --git a/core-java-modules/core-java-lang-5/src/test/java/com/baeldung/infinity/DoubleInfinityUnitTest.java b/core-java-modules/core-java-lang-5/src/test/java/com/baeldung/infinity/DoubleInfinityUnitTest.java new file mode 100644 index 0000000000..79416c713e --- /dev/null +++ b/core-java-modules/core-java-lang-5/src/test/java/com/baeldung/infinity/DoubleInfinityUnitTest.java @@ -0,0 +1,81 @@ +package com.baeldung.infinity; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class DoubleInfinityUnitTest { + + @Test + void givenInfinities_whenOperatingWithThem_thenNaNOrInfinity() { + Double positiveInfinity = Double.POSITIVE_INFINITY; + Double negativeInfinity = Double.NEGATIVE_INFINITY; + + assertEquals(Double.NaN, (positiveInfinity + negativeInfinity)); + assertEquals(Double.NaN, (positiveInfinity / negativeInfinity)); + assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity - negativeInfinity)); + assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity - positiveInfinity)); + assertEquals(Double.NEGATIVE_INFINITY, (positiveInfinity * negativeInfinity)); + } + + @Test + void givenInfinityAndPositiveNumber_whenOperatingWithThem_thenInfinity() { + Double positiveInfinity = Double.POSITIVE_INFINITY; + Double negativeInfinity = Double.NEGATIVE_INFINITY; + double positiveNumber = 10.0; + + assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity + positiveNumber)); + assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity + positiveNumber)); + + assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity - positiveNumber)); + assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity - positiveNumber)); + + assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity * positiveNumber)); + assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity * positiveNumber)); + + assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity / positiveNumber)); + assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity / positiveNumber)); + + assertEquals(Double.NEGATIVE_INFINITY, (positiveNumber - positiveInfinity)); + assertEquals(Double.POSITIVE_INFINITY, (positiveNumber - negativeInfinity)); + + assertEquals(0.0, (positiveNumber / positiveInfinity)); + assertEquals(-0.0, (positiveNumber / negativeInfinity)); + } + + @Test + void givenInfinityAndBegativeNumber_whenOperatingWithThem_thenInfinity() { + Double positiveInfinity = Double.POSITIVE_INFINITY; + Double negativeInfinity = Double.NEGATIVE_INFINITY; + double negativeNumber = -10.0; + + assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity + negativeNumber)); + assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity + negativeNumber)); + + assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity - negativeNumber)); + assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity - negativeNumber)); + + assertEquals(Double.NEGATIVE_INFINITY, (positiveInfinity * negativeNumber)); + assertEquals(Double.POSITIVE_INFINITY, (negativeInfinity * negativeNumber)); + + assertEquals(Double.NEGATIVE_INFINITY, (positiveInfinity / negativeNumber)); + assertEquals(Double.POSITIVE_INFINITY, (negativeInfinity / negativeNumber)); + + assertEquals(Double.NEGATIVE_INFINITY, (negativeNumber - positiveInfinity)); + assertEquals(Double.POSITIVE_INFINITY, (negativeNumber - negativeInfinity)); + + assertEquals(-0.0, (negativeNumber / positiveInfinity)); + assertEquals(0.0, (negativeNumber / negativeInfinity)); + } + + + @Test + void givenRealNumbers_whenDivisionByZero_thenInfinity() { + double d = 1.0; + + assertEquals(Double.POSITIVE_INFINITY, (d / 0.0)); + assertEquals(Double.NEGATIVE_INFINITY, (d / -0.0)); + assertEquals(Double.NEGATIVE_INFINITY, (-d / 0.0)); + assertEquals(Double.POSITIVE_INFINITY, (-d / -0.0)); + } +}