From 00655048c1bfcc72d62a09282424104c96cf6462 Mon Sep 17 00:00:00 2001 From: mperor Date: Tue, 29 Oct 2024 12:52:07 +0100 Subject: [PATCH] Add data operations tests ... - Operators - Type of expression - Primitive and reference types comparison --- .../data/operation/BasicArithmeticTest.java | 63 ++++++++++++++ .../data/operation/ExpressionTypeTest.java | 30 +++++++ .../java/data/operation/OperatorsTest.java | 83 +++++++++++++++++++ ...mitiveAndReferenceTypesComparisonTest.java | 34 ++++++++ 4 files changed, 210 insertions(+) create mode 100644 basics/src/test/java/pl/mperor/lab/java/data/operation/BasicArithmeticTest.java create mode 100644 basics/src/test/java/pl/mperor/lab/java/data/operation/ExpressionTypeTest.java create mode 100644 basics/src/test/java/pl/mperor/lab/java/data/operation/OperatorsTest.java create mode 100644 basics/src/test/java/pl/mperor/lab/java/data/operation/PrimitiveAndReferenceTypesComparisonTest.java diff --git a/basics/src/test/java/pl/mperor/lab/java/data/operation/BasicArithmeticTest.java b/basics/src/test/java/pl/mperor/lab/java/data/operation/BasicArithmeticTest.java new file mode 100644 index 0000000..e37931d --- /dev/null +++ b/basics/src/test/java/pl/mperor/lab/java/data/operation/BasicArithmeticTest.java @@ -0,0 +1,63 @@ +package pl.mperor.lab.java.data.operation; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.math.BigDecimal; +import java.math.RoundingMode; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class BasicArithmeticTest { + + @Test + public void testDividingDoubleVsInt() { + Assertions.assertTrue(3 == 16 / 5); + Assertions.assertTrue(3.2 == (double) 16 / 5); + } + + @Test + public void testDividingDoubleByZero() { + Assertions.assertEquals(Double.POSITIVE_INFINITY, 3.0 / 0); + Assertions.assertEquals(Double.NEGATIVE_INFINITY, -3.0 / 0); + Assertions.assertEquals(0, 0 / 4.0); + Assertions.assertTrue(Double.isNaN(0 / 0.0)); + } + + @Test + public void testDividingIntegerByZero() { + assertThrows(ArithmeticException.class, () -> { + int _ = 3 / 0; + }); + assertEquals(0, 0 / 4); + assertThrows(ArithmeticException.class, () -> { + int _ = 0 / 0; + }); + } + + @Test + public void testBigDecimalDividing() { + BigDecimal two = BigDecimal.TWO; + BigDecimal three = BigDecimal.valueOf(3); + Assertions.assertThrows(ArithmeticException.class, () -> two.setScale(2).divide(three)); + Assertions.assertEquals(new BigDecimal("0.67"), two.setScale(2).divide(three, RoundingMode.HALF_UP)); + } + + @Test + public void testFloatingPointAccuracy() { + Assertions.assertTrue(0.1 == 1.0 / 10.0); + Assertions.assertEquals(0.1, 0.3 - 0.2, 1e-9); + } + + @Test + public void testMathApi() { + Assertions.assertEquals(5, Math.abs(-5)); + Assertions.assertEquals(1, Math.min(1, 2)); + Assertions.assertEquals(2, Math.max(1, 2)); + Assertions.assertEquals(27, Math.pow(3, 3)); + Assertions.assertEquals(4, Math.sqrt(16)); + Assertions.assertEquals(Double.NaN, Math.sqrt(-1)); + Assertions.assertEquals(3.14, Math.round(Math.PI * 100) / 100.0); + } +} diff --git a/basics/src/test/java/pl/mperor/lab/java/data/operation/ExpressionTypeTest.java b/basics/src/test/java/pl/mperor/lab/java/data/operation/ExpressionTypeTest.java new file mode 100644 index 0000000..bcb782f --- /dev/null +++ b/basics/src/test/java/pl/mperor/lab/java/data/operation/ExpressionTypeTest.java @@ -0,0 +1,30 @@ +package pl.mperor.lab.java.data.operation; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class ExpressionTypeTest { + + @Test + public void testExpressionVsStatement() { + byte b = 1; + Assertions.assertEquals(2, b + b, "statement"); + int expression = b + b; + Assertions.assertEquals(2, expression, "expression"); + } + + @Test + public void testTypeOfArithmeticExpression() { + byte b = 1; + Assertions.assertInstanceOf(Integer.class, b + b); + short s = 2; + Assertions.assertInstanceOf(Integer.class, s + s); + Assertions.assertInstanceOf(Long.class, 1 + 1L); + + Assertions.assertInstanceOf(Float.class, 1.0f + 1.0f); + Assertions.assertInstanceOf(Double.class, 1 + 1.0); + Assertions.assertInstanceOf(Double.class, 1.0f + 1.0); + Assertions.assertInstanceOf(String.class, "" + 1.0); + } + +} diff --git a/basics/src/test/java/pl/mperor/lab/java/data/operation/OperatorsTest.java b/basics/src/test/java/pl/mperor/lab/java/data/operation/OperatorsTest.java new file mode 100644 index 0000000..be24112 --- /dev/null +++ b/basics/src/test/java/pl/mperor/lab/java/data/operation/OperatorsTest.java @@ -0,0 +1,83 @@ +package pl.mperor.lab.java.data.operation; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class OperatorsTest { + + @Test + public void testUnaryArithmeticOperators() { + Assertions.assertEquals(1, +1, "plus"); + Assertions.assertEquals(-1, -1, "minus"); + int i = 10; + Assertions.assertEquals(10, i++, "post-increment"); + Assertions.assertEquals(10, --i, "pre-decrement"); + } + + @Test + public void testBinaryArithmeticOperators() { + Assertions.assertEquals(3, 1 + 2, "addition"); + Assertions.assertEquals(1, 2 - 1, "subtraction"); + Assertions.assertEquals(4, 2 * 2, "multiplication"); + Assertions.assertEquals(2, 4 / 2, "division"); + Assertions.assertEquals(2, 5 % 3, "modulo"); + Assertions.assertEquals("ab", "a" + "b", "string concatenation"); + } + + @Test + public void testAssignmentOperators() { + int i = 1; + i *= 2; // same as i = i * 2; + String s = "a"; + s += "b"; + Assertions.assertEquals(2, i); + Assertions.assertEquals("ab", s); + } + + @Test + public void testComparisonOperators() { + Assertions.assertTrue(1 == 1, "equals to"); + Assertions.assertTrue(1 != 2, "not equals to"); + Assertions.assertTrue(2 > 1, "greater than"); + Assertions.assertTrue(1 < 2, "less than"); + Assertions.assertFalse(2 >= 4, "greater than or equals to"); + Assertions.assertFalse(4 <= 2, "less than or equals to"); + } + + @Test + public void testLogicalOperators() { + Assertions.assertTrue(true & true, "and"); + Assertions.assertFalse(false && true, "short and"); + Assertions.assertTrue(true | true, "or"); + Assertions.assertTrue(false || true, "short or"); + Assertions.assertFalse(!true, "not"); + Assertions.assertFalse(true ^ true, "xor"); + } + + @Test + public void testBitwiseOperators() { + Assertions.assertEquals(0b0001, 0b1001 & 0b0011, "and"); + Assertions.assertEquals(0b1011, 0b1001 | 0b0011, "or"); + Assertions.assertEquals(0b1010, 0b1001 ^ 0b0011, "xor"); + Assertions.assertEquals(0b11111111_11111111_11111111_11110110, ~0b1001, "flip"); + Assertions.assertEquals(0b0110, 0b0011 << 1, "left shift"); + Assertions.assertEquals(0b0001, 0b0011 >> 1, "right shift"); + Assertions.assertEquals( + 0b01000000_00000000_00000000_00000000, + 0b10000000_00000000_00000000_00000001 >>> 1, + "unsigned right shift" + ); + } + + @Test + public void testTernaryOperator() { + Assertions.assertTrue(isEmpty(null)); + Assertions.assertTrue(isEmpty("")); + Assertions.assertFalse(isEmpty("not empty")); + } + + private static boolean isEmpty(String string) { + return string == null ? true : string.isEmpty(); + } + +} diff --git a/basics/src/test/java/pl/mperor/lab/java/data/operation/PrimitiveAndReferenceTypesComparisonTest.java b/basics/src/test/java/pl/mperor/lab/java/data/operation/PrimitiveAndReferenceTypesComparisonTest.java new file mode 100644 index 0000000..0d0d571 --- /dev/null +++ b/basics/src/test/java/pl/mperor/lab/java/data/operation/PrimitiveAndReferenceTypesComparisonTest.java @@ -0,0 +1,34 @@ +package pl.mperor.lab.java.data.operation; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +public class PrimitiveAndReferenceTypesComparisonTest { + + @Test + public void testComparePrimitiveTypes() { + Assertions.assertTrue(1 == 1); + Assertions.assertFalse(1 == 2); + Assertions.assertTrue(65 == 'A'); + } + + @SuppressWarnings("removal") + @Test + public void testCompareReferenceTypes() { + Assertions.assertTrue(Integer.valueOf(127) == Integer.valueOf(127), "Integer pool (-128, 127)"); + Assertions.assertFalse(Integer.valueOf(128) == Integer.valueOf(128)); + + Assertions.assertTrue(new Integer(1).equals(new Integer(1)), "equals to"); + Assertions.assertFalse(new Integer(1) == new Integer(1), "=="); + } + + @Test + public void testCompareArrays() { + int[] a1 = {1, 2, 3}, a2 = {1, 2, 3}; + Assertions.assertFalse(a1 == a2); + Assertions.assertFalse((a1.equals(a2))); + Assertions.assertTrue(Arrays.equals(a1, a2)); + } +}